Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CreateProcess() Problem

41 views
Skip to first unread message

AVee

unread,
Apr 9, 2008, 3:42:07 PM4/9/08
to
Can anyone tell me why this call to CreateProcess() succeeds when the .EXE
extension is used for the application name (in this case "NOTEPAD.EXE"), but
fails with Error 2 (file not found) when the .EXE extension is omitted. (The
docs say that the EXE extension should be appended by CreateProcess() if
omitted). It also fails if I do not provide the full path to the application,
though in this case the path to NOTEPAD is included in the Environment.

DWORD error;
int pret;
STARTUPINFO theStartupInfo;
PROCESS_INFORMATION theProcessInfo;
memset(&theStartupInfo, 0, sizeof(theStartupInfo));
memset(&theProcessInfo, 0, sizeof(theProcessInfo));
GetStartupInfo( &theStartupInfo );
pret = CreateProcess(
_T("C:\\Windows\\NOTEPAD.EXE"),
_T(" C:\\testfile.txt"),
NULL, // Process Attributes NULL default
NULL, // Thread attributes NULL
FALSE, // Don't Inherit handles
CREATE_NEW_CONSOLE, //Creation flags
NULL, // Environment same as calling process
NULL, // Current directory same as calling process
&theStartupInfo, // lp to startup info
&theProcessInfo
);
error = GetLastError();

AliR (VC++ MVP)

unread,
Apr 9, 2008, 3:57:45 PM4/9/08
to
The .exe append and path finding is applied if the first parameter is NULL
and the second parameter has the name of the program.

So you would have to make the call like this

