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

Can someone tell me how to create a process in VC ++? I need to execute

regasm.exe testdll /tlb:test.tlb /codebase

in this process.

+5
source share
4 answers

regasm.exe(Assembly registration tool) makes changes to the Windows registry, so if you want to run regasm.exeas an elevated process, you can 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 the process will be run with elevated privileges. If you do not want this to just set shExecInfo.lpVerbto NULL. But on Vista or Windows 7, you must have administrator rights to modify some parts of the Windows registry.

+10
source
+5

( ), system() (. ) . , , Linux, C - , ?: -)

, , (sync/async) , CreateProcess() (. ) , , , Windows ( ).

+4

Use CreateProcess () to start the process, check the return value to make sure it starts normally, either close the process and thread descriptors, or use WaitForSingleObject () to wait until it finishes, and then close the descriptors.

+3
source

All Articles