-
Jun 13th, 2013, 10:18 PM
#1
Thread Starter
Addicted Member
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
-
Jun 13th, 2013, 10:54 PM
#2
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:
Originally Posted by MSDN
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)
-
Jun 14th, 2013, 02:48 PM
#3
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
-
Jun 14th, 2013, 03:51 PM
#4
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
-
Jun 24th, 2013, 09:42 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|