[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Problem using ShellExecute

Norm

10/2/2010 1:52:00 PM

Hi,

I have a program that hides the desktop shortcuts and displays a list
of the shortcuts when you click on the program menu and then allows you
to run them by click on the name of the shortcut.

Everything works fine except when using ShellExecute one program does
not run, although it will run if I use just Shell. The code is below
and I have tried including the startin path and enclosing the path in
double quotes, but ShellExecute would not run the program. I am also
including the path in case I missed something in it.

Thanks,
Norm

Program Path: "C:\Program Files (x86)\Spybot\SpybotSD.exe"

Code That Works:

Dim i As Long
Dim ExPath As String
Dim mnuPath As String

mnuPath = mnuPop(Index).Caption

For i = 0 To lstDeskPath.ListCount - 1
If InStr(1, lstDeskPath.List(i), mnuPath, vbTextCompare) > 0 Then
'This returns the target path of the shortcut
ExPath = ResolveLink(lstDeskPath.List(i))
'Debug.Print ExPath
If Right$(ExPath, Len(ExPath) - (Len(ExPath) - 4)) = ".exe" Then
Shell ExPath, vbNormalFocus
Else
ShellExecute Me.hwnd, "Open", ExPath, "", "", 1
End If

Exit For
End If
Next i


15 Answers

(nobody)

10/2/2010 4:37:00 PM

0

"Norm" <NormF4@Spoof.com> wrote in message
news:i87dfl$660$1@news.eternal-september.org...
> Hi,
>
> I have a program that hides the desktop shortcuts and displays a list of
> the shortcuts when you click on the program menu and then allows you to
> run them by click on the name of the shortcut.
>
> Everything works fine except when using ShellExecute one program does not
> run, although it will run if I use just Shell. The code is below and I
> have tried including the startin path and enclosing the path in double
> quotes, but ShellExecute would not run the program. I am also including
> the path in case I missed something in it.
>
> Thanks,
> Norm
>
> Program Path: "C:\Program Files (x86)\Spybot\SpybotSD.exe"
>
> Code That Works:
>
> Dim i As Long
> Dim ExPath As String
> Dim mnuPath As String
>
> mnuPath = mnuPop(Index).Caption
>
> For i = 0 To lstDeskPath.ListCount - 1
> If InStr(1, lstDeskPath.List(i), mnuPath, vbTextCompare) > 0 Then
> 'This returns the target path of the shortcut
> ExPath = ResolveLink(lstDeskPath.List(i))
> 'Debug.Print ExPath
> If Right$(ExPath, Len(ExPath) - (Len(ExPath) - 4)) = ".exe" Then

Not that it solves the problem, but the two Len() cancel each other and the
whole thing evaluates to +4.

> Shell ExPath, vbNormalFocus
> Else
> ShellExecute Me.hwnd, "Open", ExPath, "", "", 1

Check the return value and Err.LastDllError. Example:

Dim ret As Long
ret = ShellExecute(Me.hwnd, "Open", ExPath, "", "", 1)
If ret <= 32 Then
MsgBox "ShellExecute failed, ret = " & ret & ", LastDllError = " &
Err.LastDllError
End If



Norm

10/2/2010 5:30:00 PM

0

Nobody presented the following explanation :
> "Norm" <NormF4@Spoof.com> wrote in message
> news:i87dfl$660$1@news.eternal-september.org...
>> Hi,
>>
>> I have a program that hides the desktop shortcuts and displays a list of
>> the shortcuts when you click on the program menu and then allows you to run
>> them by click on the name of the shortcut.
>>
>> Everything works fine except when using ShellExecute one program does not
>> run, although it will run if I use just Shell. The code is below and I have
>> tried including the startin path and enclosing the path in double quotes,
>> but ShellExecute would not run the program. I am also including the path in
>> case I missed something in it.
>>
>> Thanks,
>> Norm
>>
>> Program Path: "C:\Program Files (x86)\Spybot\SpybotSD.exe"
>>
>> Code That Works:
>>
>> Dim i As Long
>> Dim ExPath As String
>> Dim mnuPath As String
>>
>> mnuPath = mnuPop(Index).Caption
>>
>> For i = 0 To lstDeskPath.ListCount - 1
>> If InStr(1, lstDeskPath.List(i), mnuPath, vbTextCompare) > 0 Then
>> 'This returns the target path of the shortcut
>> ExPath = ResolveLink(lstDeskPath.List(i))
>> 'Debug.Print ExPath
>> If Right$(ExPath, Len(ExPath) - (Len(ExPath) - 4)) = ".exe" Then
>
> Not that it solves the problem, but the two Len() cancel each other and the
> whole thing evaluates to +4.
>
>> Shell ExPath, vbNormalFocus
>> Else
>> ShellExecute Me.hwnd, "Open", ExPath, "", "", 1
>
> Check the return value and Err.LastDllError. Example:
>
> Dim ret As Long
> ret = ShellExecute(Me.hwnd, "Open", ExPath, "", "", 1)
> If ret <= 32 Then
> MsgBox "ShellExecute failed, ret = " & ret & ", LastDllError = " &
> Err.LastDllError
> End If

Now I am stumped, when I was testing earlier the return value was 42,
which I believe means it was working, but the program did not start. I
left to go eat breakfast and shut the computer down.

Upon testing again I get the same return value, but now the program I
am trying to start, does start.

Another one of those things to remind me when it is not working and I
am not sure why, reboot the computer and try again. :-)

