Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a process in C++ on Windows?

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.

like image 758
Cute Avatar asked Jul 01 '09 07:07

Cute


4 Answers

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.

like image 53
Kirill V. Lyadvinsky Avatar answered Oct 04 '22 03:10

Kirill V. Lyadvinsky


You need to read up on CreateProcess() in msdn. There's sample code on that page.

like image 20
ralphtheninja Avatar answered Oct 04 '22 02:10

ralphtheninja


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).

like image 25
paxdiablo Avatar answered Oct 04 '22 03:10

paxdiablo


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.

like image 34
sharptooth Avatar answered Oct 04 '22 04:10

sharptooth