-
July 3rd, 2006, 07:08 AM
#1
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
-
July 3rd, 2006, 07:18 AM
#2
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
-
July 3rd, 2006, 07:19 AM
#3
Re: Problem with CreateProcess
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:- You are not checking for the return value of CreateProcess.
- 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.
-
July 3rd, 2006, 07:22 AM
#4
Re: Problem with CreateProcess
One additional thing: unprepared STARTUPINFO structure. It can be fixed like this:
Code:
STARTUPINFO StartupInfo = { sizeof(STARTUPINFO) };
-
July 3rd, 2006, 07:25 AM
#5
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.
-
July 3rd, 2006, 07:29 AM
#6
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
-
July 3rd, 2006, 07:35 AM
#7
Re: Problem with CreateProcess
The best would be to go for RegSavekey as pointed by Ovidiu.
Regards,
Ramkrishna Pawar
-
July 3rd, 2006, 07:37 AM
#8
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!
-
July 3rd, 2006, 07:39 AM
#9
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.
-
July 3rd, 2006, 07:44 AM
#10
Re: Problem with CreateProcess
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
-
July 4th, 2006, 05:53 AM
#11
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
-
July 4th, 2006, 06:52 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|