Norm


(nobody)

10/2/2010 6:15:00 PM

0

"Norm" <NormF4@Spoof.com> wrote in message
news:i87q89$rbf$1@news.eternal-september.org...
> Now I am stumped, when I was testing earlier the return value was 42,
> which I believe means it was working, but the program did not start. I
> left to go eat breakfast and shut the computer down.
>
> Upon testing again I get the same return value, but now the program I am
> trying to start, does start.
>
> Another one of those things to remind me when it is not working and I am
> not sure why, reboot the computer and try again. :-)

I tested shelling a copy of Notepad under Windows 7 32-Bit with a path with
spaces on it, and I got successful return value(42), but Notepad didn't
start or at least I don't see it in Task Manager, whether I used Shell or
ShellExecute. Then I replaced the Notepad.exe copy with a VB6 made EXE, and
it showed up. Here is my code, add two command buttons to test it, and put a
copy of Notepad in a subfolder named "Test 1", with a space between "Test"
and "1".

Option Explicit

Private Const SW_SHOWNORMAL = 1
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Shell App.Path & "\Test 1\Notepad.exe", vbNormalFocus
End Sub

Private Sub Command2_Click()
Dim ret As Long
ret = ShellExecute(Me.hwnd, "Open", App.Path & "\Test 1\Notepad.exe", _
"", "", SW_SHOWNORMAL)
MsgBox "ShellExecute returned " & ret & ", LastDllError = " & _
Err.LastDllError
End Sub


Norm

10/2/2010 8:42:00 PM

0

