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);
    }

}
Clinton112299Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
vachoooho, you use ShellExecute when you have a data file you want to open/print and do not know the name or location of an EXE that can open it.  That is not the case here.  Clinton has not mentioned the existance of any data file.  He does know the name and location of the EXE.
0
 
nietodCommented:
>> 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.
0
 
Clinton112299Author Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Brain2000Commented:
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);
}
0
 
Brain2000Commented:
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.
0
 
Clinton112299Author Commented:
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.


           
0
 
nietodCommented:
Clinton, does the progrm that calls CreateProcess() open the com ports at all?
0
 
Clinton112299Author Commented:
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
0
 
vachoohoCommented:
If everyhing works from explorer why not use ShellExecuteEx() or ShellExecute()?


0
 
vachoohoCommented:
If everyhing works from explorer why not use ShellExecuteEx() or ShellExecute()?

All neccessary preparation for ports using will be done by explorer process.

0
 
nietodCommented:
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?  
0
 
Clinton112299Author Commented:
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
0
 
Brain2000Commented:
>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);
0
 
Clinton112299Author Commented:
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
0
 
nietodCommented:
It doesn't   STARTYUPINFO doesn' t store a path or name of the EXE.  That is passed to CreateProccess().
0
 
Brain2000Commented:
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.
0
 
vachoohoCommented:
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()
0
 
Clinton112299Author Commented:
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
0
 
Clinton112299Author Commented:
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
0
All Courses

From novice to tech pro — start learning today.