Clinton112299
asked on
CreateProcess Problem
Hello,
I just received a call from a user stating that a program which launches a Dos program isn't working correctly. The code launches a Batch file which then opens the Dos program. The Dos program uses the com ports and this is where I'm having problems. The program opens fine but has communication problems. I know that it's the CreateProcess code but what part. I'm looking for suggestion on this. I 'm not 100% sure on CREATE_NEW_CONSOLE, ? Is this correct. If I go into Windows Explorer and open the Dos program I have no problems this way.
Thanks for any suggestions
Clinton
void __fastcall TFormMain::ButtonCBAToolCl ick(TObjec t *Sender)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&pi, 0, sizeof(pi));
memset (&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if ( !CreateProcess (
NULL,
"command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO L\\CBATOOL .BAT\"",
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi))
{
MessageBox(0, "Could not execte program \n Please contact software author.", "Error", MB_ICONSTOP);
}
{
CloseHandle(&pi.hProcess);
CloseHandle(&pi.hThread);
}
}
I just received a call from a user stating that a program which launches a Dos program isn't working correctly. The code launches a Batch file which then opens the Dos program. The Dos program uses the com ports and this is where I'm having problems. The program opens fine but has communication problems. I know that it's the CreateProcess code but what part. I'm looking for suggestion on this. I 'm not 100% sure on CREATE_NEW_CONSOLE, ? Is this correct. If I go into Windows Explorer and open the Dos program I have no problems this way.
Thanks for any suggestions
Clinton
void __fastcall TFormMain::ButtonCBAToolCl
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&pi, 0, sizeof(pi));
memset (&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if ( !CreateProcess (
NULL,
"command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi))
{
MessageBox(0, "Could not execte program \n Please contact software author.", "Error", MB_ICONSTOP);
}
{
CloseHandle(&pi.hProcess);
CloseHandle(&pi.hThread);
}
}
ASKER
Nietod,
Why can I go into Windows Explorer and run the Dos program properly. It's just when I lanch the DOs program with Createprocess that it doesn't work. So it has to be the way createprocess is opening the batch file, inturn opeing the Dos program.
Clinton
Why can I go into Windows Explorer and run the Dos program properly. It's just when I lanch the DOs program with Createprocess that it doesn't work. So it has to be the way createprocess is opening the batch file, inturn opeing the Dos program.
Clinton
I have done this before and never had a problem. Try running it without the CREATE_NEW_CONSOLE flag. Also, try calling GetStartupInfo(...) instead of setting it up yourself. That will pull the same setting used to run the current process. For example:
STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
DWORD result=1;
ZeroMemory(&process_inform ation,size of(process _informati on));
GetStartupInfo(&startup_in fo);
if(CreateProcess(
NULL,
command,
NULL,
NULL,
FALSE,
(DWORD)NULL, //try NULL instead
NULL,
NULL,
&startup_info,
&process_information))
{
CloseHandle(process_inform ation.hPro cess);
CloseHandle(process_inform ation.hThr ead);
}
else
{
MessageBox(NULL,"Could not execte program\n Please contact software author.","Error",MB_ICONST OP|MB_OK);
}
STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
DWORD result=1;
ZeroMemory(&process_inform
GetStartupInfo(&startup_in
if(CreateProcess(
NULL,
command,
NULL,
NULL,
FALSE,
(DWORD)NULL, //try NULL instead
NULL,
NULL,
&startup_info,
&process_information))
{
CloseHandle(process_inform
CloseHandle(process_inform
}
else
{
MessageBox(NULL,"Could not execte program\n Please contact software author.","Error",MB_ICONST
}
Also, where is this program being launched from? Perhaps it is being run under a user context that doesn't have access to the COM ports. In that case, CreateProcessAsUser(...) would be the next thing to tackle. I have source for that if you wish.
ASKER
Brain2000 "great name"
So where you have command, shouldn't I have "command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO L\\CBATOOL .BAT\"", ? And what does (DWORD)NULL, do?
Thanks
Clinton
PS can you also post the alluser code.
So where you have command, shouldn't I have "command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO
Thanks
Clinton
PS can you also post the alluser code.
Clinton, does the progrm that calls CreateProcess() open the com ports at all?
ASKER
No, createprocess only calls a batch file which then opens the Dos program. This is in the batch file:
@echo off
cls
C:
CD "\Hi-Tech Software\JCM\CBA\CBATOOL"
CBATOOL.exe
I can go into Windows Explorer and from the batch file run the program. It also works if I open the program direct from Windows Explorer.
So my only thought was that it had to be how I was opening the batch file.
Thanks
Clinton
@echo off
cls
C:
CD "\Hi-Tech Software\JCM\CBA\CBATOOL"
CBATOOL.exe
I can go into Windows Explorer and from the batch file run the program. It also works if I open the program direct from Windows Explorer.
So my only thought was that it had to be how I was opening the batch file.
Thanks
Clinton
If everyhing works from explorer why not use ShellExecuteEx() or ShellExecute()?
If everyhing works from explorer why not use ShellExecuteEx() or ShellExecute()?
All neccessary preparation for ports using will be done by explorer process.
All neccessary preparation for ports using will be done by explorer process.
ShellExecute() would work only if he had a file to be "used" and it was associated with the EXE he wanted to run. This EXE might not have data files, or ones that are unique to it.
>> All neccessary preparation for ports using
>> will be done by explorer process
What sort of preparation is needed?
>> All neccessary preparation for ports using
>> will be done by explorer process
What sort of preparation is needed?
ASKER
Well I was trying to use ShellExecute and people (programmers) told me to use CreateProcess as this opens ShellExecute. So this is the way I went. If I could get a better understanding of the whole CreateProcess theory, maybe I'm missing something that is causing my problem. Just a thought.
Clinton
Clinton
>So where you have command, shouldn't I have "command.com /c "C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO L\\CBATOOL .BAT\"", ? And what does (DWORD)NULL, do?
Yes, I implied that. The (DWORD)NULL is telling it not to use any optional Creation Flags.
Here is the CreateProcessAsUser() function utilized:
HANDLE mytoken;
STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
if(!LogonUser("Administrat or","","pa ssword",LO GON32_LOGO N_INTERACT IVE,LOGON3 2_PROVIDER _DEFAULT,& mytoken)) {
MessageBox(NULL,"Couldn't LogonUser();","ERROR",MB_O K);
goto error;
}
ZeroMemory(&process_inform ation,size of(process _informati on));
GetStartupInfo(&startup_in fo);
startup_info.cb=sizeof(STA RTUPINFO);
if(!CreateProcessAsUser(my token,comm and,comman d,NULL,NUL L,FALSE,(D WORD)NULL, NULL,NULL, &startup_i nfo,&proce ss_informa tion)) {
MessageBox(NULL,"couldn't CreateProcessAsUser();","E RROR",MB_O K);
goto error;
}
CloseHandle(process_inform ation.hPro cess;
CloseHandle(process_inform ation.hThr ead);
CloseHandle(mytoken);
Yes, I implied that. The (DWORD)NULL is telling it not to use any optional Creation Flags.
Here is the CreateProcessAsUser() function utilized:
HANDLE mytoken;
STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
if(!LogonUser("Administrat
MessageBox(NULL,"Couldn't LogonUser();","ERROR",MB_O
goto error;
}
ZeroMemory(&process_inform
GetStartupInfo(&startup_in
startup_info.cb=sizeof(STA
if(!CreateProcessAsUser(my
MessageBox(NULL,"couldn't CreateProcessAsUser();","E
goto error;
}
CloseHandle(process_inform
CloseHandle(process_inform
CloseHandle(mytoken);
ASKER
Brain 2000,
Forgive the dump questions, but how does STARTUPINFO startup_info; know where the Dos.exe is? I have 10 Dos programs in the same folder. So with this do I have to define the path. Also with using your method, do I still have to call the batch file.
Thanks
Clinton
Forgive the dump questions, but how does STARTUPINFO startup_info; know where the Dos.exe is? I have 10 Dos programs in the same folder. So with this do I have to define the path. Also with using your method, do I still have to call the batch file.
Thanks
Clinton
It doesn't STARTYUPINFO doesn' t store a path or name of the EXE. That is passed to CreateProccess().
The path is defined like this:
CreateProcessAsUser(mytoke n,filename ,filename, NULL,NULL, FALSE,(DWO RD)NULL,NU LL,path,&s tartup_inf o,&process _informati on);
I was just setting it as a NULL, but you can make it the path if you want. Then filename is your file that you want to execute. STARTUPINFO just gives information on starting the program. For example, I can set STARTUPINFO so that it runs the process on an entirely different desktop.
CreateProcessAsUser(mytoke
I was just setting it as a NULL, but you can make it the path if you want. Then filename is your file that you want to execute. STARTUPINFO just gives information on starting the program. For example, I can set STARTUPINFO so that it runs the process on an entirely different desktop.
Well I was trying to use ShellExecute and people (programmers) told me to use CreateProcess as this opens ShellExecute. So this is the way I went. If I could get a better understanding of the whole CreateProcess theory, maybe I'm missing something that is causing my problem. Just a thought.
========================== ====
No
ShellExecute() will open CreateProcess()
Try ShellExecute()
==========================
No
ShellExecute() will open CreateProcess()
Try ShellExecute()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes that's correct I know the exact path for the DOs programs. As for SHellExecute, I thought this was opened by CreateProcess? Anyways, all the programs I'm opeing are 16 bit (DOS) and not 32bit applications. Isn't ShellExcute 32 bit. And all Dos programs use the Com port for serial communication.
Maybe I have to many doors opened and don't know which one to exit from. I think that CreateProcess is the way to do this along with a Dos Batch file. I don't fully understand what evey option does in CreateProcess and just have to fine tune it. I have 10 Dos program which open the same way. Out of the 10, 9 work fine "to date". I think that once I figure out the problem with the last one, I'll feel conident enought to push forward. If someone could Please take the time and go over my CreateProcess and check it out to make sure that this is done correct, "Make suggestion and what they would change and why"
void __fastcall TFormMain::ButtonCBAToolCl ick(TObjec t *Sender)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&pi, 0, sizeof(pi));
memset (&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if ( !CreateProcess (
NULL,
"command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO L\\CBATOOL .BAT\"",
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi))
{
MessageBox(0, "Could not execte program \n Please contact software author.", "Error", MB_ICONSTOP);
}
{
CloseHandle(&pi.hProcess);
CloseHandle(&pi.hThread);
}
}
Thanks
Clinton
Maybe I have to many doors opened and don't know which one to exit from. I think that CreateProcess is the way to do this along with a Dos Batch file. I don't fully understand what evey option does in CreateProcess and just have to fine tune it. I have 10 Dos program which open the same way. Out of the 10, 9 work fine "to date". I think that once I figure out the problem with the last one, I'll feel conident enought to push forward. If someone could Please take the time and go over my CreateProcess and check it out to make sure that this is done correct, "Make suggestion and what they would change and why"
void __fastcall TFormMain::ButtonCBAToolCl
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset (&pi, 0, sizeof(pi));
memset (&si, 0, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOWNORMAL;
if ( !CreateProcess (
NULL,
"command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOO
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi))
{
MessageBox(0, "Could not execte program \n Please contact software author.", "Error", MB_ICONSTOP);
}
{
CloseHandle(&pi.hProcess);
CloseHandle(&pi.hThread);
}
}
Thanks
Clinton
ASKER
nietod,
Thanks agian for your help. What I ended up doing is placing some batch code that will check the com port before the Dos program is opened. If the com port is in use the program doesn't open. If it is not in used, then the custom has a hardware problem.
Clinton
Thanks agian for your help. What I ended up doing is placing some batch code that will check the com port before the Dos program is opened. If the com port is in use the program doesn't open. If it is not in used, then the custom has a hardware problem.
Clinton
>> what part. I'm looking for suggestion on this.
If the DOs program is running, but having probelems why would you think it has anything to do with CreateProccess()?
The only thing I can think of related to this is that you might want to specify the CREATE_SEPERATE_WOW_VDM flag. But honestly I don't think it has anything to do with createprocess.