Nobody formulated the question :
> "Norm" <NormF4@Spoof.com> wrote in message
> news:i87q89$rbf$1@news.eternal-september.org...
>> Now I am stumped, when I was testing earlier the return value was 42, which
>> I believe means it was working, but the program did not start. I left to go
>> eat breakfast and shut the computer down.
>>
>> Upon testing again I get the same return value, but now the program I am
>> trying to start, does start.
>>
>> Another one of those things to remind me when it is not working and I am
>> not sure why, reboot the computer and try again. :-)
>
> I tested shelling a copy of Notepad under Windows 7 32-Bit with a path with
> spaces on it, and I got successful return value(42), but Notepad didn't start
> or at least I don't see it in Task Manager, whether I used Shell or
> ShellExecute. Then I replaced the Notepad.exe copy with a VB6 made EXE, and
> it showed up. Here is my code, add two command buttons to test it, and put a
> copy of Notepad in a subfolder named "Test 1", with a space between "Test"
> and "1".
>
> Option Explicit
>
> Private Const SW_SHOWNORMAL = 1
> Private Declare Function ShellExecute Lib "shell32.dll" Alias _
> "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
> ByVal lpFile As String, ByVal lpParameters As String, _
> ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
>
> Private Sub Command1_Click()
> Shell App.Path & "\Test 1\Notepad.exe", vbNormalFocus
> End Sub
>
> Private Sub Command2_Click()
> Dim ret As Long
> ret = ShellExecute(Me.hwnd, "Open", App.Path & "\Test 1\Notepad.exe", _
> "", "", SW_SHOWNORMAL)
> MsgBox "ShellExecute returned " & ret & ", LastDllError = " & _
> Err.LastDllError
> End Sub

I spoke too soon before, as I had forgoten to comment out the Shell
line. I should know not to keep working with no sleep. :-)

I tried your test and got two different results. If I just opened a new
VB6 and printed out app.path it showed C:\Windows\System32 so I created
a folder called Test 1 and put Notepad into it, but neither shell or
shellexecute would run. Shell stated file not found, shellexecute
returned an error of 2. But thinking about it I am using Win7 64 bit,
so even though app.path returns System32 I wonder if it should be under
Wow.

Anyway I saved the project to another drive copied the Test 1 folder
there and both Shell and ShellExecute worked fine.

I am not sure why the one file of mine will not run, there are a lot of
other paths with spaces in them and they work just fine. Maybe we
tripped over a bug in Win7.

I have been going nuts all day trying to add the desktop icons to the
shortcuts I load from the desktop. I create the menu's on the fly and
so far have not had any luck with adding the icons.

Anyway let me know if you figure out why that is not working on your
system.

Norm


Helmut_Meukel

10/2/2010 9:24:00 PM

0

Norm schrieb :
> Nobody formulated the question :
>> "Norm" <NormF4@Spoof.com> wrote in message
>> news:i87q89$rbf$1@news.eternal-september.org...
>>> Now I am stumped, when I was testing earlier the return value was 42,
>>> which I believe means it was working, but the program did not start. I
>>> left to go eat breakfast and shut the computer down.
>>>
>>> Upon testing again I get the same return value, but now the program I am
>>> trying to start, does start.
>>>
>>> Another one of those things to remind me when it is not working and I am
>>> not sure why, reboot the computer and try again. :-)
>>
>> I tested shelling a copy of Notepad under Windows 7 32-Bit with a path with
>> spaces on it, and I got successful return value(42), but Notepad didn't
>> start or at least I don't see it in Task Manager, whether I used Shell or
>> ShellExecute. Then I replaced the Notepad.exe copy with a VB6 made EXE, and
>> it showed up. Here is my code, add two command buttons to test it, and put
>> a copy of Notepad in a subfolder named "Test 1", with a space between
>> "Test" and "1".
>>
>> Option Explicit
>>
>> Private Const SW_SHOWNORMAL = 1
>> Private Declare Function ShellExecute Lib "shell32.dll" Alias _
>> "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
>> ByVal lpFile As String, ByVal lpParameters As String, _
>> ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
>>
>> Private Sub Command1_Click()
>> Shell App.Path & "\Test 1\Notepad.exe", vbNormalFocus
>> End Sub
>>
>> Private Sub Command2_Click()
>> Dim ret As Long
>> ret = ShellExecute(Me.hwnd, "Open", App.Path & "\Test 1\Notepad.exe", _
>> "", "", SW_SHOWNORMAL)
>> MsgBox "ShellExecute returned " & ret & ", LastDllError = " & _
>> Err.LastDllError
>> End Sub
>
> I spoke too soon before, as I had forgoten to comment out the Shell line. I
> should know not to keep working with no sleep. :-)
>
> I tried your test and got two different results. If I just opened a new VB6
> and printed out app.path it showed C:\Windows\System32 so I created a folder
> called Test 1 and put Notepad into it, but neither shell or shellexecute
> would run. Shell stated file not found, shellexecute returned an error of 2.
> But thinking about it I am using Win7 64 bit, so even though app.path returns
> System32 I wonder if it should be under Wow.
>
> Anyway I saved the project to another drive copied the Test 1 folder there
> and both Shell and ShellExecute worked fine.
>
> I am not sure why the one file of mine will not run, there are a lot of other
> paths with spaces in them and they work just fine. Maybe we tripped over a
> bug in Win7.
>
> I have been going nuts all day trying to add the desktop icons to the
> shortcuts I load from the desktop. I create the menu's on the fly and so far
> have not had any luck with adding the icons.
>
> Anyway let me know if you figure out why that is not working on your system.
>

