5

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Cute
  • 13,643
  • 36
  • 96
  • 112
  • 1
    Not a dupe at all. That other question presupposes that CreateProcess is the only way to do it. This one is more open-ended. For the record, the suggested dupe was http://stackoverflow.com/questions/42531/how-do-i-call-createprocess-in-c-to-launch-a-windows-executable - I've taken it out of the question since I consider it bad form to edit the content. I believe that should be put in comments. If it gets closed as a dupe, then SO itself will add that to the question. – paxdiablo Jul 01 '09 at 08:34

4 Answers4

10

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.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • It's Working Fine Rather Than Above.But meanwhile it is asking under which user it has to run .. Can u explain about this prog SHELLEXECUTEINFO shExecInfo; shExecInfo.lpVerb = L"runas"; – Cute Jul 02 '09 at 05:37
  • I have modified l"run as" to null then works without asking. ok what does this L stand for and how the process created with this?? – Cute Jul 02 '09 at 05:41
  • what about complexity in terms of space and time of creating a process in this way?? Any Idea – Cute Jul 02 '09 at 07:54
  • ShellExecute a bit slower than CreateProcess. In fact ShellExecure use CreateProcess in the end. If your code timecritical you should use CreateProcess. – Kirill V. Lyadvinsky Jul 02 '09 at 09:43
4

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

rtn
  • 127,556
  • 20
  • 111
  • 121
4

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

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979