S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

create_process_as_user

Posted on 2009-04-03 08:52 S.l.e!ep.¢% 阅读(248) 评论(0)  编辑 收藏 引用 所属分类: VC

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;

}

 


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理