Curiouser and curiouser.
I just tried it on my Win7 system (just the Shell):
I had opened TaskManager/processes to see what happens.
Sometimes TaskManager shows notepad.exe but it will always go away
immediadtely. I think the other times the execution time was too short
to show up in TaskManager.
The directory was on an other drive with no restrictions.
I tried it both ways, logged on as Admin and VB6 started normal and
VB6 started right-clicked as Admin. No change.

Helmut.


Norm

10/2/2010 9:27:00 PM

0

Helmut_Meukel used his keyboard to write :
> Norm schrieb :
>> Nobody formulated the question :
>>> "Norm" <NormF4@Spoof.com> wrote in message
>>> news:i87q89$rbf$1@news.eternal-september.org...
>>>> Now I am stumped, when I was testing earlier the return value was 42,
>>>> which I believe means it was working, but the program did not start. I
>>>> left to go eat breakfast and shut the computer down.
>>>>
>>>> Upon testing again I get the same return value, but now the program I am
>>>> trying to start, does start.
>>>>
>>>> Another one of those things to remind me when it is not working and I am
>>>> not sure why, reboot the computer and try again. :-)
>>>
>>> I tested shelling a copy of Notepad under Windows 7 32-Bit with a path
>>> with spaces on it, and I got successful return value(42), but Notepad
>>> didn't start or at least I don't see it in Task Manager, whether I used
>>> Shell or ShellExecute. Then I replaced the Notepad.exe copy with a VB6
>>> made EXE, and it showed up. Here is my code, add two command buttons to
>>> test it, and put a copy of Notepad in a subfolder named "Test 1", with a
>>> space between "Test" and "1".
>>>
>>> Option Explicit
>>>
>>> Private Const SW_SHOWNORMAL = 1
>>> Private Declare Function ShellExecute Lib "shell32.dll" Alias _
>>> "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
>>> ByVal lpFile As String, ByVal lpParameters As String, _
>>> ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
>>>
>>> Private Sub Command1_Click()
>>> Shell App.Path & "\Test 1\Notepad.exe", vbNormalFocus
>>> End Sub
>>>
>>> Private Sub Command2_Click()
>>> Dim ret As Long
>>> ret = ShellExecute(Me.hwnd, "Open", App.Path & "\Test 1\Notepad.exe",
>>> _
>>> "", "", SW_SHOWNORMAL)
>>> MsgBox "ShellExecute returned " & ret & ", LastDllError = " & _
>>> Err.LastDllError
>>> End Sub
>>
>> I spoke too soon before, as I had forgoten to comment out the Shell line. I
>> should know not to keep working with no sleep. :-)
>>
>> I tried your test and got two different results. If I just opened a new VB6
>> and printed out app.path it showed C:\Windows\System32 so I created a
>> folder called Test 1 and put Notepad into it, but neither shell or
>> shellexecute would run. Shell stated file not found, shellexecute returned
>> an error of 2. But thinking about it I am using Win7 64 bit, so even though
>> app.path returns System32 I wonder if it should be under Wow.
>>
>> Anyway I saved the project to another drive copied the Test 1 folder there
>> and both Shell and ShellExecute worked fine.
>>
>> I am not sure why the one file of mine will not run, there are a lot of
>> other paths with spaces in them and they work just fine. Maybe we tripped
>> over a bug in Win7.
>>
>> I have been going nuts all day trying to add the desktop icons to the
>> shortcuts I load from the desktop. I create the menu's on the fly and so
>> far have not had any luck with adding the icons.
>>
>> Anyway let me know if you figure out why that is not working on your
>> system.
>>
>
> Curiouser and curiouser.
> I just tried it on my Win7 system (just the Shell):
> I had opened TaskManager/processes to see what happens.
> Sometimes TaskManager shows notepad.exe but it will always go away
> immediadtely. I think the other times the execution time was too short
> to show up in TaskManager.
> The directory was on an other drive with no restrictions.
> I tried it both ways, logged on as Admin and VB6 started normal and
> VB6 started right-clicked as Admin. No change.
>
> Helmut.

