Can anyone tell me how to create a process in VC++? I need to execute
regasm.exe testdll /tlb:test.tlb /codebase
command in that process.
regasm.exe
(Assembly Registration Tool) makes changes to the Windows Registry, so if you want to start regasm.exe
as elevated process you could use the following code:
#include "stdafx.h"
#include "windows.h"
#include "shellapi.h"
int _tmain(int argc, _TCHAR* argv[])
{
SHELLEXECUTEINFO shExecInfo;
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = NULL;
shExecInfo.hwnd = NULL;
shExecInfo.lpVerb = L"runas";
shExecInfo.lpFile = L"regasm.exe";
shExecInfo.lpParameters = L"testdll /tlb:test.tlb /codebase";
shExecInfo.lpDirectory = NULL;
shExecInfo.nShow = SW_NORMAL;
shExecInfo.hInstApp = NULL;
ShellExecuteEx(&shExecInfo);
return 0;
}
shExecInfo.lpVerb = L"runas"
means that process will be started with elevated privileges. If you don't want that just set shExecInfo.lpVerb
to NULL. But under Vista or Windows 7 it's required administrator rights to change some parts of Windows Registry.
You need to read up on CreateProcess() in msdn. There's sample code on that page.
If you just want to execute a synchronous command (run and wait), your best bet is to just use the system()
call (see here) to run it. Yes, I know it's a Linux page but C is a standard, no? :-)
For more fine-grained control of what gets run, how it runs (sync/async) and lots more options, CreateProcess()
(see here), and its brethren, are probably better, though you'll be tied to the Windows platform (which may not be of immediate concern to you).
Use CreateProcess() to spawn the process, check the return value to ensure that it started okay, then either close the handles to the process and the thread or use WaitForSingleObject() to wait until it finishes and then close handles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With