Results 1 to 5 of 5

Thread: Problem when implement "SpecialFolderLocation"

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Problem when implement "SpecialFolderLocation"

    Hello All
    I have problem when i get startupmenu location path to combine with Application EXE Name.
    here the complete code:
    Code:
    Declare Function SHGetSpecialFolderLocation Lib "shell32" (ByVal hwnd As Long, ByVal nFolder As Long, Pidl As Long) As Long
    
    Declare Function SHGetPathFromIDList Lib "shell32" (Pidl As Long, ByVal FolderPath As String) As Long
        Const CSIDL_COMMON_STARTUP = 24
        Const MAX_PATH = 260
    
    Public Function StartupMenu() As String
        Dim lpStartupPath As String * MAX_PATH
        Dim Pidl As Long
        Dim hResult As Long
        
        hResult = SHGetSpecialFolderLocation(0, CSIDL_COMMON_STARTUP, Pidl)
    
    
        If hResult = 0 Then
            hResult = SHGetPathFromIDList(ByVal Pidl, lpStartupPath)
    
    
            If hResult = 1 Then
                lpStartupPath = Left(lpStartupPath, InStr(lpStartupPath, Chr(0)) - 1)
                StartupMenu = lpStartupPath
            End If
        End If
    End Function
    
    Private Sub Form_Load()
     S = StartupMenu
     sDest = S & "\" & App.EXEName & ".EXE"
    End Sub
    problem is here:
    Code:
    sDest = S & "\" & App.EXEName & ".EXE"
    because sDest always give startUpPath location "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\"
    should be "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\myapp.exe"

    but, when i do manually, this is work:
    Code:
    S = "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\" & App.EXEName & ".EXE"
    but this is not what i want, i want to use this way :
    sDest = S & "\" & App.EXEName & ".EXE"

    Anyone know to solve my problem..

    thank you

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Problem when implement "SpecialFolderLocation"

    Try:

    Code:
    Option Explicit
    
    Private Const ssfCOMMONSTARTUP As Long = 24
    
    Private Sub Form_Load()
        Dim sDest As String
    
        sDest = CreateObject("Shell.Application").NameSpace(ssfCOMMONSTARTUP).Self.Path & ("\" & App.EXEName & ".exe")
        Debug.Print """" & sDest & """"
    End Sub
    Note that Microsoft warns:

    Quote Originally Posted by MSDN
    [SHGetSpecialFolderLocation is not supported and may be altered or unavailable in the future. Instead, use SHGetFolderLocation.]
    That is why it is probably a better idea to use the Shell object's NameSpace method instead because it works in all Windows OSs.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Problem when implement "SpecialFolderLocation"

    I agree with Bonnie West's alternative, which is far less trouble and has far less to go wrong.


    Your code has a lot of problems as written. Memory leaks, sloppy use of Variant-typed functions, assumption that API "true" is = 1, misuse of VB6 fixed-length Strings, etc.

    Here's a working version that addresses many of these flaws. But before using it I'd add logic to raise exceptions on API call errors. Or better yet use the other approach.

    Code:
    Option Explicit
    
    Private Const CSIDL_COMMON_STARTUP = 24
    Private Const MAX_PATH = 260
    Private Const NOERROR = 0
    Private Const API_FALSE = 0
    
    Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal pv As Long)
    
    Private Declare Function SHGetSpecialFolderLocation Lib "shell32" ( _
        ByVal hWnd As Long, _
        ByVal nFolder As Long, _
        ByRef Pidl As Long) As Long
    
    Private Declare Function SHGetPathFromIDList Lib "shell32" _
        Alias "SHGetPathFromIDListW" ( _
        ByVal Pidl As Long, _
        ByVal pFolderPath As Long) As Long
    
    Public Function StartupMenu() As String
        Dim StartupPath As String
        Dim Pidl As Long
    
        If SHGetSpecialFolderLocation(hWnd, CSIDL_COMMON_STARTUP, Pidl) = NOERROR Then
            StartupPath = String$(MAX_PATH, 0)
            If SHGetPathFromIDList(Pidl, StrPtr(StartupPath)) <> API_FALSE Then
                StartupMenu = Left$(StartupPath, InStr(StartupPath, vbNullChar) - 1)
            End If
            CoTaskMemFree Pidl
        End If
    End Function
    
    Private Sub Form_Load()
        MsgBox StartupMenu() & "\" & App.EXEName & ".EXE"
    End Sub

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Problem when implement "SpecialFolderLocation"

    Quick question... is the the app REALLY installed in your menu? I suspect probably not... usually a SHORTCUT is placed there, but not the app itself... so appending the exe name to the startup path, depending on what you're planning to do with it, isn't the right thing to do anyways.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Problem when implement "SpecialFolderLocation"

    ok, thanks all for your replay.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width