Hi Helmut,

I am beginning the think I might have stumbled across something that is
not my fault. :-)

Norm


Henning

10/2/2010 11:30:00 PM

0


"Norm" <NormF4@Spoof.com> skrev i meddelandet
news:i8883f$614$1@news.eternal-september.org...
> Helmut_Meukel used his keyboard to write :
>> Norm schrieb :
>>> Nobody formulated the question :
>>>> "Norm" <NormF4@Spoof.com> wrote in message
>>>> news:i87q89$rbf$1@news.eternal-september.org...
>>>>> Now I am stumped, when I was testing earlier the return value was 42,
>>>>> which I believe means it was working, but the program did not start. I
>>>>> left to go eat breakfast and shut the computer down.
>>>>>
>>>>> Upon testing again I get the same return value, but now the program I
>>>>> am trying to start, does start.
>>>>>
>>>>> Another one of those things to remind me when it is not working and I
>>>>> am not sure why, reboot the computer and try again. :-)
>>>>
>>>> I tested shelling a copy of Notepad under Windows 7 32-Bit with a path
>>>> with spaces on it, and I got successful return value(42), but Notepad
>>>> didn't start or at least I don't see it in Task Manager, whether I used
>>>> Shell or ShellExecute. Then I replaced the Notepad.exe copy with a VB6
>>>> made EXE, and it showed up. Here is my code, add two command buttons to
>>>> test it, and put a copy of Notepad in a subfolder named "Test 1", with
>>>> a space between "Test" and "1".
>>>>
>>>> Option Explicit
>>>>
>>>> Private Const SW_SHOWNORMAL = 1
>>>> Private Declare Function ShellExecute Lib "shell32.dll" Alias _
>>>> "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
>>>> ByVal lpFile As String, ByVal lpParameters As String, _
>>>> ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
>>>>
>>>> Private Sub Command1_Click()
>>>> Shell App.Path & "\Test 1\Notepad.exe", vbNormalFocus
>>>> End Sub
>>>>
>>>> Private Sub Command2_Click()
>>>> Dim ret As Long
>>>> ret = ShellExecute(Me.hwnd, "Open", App.Path & "\Test
>>>> 1\Notepad.exe", _
>>>> "", "", SW_SHOWNORMAL)
>>>> MsgBox "ShellExecute returned " & ret & ", LastDllError = " & _
>>>> Err.LastDllError
>>>> End Sub
>>>
>>> I spoke too soon before, as I had forgoten to comment out the Shell
>>> line. I should know not to keep working with no sleep. :-)
>>>
>>> I tried your test and got two different results. If I just opened a new
>>> VB6 and printed out app.path it showed C:\Windows\System32 so I created
>>> a folder called Test 1 and put Notepad into it, but neither shell or
>>> shellexecute would run. Shell stated file not found, shellexecute
>>> returned an error of 2. But thinking about it I am using Win7 64 bit, so
>>> even though app.path returns System32 I wonder if it should be under
>>> Wow.
>>>
>>> Anyway I saved the project to another drive copied the Test 1 folder
>>> there and both Shell and ShellExecute worked fine.
>>>
>>> I am not sure why the one file of mine will not run, there are a lot of
>>> other paths with spaces in them and they work just fine. Maybe we
>>> tripped over a bug in Win7.
>>>
>>> I have been going nuts all day trying to add the desktop icons to the
>>> shortcuts I load from the desktop. I create the menu's on the fly and so
>>> far have not had any luck with adding the icons.
>>>
>>> Anyway let me know if you figure out why that is not working on your
>>> system.
>>>
>>
>> Curiouser and curiouser.
>> I just tried it on my Win7 system (just the Shell):
>> I had opened TaskManager/processes to see what happens.
>> Sometimes TaskManager shows notepad.exe but it will always go away
>> immediadtely. I think the other times the execution time was too short
>> to show up in TaskManager.
>> The directory was on an other drive with no restrictions.
>> I tried it both ways, logged on as Admin and VB6 started normal and
>> VB6 started right-clicked as Admin. No change.
>>
>> Helmut.
>
> Hi Helmut,
>
> I am beginning the think I might have stumbled across something that is
> not my fault. :-)
>
> Norm
>
>

