// example code how to call 12-RunAsStdUser in C/C++ // start program non-elevated ExecuteNonElevated(NULL, szProgram, szParams, FALSE); // start several programs non-elevated, keep 12-RunAsStdUser running ExecuteNonElevated(NULL, szProgram1, szParams, TRUE); // keep running ExecuteNonElevated(NULL, szProgram2, szParams, TRUE); // keep running ... ExecuteNonElevated(NULL, szProgramLast, szParams, FALSE); // stop 12-RunAsStdUser // just stop 12-RunAsStdUser ExecuteNonElevated(NULL, NULL, NULL, FALSE); // -------------------------------------------------- // szSourcePath - the path where 12runas.exe and 12srvc.exe have been unzipped to // szStartPath - path to your program that you want to start non-elevated // szParams - command line parameters for your program // bKeepRun - TRUE: don't stop, faster if you are going to start more than one program void ExecuteNonElevated(LPCTSTR szSourcePath, LPCTSTR szStartPath, LPCTSTR szParams, BOOL bKeepRun) { char szCompleteParams[2 * MAX_STRING] = {0}; char szRunAsPath[2 * MAX_STRING] = {0}; lstrcpy(szCompleteParams, ""); if( bKeepRun ) lstrcat(szCompleteParams, "/keeprunas "); // don't stop and delete the service if( szStartPath && *szStartPath ) { lstrcat(szCompleteParams, "\""); // path in double-quotes lstrcat(szCompleteParams, szStartPath); lstrcat(szCompleteParams, "\""); } if( szParams && *szParams ) { lstrcat(szCompleteParams, " "); lstrcat(szCompleteParams, szParams); } // just stop? if( 0 == lstrlen(szCompleteParams) ) lstrcat(szCompleteParams, "/stoprunas"); // without params it shows a dialog box // path to 12runas.exe if( szSourcePath && *szSourcePath ) { // the temp path, where everything was unzipped lstrcpy(szRunAsPath, szSourcePath); } else { // try the path of this module (setup.exe) char *pSlash = NULL; GetModuleFileName(NULL, szRunAsPath, MAX_PATH); pSlash = strrchr(szRunAsPath, '\\'); if( pSlash ) *pSlash = 0; // cut last slash } AddEndSlash(szRunAsPath); lstrcat(szRunAsPath, "12runas.exe"); // start 12-RunAsStdUser ShellExecute(NULL, NULL, szRunAsPath, szCompleteParams, NULL, SW_SHOWNORMAL); } // -------------------------------------------------- void AddEndSlash(LPTSTR szPath) { // is last char a backslash? otherwise add one DWORD dwStrLen = 0; dwStrLen = lstrlen(szPath); if( dwStrLen > 0 ) if( '\\' != szPath[dwStrLen - 1] ) lstrcat(szPath, "\\"); }