Link to home
Start Free TrialLog in
Avatar of Clinton112299
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::ButtonCBAToolClick(TObject *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\\CBATOOL\\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);
    }

}
Avatar of nietod
nietod

>> I know that it's the CreateProcess code but
>> 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.
Avatar of Clinton112299

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
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_information,sizeof(process_information));
GetStartupInfo(&startup_info);
if(CreateProcess(
  NULL,
  command,
  NULL,
  NULL,
  FALSE,
  (DWORD)NULL, //try NULL instead
  NULL,
  NULL,
  &startup_info,
  &process_information))
{
  CloseHandle(process_information.hProcess);
  CloseHandle(process_information.hThread);
}
else
{
  MessageBox(NULL,"Could not execte program\n Please contact software author.","Error",MB_ICONSTOP|MB_OK);
}
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.
Brain2000 "great name"

So where you have command, shouldn't I have "command.com /c \"C:\\Hi-Tech Software\\JCM\\CBA\\CBATOOL\\CBATOOL.BAT\"", ? And what does (DWORD)NULL, do?

Thanks
Clinton

PS can you also post the alluser code.


           
Clinton, does the progrm that calls CreateProcess() open the com ports at all?
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
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.

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?  
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
>So where you have command, shouldn't I have "command.com /c "C:\\Hi-Tech Software\\JCM\\CBA\\CBATOOL\\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("Administrator","","password",LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,&mytoken)) {
    MessageBox(NULL,"Couldn't LogonUser();","ERROR",MB_OK);
    goto error;
  }

  ZeroMemory(&process_information,sizeof(process_information));
  GetStartupInfo(&startup_info);
  startup_info.cb=sizeof(STARTUPINFO);
  if(!CreateProcessAsUser(mytoken,command,command,NULL,NULL,FALSE,(DWORD)NULL,NULL,NULL,&startup_info,&process_information)) {
    MessageBox(NULL,"couldn't CreateProcessAsUser();","ERROR",MB_OK);
    goto error;
  }
  CloseHandle(process_information.hProcess;
  CloseHandle(process_information.hThread);
  CloseHandle(mytoken);
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
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(mytoken,filename,filename,NULL,NULL,FALSE,(DWORD)NULL,NULL,path,&startup_info,&process_information);

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()
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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::ButtonCBAToolClick(TObject *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\\CBATOOL\\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
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