Pelles C forum

Pelles C => General discussions => Topic started by: bitcoin on November 11, 2022, 08:32:52 PM

Title: CreateProcess not found ?
Post by: bitcoin on November 11, 2022, 08:32:52 PM
Hello
I have simple code

Code: [Select]
int entry(void)
{
WinExec("cmd.exe /k systeminfo.exe",SW_SHOW);
ExitProcess(0);
}

It works, when I start it from explorer.exe / total commander / cmd.exe / etc.

But when I start it from PellesC IDE (button "Execute" under the menu) I have error "command systeminfo not found" - because cmd.exe don't found this program. Also , same errors are with ping.exe , tracert and other. Why?
Title: Re: CreateProcess not found ?
Post by: TimoVJL on November 12, 2022, 09:18:00 AM
Check Tools -> Options -> Projects -> Folders Executables
If C:\windows\system32 isn't there, add it.
Title: Re: CreateProcess not found ?
Post by: John Z on November 12, 2022, 12:58:36 PM
But when I start it from PellesC IDE (button "Execute" under the menu) I have error "command systeminfo not found" - because cmd.exe don't found this program. Also , same errors are with ping.exe , tracert and other. Why?

When the cmd window opens and displays the error method, type path <return> you will see the problem.

You could use this format as well as TimoVJL more versatile (better) solution:
 WinExec("cmd.exe /k c:\\windows\\syswow64\\systeminfo.exe",SW_SHOW);
or
 WinExec("cmd.exe /k c:\\windows\\system32\\systeminfo.exe",SW_SHOW);


John Z

BTW - if you run the resulting exe outside of Pelles UI you will see that your original code does work, and if you again type path <return> you will see why....
Title: Re: CreateProcess not found ?
Post by: bitcoin on December 10, 2022, 03:12:37 PM
TimoVJL, John Z
both method works, thanks!
Title: Re: CreateProcess not found ?
Post by: Vortex on February 05, 2023, 01:04:16 PM
Hello bitcoin,

This one works too :

Code: [Select]
#include <stdio.h>
#include <windows.h>

int main(void)
{
ShellExecute(0,"open","cmd.exe","/k systeminfo.exe",0,SW_SHOW);
return 0;
}
Title: Re: CreateProcess not found ?
Post by: bitcoin on February 14, 2023, 12:32:07 AM
hello Vortex
And why is that? Is it the key '/k' to or the function ShellExecute?
Title: Re: CreateProcess not found ?
Post by: John Z on February 14, 2023, 02:35:53 PM
And why is that? Is it the key '/k' to or the function ShellExecute?

Hi Bitcoin,

It is not due to the '/k' that just keeps the command window from closing, which then allows you to view the results.

It is the ShellExecute because it uses the registry.  Here is an excerpt from Microsoft at:
https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea

Code: [Select]
Because ShellExecute can delegate execution to Shell extensions (data sources, context menu handlers, verb implementations)
 that are activated using Component Object Model (COM), COM should be initialized before ShellExecute is called. Some Shell
extensions require the COM single-threaded apartment (STA) type. In that case, COM should be initialized as shown here:

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

There are certainly instances where ShellExecute does not use one of these types of Shell extension and those
instances would not require COM to be initialized at all. Nonetheless, it is good practice to always initialize COM
before using this function.

This method allows you to execute any commands in a folder's shortcut menu or stored in the registry.

In this case the needed 'path' which was missing previously is obtained elsewhere. Also note that in many use cases
for ShellExecute you will need coInitializeEx.....

John Z
Title: Re: CreateProcess not found ?
Post by: Vortex on February 14, 2023, 08:42:50 PM
Hi bitcoin,

John's explanation is correct. ShellExecute can be useful for practical programming.