From what I read in nobodys reply. Create a Folder anyname, copy
Notepad.exe -> Test 1.exe and run his code.

/Henning


(nobody)

10/3/2010 10:05:00 AM

0

"Helmut_Meukel" <Helmut_Meukel@bn-hof.invalid> wrote in message
news:i887tn$tb4$1@news.eternal-september.org...
> Curiouser and curiouser.
> I just tried it on my Win7 system (just the Shell):
> I had opened TaskManager/processes to see what happens.
> Sometimes TaskManager shows notepad.exe but it will always go away
> immediadtely. I think the other times the execution time was too short
> to show up in TaskManager.
> The directory was on an other drive with no restrictions.
> I tried it both ways, logged on as Admin and VB6 started normal and
> VB6 started right-clicked as Admin. No change.

That's correct. Notepad on Windows 7 starts and terminates quickly when you
copy it to another folder. Neither Task Manager nor Process Explorer show
processes that start or terminate quickly unless the process was running at
the time they check for processes.

Under XP, Notepad starts fine, both ways(Shell/ShellExecute).


Norm

10/3/2010 5:51:00 PM

0

Nobody expressed precisely :
> "Helmut_Meukel" <Helmut_Meukel@bn-hof.invalid> wrote in message
> news:i887tn$tb4$1@news.eternal-september.org...
>> Curiouser and curiouser.
>> I just tried it on my Win7 system (just the Shell):
>> I had opened TaskManager/processes to see what happens.
>> Sometimes TaskManager shows notepad.exe but it will always go away
>> immediadtely. I think the other times the execution time was too short
>> to show up in TaskManager.
>> The directory was on an other drive with no restrictions.
>> I tried it both ways, logged on as Admin and VB6 started normal and
>> VB6 started right-clicked as Admin. No change.
>
> That's correct. Notepad on Windows 7 starts and terminates quickly when you
> copy it to another folder. Neither Task Manager nor Process Explorer show
> processes that start or terminate quickly unless the process was running at
> the time they check for processes.
>
> Under XP, Notepad starts fine, both ways(Shell/ShellExecute).

That is exactly the behavior trying to start SpyBot with ShellExecute
is showing on my Win7 64 Bit, but it does run correctly with Shell.


(nobody)

10/3/2010 7:26:00 PM

0

"Norm" <NormF4@Spoof.com> wrote in message
news:i8afrp$dci$1@news.eternal-september.org...
> That is exactly the behavior trying to start SpyBot with ShellExecute is
> showing on my Win7 64 Bit, but it does run correctly with Shell.

See if this gets you anywhere:

File System Redirector:
http://msdn.microsoft.com/en-us/library/aa384187%28VS....