Real Software Forums
http://forums.realsoftware.com/

Createprocess() & Shellexecute Directory Issues.
http://forums.realsoftware.com/viewtopic.php?f=6&t=46939
Page 1 of 1

Author:  lukus001 [ Thu Feb 14, 2013 4:26 am ]
Post subject:  Createprocess() & Shellexecute Directory Issues.

Hey guys,

I have tried both CreateProcess() and ShellExecute() to launch a program and both return error codes 267 / 2 respectively (directory /file does not exist).

The Program /Directory 100% does exist - not only was it copied from explorer, I can cut and paste it into cmd.exe and it will load / launch.

I have tried changing directories, tried launching other programs, tried various combinations of directories /command line variations etc and it seems windows refuses to acknowledge the directory?

This is on windows 8 if that changes anything. Is there any particular reason why realstudio in combination with this API wouldn't work? I am completely lost as to how to launch a simple program .

Folderitem.launch is not a viable option because i need to set the "current directory" of the launched application for it to load properly. If i place my built real studio application in the "current directory" needed (because it inherits the current directory) it will work, but that is not a viable solution because it regularly changes and there is more than one application that needs launching.

Any help much appreciated

edit: Well I managed to get it to work yey!

Author:  kermit [ Thu Feb 14, 2013 5:25 am ]
Post subject:  Re: Createprocess() & Shellexecute Directory Issues.

Quote:
Folderitem.launch is not a viable option

Yes it is.

Quote:
i need to set the "current directory" of the launched application

why?

if you know the right path to the file, you can do

myFolderItem = GetFolderItem(theFullPath)
after that, does myFolderItem.exists return true? If no, the path is wrong.

If yes, then myFolderItem.launch should work just fine
You shouldnt normally be forcing the launched EXE to 'think' it is running in another location.

Quote:
If i place my built real studio application in the "current directory" needed (because it inherits the current directory) it will work


So are you NOT supplying the full path to the required file?
That will only work if the program you want to laucnh exists on the windows 'PATH' environment variable.
(Places where it looks for exectuables if someone forgets to say where the program lives)

Quote:
that is not a viable solution because it regularly changes

That sounds suspect...

Author:  lukus001 [ Thu Feb 14, 2013 5:40 am ]
Post subject:  Re: Createprocess() & Shellexecute Directory Issues.

kermit wrote:
You shouldnt normally be forcing the launched EXE to 'think' it is running in another location.

...

So are you NOT supplying the full path to the required file?
That will only work if the program you want to laucnh exists on the windows 'PATH' environment variable.
(Places where it looks for exectuables if someone forgets to say where the program lives)
I think you misunderstood.

Every application has a "current directory" value which is irrelevant to where the application is actually located or one's ability to load that application, but affects that application's relative references to files or directories.

Processmonitor will show "application: C:\fulladdress to executable.exe + commandline vars" & "currentDirectory: C:\myrealstudioapp_directory"

The problem is, that the application I want to load requires access to certain files relative to it's location or it will close, and for it to have access to those files it's "current directory" value has to be set to the directory the executable was located. This is not my application, this is not something I can alter, it is out of my control regardless of whether that is or isn't normal behavior.

The problem with folderitem.launch is that it does not allow one to set the "current directory" value, the launched process then inherits the parent processes' directory which of course will not work for the sub process and it will self terminate.

I will clarify, folderitem.launch DOES load the application but the application will self terminate without the correct "current directory" value set, which folderitem.launch does not facilitate.

Thus, I require CreateProcess() or ShellExecute() both of which seem to dislike accepting the directory even though it's valid




edit: Well I managed to get it to work yey!

Author:  charonn0 [ Thu Feb 14, 2013 12:53 pm ]
Post subject:  Re: Createprocess() & Shellexecute Directory Issues.

Since apps launched with FolderItem.Launch inherit the parent app's CWD, change the CWD before launching and then change it back, e.g.:

Sub LaunchInWD(File As FolderItem, WD As FolderItem)
Declare Function SetCurrentDirectoryW Lib "Kernel32" (PathName As WString) As Boolean
Declare Function GetCurrentDirectoryW Lib "Kernel32" (BufferLen As Integer, Buffer As Ptr) As Integer

'first get the actual CWD
Dim buffer As New MemoryBlock(2048)
Dim sz As Integer = GetCurrentDirectoryW(buffer.Size, Buffer)
If sz > Buffer.Size And sz <> 0 Then
Buffer = New MemoryBlock(sz)
Call GetCurrentDirectoryW(buffer.Size, Buffer)
End If

'set the process working directory to WD
If SetCurrentDirectoryW(CWD.AbsolutePath) Then
file.Launch 'Launch the app
Call SetCurrentDirectoryW(Buffer) 'reset the CWD
End If
End Sub

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/