CreateProcess(NULL,_T("notepad c:\\testfile.txt),.....);

or you can use
ShellExecute(NULL,_T("open"),_T("notepad"),_T("c:\\testfile.txt"),NULL,SW_SHOWNORMAL);


AliR.

"AVee" <AV...@discussions.microsoft.com> wrote in message
news:3ECCEB36-6C67-412F...@microsoft.com...

Joseph M. Newcomer

unread,
Apr 9, 2008, 5:21:12 PM4/9/08
to
See below...

On Wed, 9 Apr 2008 12:42:07 -0700, AVee <AV...@discussions.microsoft.com> wrote:

>Can anyone tell me why this call to CreateProcess() succeeds when the .EXE
>extension is used for the application name (in this case "NOTEPAD.EXE"), but
>fails with Error 2 (file not found) when the .EXE extension is omitted. (The
>docs say that the EXE extension should be appended by CreateProcess() if
>omitted). It also fails if I do not provide the full path to the application,
>though in this case the path to NOTEPAD is included in the Environment.
>
>DWORD error;
>int pret;
>STARTUPINFO theStartupInfo;
>PROCESS_INFORMATION theProcessInfo;
>memset(&theStartupInfo, 0, sizeof(theStartupInfo));
>memset(&theProcessInfo, 0, sizeof(theProcessInfo));

****
You should learn to write ::ZeroMemory instead of using memset. It is easier to write and
more obvious what your intention is. Also, since ProcessInfo is output only, there is no
reason to prezero it (if you don't prezero startupinfo, your call will fail)
****
>GetStartupInfo( &theStartupInfo );
****
Why did you zero it and then load it? In fact, why do you want to GetStartupInfo?
****


>pret = CreateProcess(
> _T("C:\\Windows\\NOTEPAD.EXE"),

****
This must be the name of a program, but why do you assume that it is in C:\? This is not
a fair assumption, and in multiboot environments it will definitely be wrong. You need to
find out what the current directory is, by using something like GetWindowsDirectory, or
SHGetFolder.

If this is not working correctly, then the documentation may be in error. That's the best
I can say. I'll check this out and may add something to my errors-and-omissions document
if I find what the problem is.
****
> _T(" C:\\testfile.txt"),
****
Note that you cannot assume the root directory is accessible or writeable. And there is
no reason to expect the C: drive really exists.
****


> NULL, // Process Attributes NULL default
> NULL, // Thread attributes NULL
> FALSE, // Don't Inherit handles
> CREATE_NEW_CONSOLE, //Creation flags
> NULL, // Environment same as calling process
> NULL, // Current directory same as calling process
> &theStartupInfo, // lp to startup info
> &theProcessInfo
> );
>error = GetLastError();

****
Note that GetLastError() has no meaning unless you check the return value pret; it is
valid ONLY if the value is FALSE. So this should never be unconditionally retrieved.
joe
****
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

AVee

unread,
Apr 9, 2008, 5:19:01 PM4/9/08
to
Thanks, Alir.

Unknown

unread,
Apr 10, 2008, 5:36:35 AM4/10/08
to
On Wed, 9 Apr 2008 12:42:07 -0700, AVee
<AV...@discussions.microsoft.com> wrote:

>It also fails if I do not provide the full path to the application,
>though in this case the path to NOTEPAD is included in the Environment.

http://bobmoore.mvps.org/Win32/w32tip46.htm

Bob Moore
http://bobmoore.mvps.org/

AVee

unread,
Apr 10, 2008, 9:02:01 PM4/10/08
to
Thanks, Joe. This code is only pasted from the MS example for the purpose of
generating the error for my question. ::ZeroMemory is a good tip. Thanks. You
are right about the documentation error, or at least difficiency, as Bob
Moore pointed out below. As always you have been helpful. Thanks again.

AVee

unread,
Apr 10, 2008, 9:08:00 PM4/10/08
to
Thanks, Bob. Your documentaion is more complete, and I bookmarked your web
page for future reference. Thanks again.

laksh2204

unread,
Aug 18, 2008, 2:49:01 AM8/18/08
to
Hi,
I have another problem with CreateProcess(). Following code is working fine
when launched from an application (.exe). However, when I am calling this
from a running service, its not showing anything but function CreateProcess
is returning non-zero value.

STARTUPINFO si;
PROCESS_INFORMATION pi;
BOOL bRes = false;
DWORD dwCode = 0;
ZeroMemory ( &si, sizeof ( STARTUPINFO));
si.cb = sizeof ( STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;

bRes = CreateProcess( NULL,
(char *)it->second.c_str(), // calc.exe
NULL,
NULL,
TRUE, // also tried FALSE
NORMAL_PRIORITY_CLASS, //also tried 0
NULL,
NULL,
&si,
&pi
);
DWORD error = GetLastError();
XERROR1( "ExecuteFailoverCommand :after execution Error=%d", error);
if( bRes == false )
{
XERROR3( "ExecuteFailoverCommand :Error=%d, Could not invoke process %s
for service %s", error, it->second.c_str(), ServiceName.c_str());
}
else
{
XTRACE2("ExecuteFailoverCommand : executed command %s on failure of %s
returned %d", it->second.c_str(), ServiceName.c_str(),bRes);
return true;
}

Please tell me if this is not the right place to ask this question.

Regards,
Arun

laksh2204

unread,
Aug 18, 2008, 2:56:01 AM8/18/08
to
Sorry forgot mention, I am using Windows Server2003 SP2.

David Ching

unread,
Aug 18, 2008, 12:58:27 PM8/18/08
to
"laksh2204" <laks...@discussions.microsoft.com> wrote in message
news:C5E00267-AFF1-428E...@microsoft.com...

> Hi,
> I have another problem with CreateProcess(). Following code is working
> fine
> when launched from an application (.exe). However, when I am calling this
> from a running service, its not showing anything but function
> CreateProcess
> is returning non-zero value.

A service is running on a different desktop than what the logged in user is
seeing, so any process started by the service will have it's UI being put
onto a different desktop. One way around this is to run another process on
the user's desktop which communicates with the service. If the service
needs to run an app, it sends this process a message and the process
launches the app on the user's desktop, on behalf of the server.

-- David


laksh2204

unread,
Aug 20, 2008, 8:03:01 AM8/20/08
to

> A service is running on a different desktop than what the logged in user is
> seeing, so any process started by the service will have it's UI being put
> onto a different desktop. One way around this is to run another process on
> the user's desktop which communicates with the service. If the service
> needs to run an app, it sends this process a message and the process
> launches the app on the user's desktop, on behalf of the server.

Thanks David,
But, What if I dont want to see any UI, or should following call generate
the file "macaddr.txt" or not.
CreateProcess(NULL,"cmd.exe /C getmac /FO LIST >
macaddr.txt",NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi);

P.S. I am new guy to IPC's and threading.

David Ching

unread,
Aug 21, 2008, 12:04:47 AM8/21/08
to
"laksh2204" <laks...@discussions.microsoft.com> wrote in message
news:3D103B9F-C904-4E16...@microsoft.com...

>
> Thanks David,
> But, What if I dont want to see any UI, or should following call generate
> the file "macaddr.txt" or not.
> CreateProcess(NULL,"cmd.exe /C getmac /FO LIST >
> macaddr.txt",NULL,NULL,TRUE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi);
>


Yes, I believe the file should be generated. Try it and see?

-- David


0 new messages