Error 123 in API call CreateProcessA

Hello everybody

I’ve been developing window applications with Xojo 2013.r4.1 ff. Working with release 2014r3.2 I’ve encountered a problem having to do with creating processes. I tried to create a simple process such as Windows Notepad just to start the program by Windows API CreateProcessA. This fails with error code 123 no matter if I pass the process name in the first argument (AppName) or the following one (AppArgs) while in former releases the process was created without problems. Does anyone know what has changed in this release and what I’m doing wrong now? I can provide an excerpt of the used code:

Soft Declare Function CreateProcess Lib “kernel32.dll” Alias “CreateProcessA” ( lpApplicationName As CString, lpCommandLine As CString, _
lpProcessAttributes As Ptr, lpThreadAttributes As Ptr, bInheritHandles As Boolean, dwCreationFlags As integer, lpEnvironment As Ptr, _
lpCurrentDirectory As CString, lpStartupInfo As Ptr, lpProcessInformation As Ptr) As Boolean

dim bRes as Boolean

Const SW_SHOWNORMAL = 1
Const SW_HIDE = 0
Const SW_MINIMIZE = 6
Const STARTF_USESHOWWINDOW = &h00000001

dim ProcInfo as new MemoryBlock(16)
dim StartInfo as new MemoryBlock(64)

StartInfo.Int32Value( 0 ) = StartInfo.Size ’ cb = length of buffer
StartInfo.Int32Value( 44 ) = STARTF_USESHOWWINDOW ’ dwFlags
StartInfo.Int16Value( 48 ) = SW_SHOWNORMAL ’ show window normally

bRes = CreateProcess( Nil, “C:\Windows\otepad.exe”, Nil, Nil, False, 0, Nil, Nil, StartInfo, ProcInfo)

This line doesn’t provide a different result either:

bRes = CreateProcess( “C:\Windows\otepad.exe”, “”, Nil, Nil, False, 0, Nil, Nil, StartInfo, ProcInfo)

The function always returns false and an API call GetLastError retrieves 123.

Thanks in advance.

Greetz
Peter

This may help…

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx

shows the error code as:-

ERROR_INVALID_NAME
    123 (0x7B)
    The filename, directory name, or volume label syntax is incorrect.

Thanks for the quick reply.

But if due to volume, folder or file specification what might be wrong with C:\Windows\otepad.exe? I enter it in Windows 7 Run command line and notepad is launched. Do I need escape sequences or double backslashes \\ like in C-Syntax or must I enclose it in a pair of double quotes (chr(34))?

Well, I can confirm that on Windows7/64Bit your CreateProcess will work with “C:\Windows\otepad.exe” if compiled with Xojo 2014R2.1 while it gives error 123 when compiled with 2014R3.2.

Could this be it ?..

You have startinfo as length 64… I make it 68

Why do you need the API to launch a program ?

Is this not sufficient :

dim f as FolderItem = GetFolderItem("c:\\Windows\otepad.exe", FolderItem.PathTypeShell) f.launch

[quote=163998:@Chris Carter]Could this be it ?..

You have startinfo as length 64… I make it 68[/quote]

Sorry the same mistake occurs. Do I have to move the offsets of the StartInfo block parts?

[quote=164008:@Michel Bujardet]Why do you need the API to launch a program ?

Is this not sufficient :

dim f as FolderItem = GetFolderItem("c:\\Windows\otepad.exe", FolderItem.PathTypeShell) f.launch [/quote]

In my case not as the application which launches child applications needs to keep track of those therefore I need their ProcessIDs.

You can get the process ID through a shell to TaskList.

Using launch is in case you cannot get CreateProcess to work.

The offsets look OK to me.

Probably not your issue, but I have noticed that you are not setting a priority code into dwCreationFlags
( NORMAL_PRIORITY_CLASS = &h20 )

But CreateProcess should still be ready to call. I wonder what has spoilt it. There must be some slight changement just a detail

@Michel Bujardet: If he needs the PID, Tasklist probably would be no help - there may be many instances per executable and you would need to know the PID in order to pick the right line of the output :slight_smile: .

Not if Tasklist is called just before, then immediately after launch, to see which new process has been added with the application name.

Once again, if CreateProcess works, this won’t be necessary.

Just installed latest Xojo on a test VM to get to see the failure…

Looks like a bug in cstring paramters being passed as nil

Changing them to int32 and passing 0 works for me :-

[code] Soft Declare Function CreateProcess Lib “kernel32.dll” Alias “CreateProcessA” ( lpApplicationName As Int32, lpCommandLine As CString, _
lpProcessAttributes As Ptr, lpThreadAttributes As Ptr, bInheritHandles As Boolean, dwCreationFlags As integer, lpEnvironment As Ptr, _
lpCurrentDirectory As Int32, lpStartupInfo As Ptr, lpProcessInformation As Ptr) As Boolean

dim bRes as Boolean

Const SW_SHOWNORMAL = 1
Const SW_HIDE = 0
Const SW_MINIMIZE = 6
Const STARTF_USESHOWWINDOW = &h00000001

dim ProcInfo as new MemoryBlock(16)
dim StartInfo as new MemoryBlock(68)

StartInfo.Int32Value( 0 ) = StartInfo.Size ’ cb = length of buffer
StartInfo.Int32Value( 44 ) = STARTF_USESHOWWINDOW ’ dwFlags
StartInfo.Int16Value( 48 ) = SW_SHOWNORMAL ’ show window normally

bRes = CreateProcess( 0, “C:\Windows\otepad.exe”, Nil, Nil, False, 0, Nil, 0, StartInfo, ProcInfo)[/code]

Hope it works for you too.

I tried the suggested solution of yours, Chris, and yes, for the first time I saw CreateProcess run in the new environment, too. But as soon as I try to pass the commandline argument via the AppArgs variable CreateProcess refused to do its job again. I’d like to keep this routine exchangeable with other application our team is building so we’ll have to do more investigation I’m afraid. But you also changed lpCurrentDirectory to Int32, didn’t you? Do we have to change the commandline argument as well?

Oh sorry of course I can pass arguments via variables in this case, I looked into the wrong branch of the complete code. All in all this solution works fine, thank you very much.

Glad to have been of help…

As this would seem to point to a bug in this release of Xojo with passing a nil pointer via cstring, you might wish to file a feedback report. ( Am getting busy right now, so cannot do it ).

If you need to get the process so you are able to terminate it later you can also do this:

https://forum.xojo.com/11601-end-shell/p1#p160078