create_process_as_user

int create_process_as_user(int nArgs, __wchar_t* thisApp)

{

        long lError;

 

        STARTUPINFO si;

        PROCESS_INFORMATION pi;

 

        __wchar_t szUserName[CREDUI_MAX_USERNAME_LENGTH + 1];

        __wchar_t szPassword[CREDUI_MAX_PASSWORD_LENGTH + 1];

        __wchar_t szDomain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1];

        __wchar_t szUser[CREDUI_MAX_USERNAME_LENGTH + 1];

 

        __wchar_t* pszTarget = NULL;

 

        if(!is_windows_xp())

                return FALSE;

 

        szUserName[0] = L'\0';

        szPassword[0] = L'\0';

        szDomain[0] = L'\0';

 

        memset(&si, 0, sizeof(STARTUPINFO));

        memset(&pi, 0, sizeof(PROCESS_INFORMATION));

 

        __try

        {

                pszTarget = computer_name(ComputerNamePhysicalNetBIOS);

 

                _RPT0(_CRT_WARN, "The computer name is ");

                OutputDebugStringW(pszTarget != NULL ? pszTarget : L"");

                _RPT0(_CRT_WARN, "\n");

 

                lError = prompt_for_user_name_and_password(pszTarget, szUserName, CREDUI_MAX_USERNAME_LENGTH, szPassword, CREDUI_MAX_PASSWORD_LENGTH);

                if(lError != NO_ERROR)

                        RaiseException(lError, 0, 0, 0);

 

                if((lError = CredUIParseUserNameW(szUserName,

                                                        szUser,

                                                        CREDUI_MAX_USERNAME_LENGTH + 1,

                                                        szDomain,

                                                        CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1

                                                        )) != NO_ERROR)

                {

                        _RPT1(_CRT_WARN, "CredUIParseUserName failed: Last Error = %ld\n", lError);

                        RaiseException(lError, 0, 0, 0);

                }

 

                GetStartupInfoW(&si);

 

                si.lpDesktop = NULL;

 

                _RPT0(_CRT_WARN, "Calling CreateProcessWithLogonW for application: ");

                OutputDebugStringW(thisApp);

                _RPT1(_CRT_WARN, "\nCommand Line: %s\nUser Name: ", GetCommandLineA());

                OutputDebugStringW(szUser);

                _RPT0(_CRT_WARN, "\nDomain: ");

                OutputDebugStringW(szDomain);

                _RPT0(_CRT_WARN, "\n");

 

                if(!CreateProcessWithLogonW(szUser,

                                                                                szDomain,

                                                                                szPassword,

                                                                                0UL,

                                                                                thisApp,

                                                                                GetCommandLineW(),

                                                                                CREATE_UNICODE_ENVIRONMENT | CREATE_SUSPENDED,

                                                                                NULL,

                                                                                NULL,

                                                                                &si,

                                                                                &pi

                                                                                ))

                        RaiseException(GetLastError(), 0, 0, 0);

 

                close_log_file();

 

                if(log_file != NULL &&

                        _tcsnicmp(log_file, _T("CONOUT$"), 8) != 0)

                        _tchmod(log_file, S_IWRITE | S_IREAD);

                ResumeThread(pi.hThread);

        }

        __finally

        {

                memset(&szUserName, 0, (CREDUI_MAX_USERNAME_LENGTH + 1) * sizeof(__wchar_t));

                memset(&szPassword, 0, (CREDUI_MAX_PASSWORD_LENGTH + 1) * sizeof(__wchar_t));

 

                if(pszTarget != NULL)

                        free(pszTarget);

 

                if(pi.hProcess != (HANDLE)0)

                        CloseHandle(pi.hProcess);

                if(pi.hThread != (HANDLE)0)

                        CloseHandle(pi.hThread);

        }

 

        return TRUE;

}

 

你可能感兴趣的:(create_process_as_user)