CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2005
    Posts
    173

    Problem with CreateProcess

    Hi All
    I am having a problem in my application, what I am doing is:
    Calling a function SaveKeys(TCHAR *) within a loop and this function save the specified registry key to a .reg file,
    for this using CreateProcess( ) for calling regedit.exe from command line and WaitForSingleObject( ) function.
    code looks like this
    Code:
    for (int i = 0; i < 10; i++)
    {
        // some code here
       SaveKeys(cRegistryKey );  // for specified Registry Key
    }
    
    void MyClass::SaveKeys(TCHAR *cRegistryKey)
    {
            STARTUPINFO			StartupInfo;
    	PROCESS_INFORMATION ProcessInformation;
      
            CreateProcess(NULL, cCommand,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|CREATE_SUSPENDED,NULL, NULL, &StartupInfo, &ProcessInformation);
    WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
    
    }
    //e.g. Here cCommand  = regedit /e C:\File.reg HKEY_CURRENT_USER\MyKey
    Now what is my problem is that here WaitForSingleObject( ) function is not working. and only a single reg file is created.
    and when use Sleep(1000) then all the 10 reg file are created.

    So please tell me that how should I overcome this problem.

    Please reply
    Thanks

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Problem with CreateProcess

    You are passing CREATE_SUSPENDED flag, but not calling ResumeThread(ProcessInformation.hThread).

    You should pass 0 as flag if you want it to start immediately.
    Code:
    void MyClass::SaveKeys(TCHAR *cRegistryKey)
    {
            STARTUPINFO			StartupInfo;
    	PROCESS_INFORMATION ProcessInformation;
      
            CreateProcess(NULL, cCommand,NULL,NULL,FALSE,0,NULL, NULL, &StartupInfo, &ProcessInformation);
    WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
    
    }
    or call ResumeThread(ProcessInformation.hThread) ; after CreateProcess.
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Problem with CreateProcess

    Quote Originally Posted by hash123
    Code:
    for (int i = 0; i < 10; i++)
    {
        // some code here
       SaveKeys(cRegistryKey );  // for specified Registry Key
    }
    
    void MyClass::SaveKeys(TCHAR *cRegistryKey)
    {
            STARTUPINFO			StartupInfo;
    	PROCESS_INFORMATION ProcessInformation;
      
            CreateProcess(NULL, cCommand,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|CREATE_SUSPENDED,NULL, NULL, &StartupInfo, &ProcessInformation);
    WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
    
    }
    //e.g. Here cCommand  = regedit /e C:\File.reg HKEY_CURRENT_USER\MyKey
    In addition to what Krishnaa says, I would like to add the following:
    1. You are not checking for the return value of CreateProcess.
    2. How is cCommand formulated inside SaveKeys? It isn't the input.
    Due to point # 2, are you sure that you aren't calling CreateProcess in a way that it invokes regedit.exe with the same export file name parameter C:\File.reg (thus overwriting the previous file created) - check file time stamps while single-stepping.

  4. #4
    Join Date
    May 2006
    Posts
    327

    Re: Problem with CreateProcess

    One additional thing: unprepared STARTUPINFO structure. It can be fixed like this:

    Code:
    STARTUPINFO StartupInfo = { sizeof(STARTUPINFO) };

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Problem with CreateProcess

    [ Redirected thread ]

    Avoid from start this kind of headache, by directly using RegSaveKey Windows APi function instead of launching regedit.exe.
    Last edited by ovidiucucu; July 3rd, 2006 at 07:28 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Sep 2005
    Posts
    173

    Re: Problem with CreateProcess

    thanks u all
    I tried flag o too but didnt work
    As stated createprocess is working right as it saves .reg file as desired with Sleep(1000) function but when I dont use Sleep function then out of 10 onle 2-3 reg files are saved.
    that means WaitForSingleObject( ) function is not working.

    So please tell that how to save all the reg files without using Sleep( ).

    Thanks

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Problem with CreateProcess

    The best would be to go for RegSavekey as pointed by Ovidiu.
    Regards,
    Ramkrishna Pawar

  8. #8
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: Problem with CreateProcess

    Both STARTUPINFO and PROCESS_INFORMATION structs are uninitialized. I think one of them also has a cbSize value which must be set to its size.
    Please don't forget to rate users who helped you!

  9. #9
    Join Date
    May 2006
    Posts
    327

    Re: Problem with CreateProcess

    I suppose the invoked regedit tools cannot work in multi-instance mode. For example, I cannot start more then one regedit application from Windows menu or command line. That's why only some of your calls work.

    This probably means that you cannot do multi-threaded export using regedit tool.
    Last edited by Viorel; July 3rd, 2006 at 07:42 AM.

  10. #10
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Problem with CreateProcess

    Quote Originally Posted by Viorel
    I suppose the invoked regedit tools cannot work in multi-instance mode. For example, I cannot start more then one regedit application from Windows menu or command line. That's why only some of your calls work.

    This probably means that you cannot do multi-threaded export using regedit tool.
    Totally agree !
    Regards,
    Ramkrishna Pawar

  11. #11
    Join Date
    Sep 2005
    Posts
    173

    Re: Problem with CreateProcess

    Hi all
    Please have a look at the complete code given below:
    Code:
    void CDisplayErrors::BackupRegistry()
    {
    	STARTUPINFO			StartupInfo;
    	PROCESS_INFORMATION ProcessInformation;
    
    	memset(&StartupInfo, 0x00, sizeof(STARTUPINFO));
    	StartupInfo.cb = sizeof(STARTUPINFO);
    
    	memset(&ProcessInformation, 0x00, sizeof(PROCESS_INFORMATION));
    
    
    	if(!::CreateProcess(NULL, cCommand,NULL,NULL,FALSE,CREATE_DEFAULT_ERROR_MODE|CREATE_SUSPENDED,NULL, NULL, &StartupInfo, &ProcessInformation))
    		AfxMessageBox(_T("Process cud not b created!"));
    
    	else
    	{
    		CString cc	=	"Process Created successfully!   ";
    		obj.WriteString(cc);
    
    		m_hImageProcess = ProcessInformation.hProcess;
    		::ResumeThread(ProcessInformation.hThread);
    	}
    
    	DWORD bc	=	WaitForSingleObject( ProcessInformation.hProcess, INFINITE );
    
        if(bc==WAIT_FAILED)
    	{
    		CString cc	=	"WaitForSingleObject has failed   ";
    
    		obj.WriteString(cc);
    
    	}
    	switch (bc)
    	{
    		case WAIT_OBJECT_0: 
    			{ 
    				CString cc	=	"WAIT_OBJECT_0   ";
    
    				obj.WriteString(cc);
    
    
    				BOOL retval = ::GetExitCodeProcess(m_hImageProcess, &ExitCode);
    				DWORD dd = GetLastError();
    
    
    				BOOL r1 = ::CloseHandle(ProcessInformation.hProcess);
    				BOOL r2 = ::CloseHandle(ProcessInformation.hThread);
    
    
    
    				dd = GetLastError();
    
    				if(r1 && r2)	
    				{
    					CString cc	=	"Process Terminated successfully!   ";
    					obj.WriteString(cc);
    				}
    
    
    
    				break;
    			}
    		case WAIT_TIMEOUT:
    			{
    				CString cc	=	"WAIT_TIMEOUT   ";
    
    				obj.WriteString(cc);
    
    				break;
    			}
                
    
    		case WAIT_ABANDONED: 
    			{
    				CString cc	=	"WAIT_ABANDONED   ";
    
    				obj.WriteString(cc);
    
    				break;
    			}
    	}
    
    
    }
    Summary:

    The function BackupRegistry() is called four times from a different function.

    The cCommand(2nd argument) in the CreateProcess function contains value something like this:

    regedit /e c:\123xx.reg HKEY_LOCAL_MACHINE\Software\MyKeyxx

    As a result 4 seperate .reg file must be created with the exported keys as their data.

    BUT only 1 .reg file gets created instead of 4.

    I have the scanned the code in the BackupRegistry() function but found no bug.

    Please suggest me where am i getting wrong?


    One more thing if i add Sleep() to the code 4 .reg files are created successfully.


    Waiting for suggestions

    Regards

  12. #12
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Problem with CreateProcess

    Why can you not use easy and simple RegSaveKey function instead of CreateProcess.
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured