0

Possible Duplicates:
CreateProcess to run as administrator
How can I run a child process that requires elevation and wait?

How can I modify my current code here to make it run the CreateProcess as administrator?

  SECURITY_ATTRIBUTES secattr; 
  ZeroMemory(&secattr,sizeof(secattr));
  secattr.nLength = sizeof(secattr);
  secattr.bInheritHandle = TRUE;
  HANDLE rPipe, wPipe;

  //Create pipes to write and read data
  CreatePipe(&rPipe,&wPipe,&secattr,0);  
  STARTUPINFO sInfo; 
  ZeroMemory(&sInfo,sizeof(sInfo));
  PROCESS_INFORMATION pInfo; 
  ZeroMemory(&pInfo,sizeof(pInfo));
  sInfo.cb=sizeof(sInfo);
  sInfo.dwFlags=STARTF_USESTDHANDLES;
  sInfo.hStdInput=NULL; 
  sInfo.hStdOutput=wPipe; 
  sInfo.hStdError=wPipe;
  sInfo.wShowWindow;

  CString parameterB = _T(" -format \"%h\" ") + pictureName;  
  CString parameterA = mycustompath + _T("identify.exe");

  if (CreateProcess(parameterA.GetBuffer(), parameterB.GetBuffer(),0,0,TRUE,
          NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo))
          {
            WaitForSingleObject (pInfo.hProcess, INFINITE);
          }

  CloseHandle(wPipe);

I am using Visual Studio 2008, if it matters.

Community
  • 1
  • 1
karikari
  • 6,129
  • 14
  • 57
  • 75
  • Although I'd say the actual solution you're looking for is here: [How can I run a child process that requires elevation and wait?](http://stackoverflow.com/questions/4893262/how-can-i-run-a-child-process-that-requires-elevation-and-wait) Just skip the part about waiting. – Cody Gray Jul 14 '11 at 07:49
  • is it the same if i change createprocess to shellexecute? – karikari Jul 14 '11 at 07:56
  • The same was what? `ShellExecuteEx` is an alternative way of starting a process, but it works a little bit differently from `CreateProcess`. I provided an example of its usage in an answer to the linked question. Does that not work for you? – Cody Gray Jul 14 '11 at 07:57
  • @Mike: Maybe, except that you don't actually want to run the process as "Administrator". You simply want to run it with *administrative privileges*, also known as "elevation" in the jargon of UAC. You can't just guess that there's an administrative account named "Administrator" on the computer. That's one of those assumptions that's true in 75% of cases, but not the other 15%. As Kate's answer (and the bottom of mine) indicates, the *correct* solution is including a manifest with the application/process you want to launch that indicates it requires elevation. – Cody Gray Jul 14 '11 at 08:49
  • 1
    @Cody Gray: No need to guess. Use SID `S-1-5-21-{domain}-500`. – MSalters Jul 14 '11 at 08:52
  • While I understand that for many cases ShellExecute is better there are some use cases (e.g. if you need the DEBUG_PROCESS flag) where it just is not able to replace createprocess so it is imo not a great idea to allways assume that it can be used as replacement. – Syberdoor Sep 04 '19 at 10:43

0 Answers0