konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

          如一個程序要有以下的命令來運行
          runas /env /savecred /user:hhql "c:\qlnetbar\bc2\bc2"
          我現(xiàn)在的問題是如何在Delphi中用代碼來代替 runas /env /savecred /user:hhql 的功能,因為我要監(jiān)視 c:\qlnetbar\bc2\bc2 的運行情況,所以 c:\qlnetbar\bc2\bc2 必須要由我用Delphi寫的程序來運行

          請高手指教。。。。


          果你用XP或2000,可以用下面的API:CreateProcessWithLogonW
          type
            _STARTUPINFOW = record
              cb: DWORD;
              lpReserved: LPWSTR;
              lpDesktop: LPWSTR;
              lpTitle: LPWSTR;
              dwX: DWORD;
              dwY: DWORD;
              dwXSize: DWORD;
              dwYSize: DWORD;
              dwXCountChars: DWORD;
              dwYCountChars: DWORD;
              dwFillAttribute: DWORD;
              dwFlags: DWORD;
              wShowWindow: Word;
              cbReserved2: Word;
              lpReserved2: PByte;
              hStdInput: THandle;
              hStdOutput: THandle;
              hStdError: THandle;
            end;
            STARTUPINFOW = _STARTUPINFOW;

          function CreateProcessWithLogonW(lpUserName, lpDomain, lpPassword: LPCWSTR;
            dwLogonFlags: DWORD; lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
            dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPCWSTR;
            const lpStartupInfo: STARTUPINFOW; var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall;
            external advapi32 Name 'CreateProcessWithLogonW'

          procedure TForm1.Button2Click(Sender: TObject);
          var
            STARTUPINFO: StartupInfoW;
            ProcessInfo: TProcessInformation;
            AUser, ADomain, APass, AExe: WideString;
          const
            LOGON_WITH_PROFILE = $00000001;
            LOGON_NETCREDENTIALS_ONLY = $00000002;
          begin
            FillChar(STARTUPINFO, SizeOf(StartupInfoW), #0);
            STARTUPINFO.cb := SizeOf(StartupInfoW);
            STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
            STARTUPINFO.wShowWindow := SW_SHOW;
            AUser := edtUser.Text;
            ADomain := edtDomain.Text;
            APass := edtPass.Text;
            AExe := edtExe.Text;
            if not CreateProcessWithLogonW(PWideChar(AUser), PWideChar(ADomain),
              PWideChar(APass),
              LOGON_WITH_PROFILE, nil, PWideChar(AExe),
              NORMAL_PRIORITY_CLASS, nil, nil, STARTUPINFO, ProcessInfo) then
              RaiseLastOSError;
          end;

          已經(jīng)測試通過

          代碼修改了一下:

          unit Unit1;

          interface

          uses
            Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
            Dialogs, StdCtrls;

          type
            TForm1 = class(TForm)
              Button1: TButton;
              procedure Button1Click(Sender: TObject);
            private
              { Private declarations }
            public
              { Public declarations }
            end;

          type
            _STARTUPINFOW = record
              cb: DWORD;
              lpReserved: LPWSTR;
              lpDesktop: LPWSTR;
              lpTitle: LPWSTR;
              dwX: DWORD;
              dwY: DWORD;
              dwXSize: DWORD;
              dwYSize: DWORD;
              dwXCountChars: DWORD;
              dwYCountChars: DWORD;
              dwFillAttribute: DWORD;
              dwFlags: DWORD;
              wShowWindow: Word;
              cbReserved2: Word;
              lpReserved2: PByte;
              hStdInput: THandle;
              hStdOutput: THandle;
              hStdError: THandle;
            end;
            STARTUPINFOW = _STARTUPINFOW;

          function CreateProcessWithLogonW(lpUserName, lpDomain, lpPassword: LPCWSTR;
            dwLogonFlags: DWORD; lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
            dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPCWSTR;
            const lpStartupInfo: STARTUPINFOW; var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall;
            external advapi32 Name 'CreateProcessWithLogonW'
          var
            Form1: TForm1;

          implementation

          {$R *.dfm}

          procedure TForm1.Button1Click(Sender: TObject);
          var
            STARTUPINFO: StartupInfoW;
            ProcessInfo: TProcessInformation;
            AUser, ADomain, APass, AExe: WideString;
          const
            LOGON_WITH_PROFILE = $00000001;
            LOGON_NETCREDENTIALS_ONLY = $00000002;
          begin
            FillChar(STARTUPINFO, SizeOf(StartupInfoW), #0);
            STARTUPINFO.cb := SizeOf(StartupInfoW);
            STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
            STARTUPINFO.wShowWindow := SW_SHOW;
            AUser := 'pcmax';
            //ADomain := edtDomain.Text;
            APass := 'pcmax';
            AExe := 'c:\windows\system32\mspaint.exe';
            if not CreateProcessWithLogonW(PWideChar(AUser), PWideChar(ADomain),
              PWideChar(APass),
              LOGON_WITH_PROFILE, nil, PWideChar(AExe),
              NORMAL_PRIORITY_CLASS, nil, nil, STARTUPINFO, ProcessInfo) then
              RaiseLastOSError;
            WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
            ShowMessage('over now');
          end;

          end.

          運行上面的代碼,點擊button1就會以用戶pcmax運行 c:\windows\system32\mspaint.exe。然后等待運行結(jié)束后彈出提示對話框。

           

          posted on 2005-11-14 19:15 konhon 優(yōu)華 閱讀(1330) 評論(1)  編輯  收藏 所屬分類: Delphi

          Feedback

          # re: Delphi 中如何用另外一個用戶的身份來運行一人程序 2008-05-07 07:41 horizon
          不錯,謝謝  回復(fù)  更多評論
            

          主站蜘蛛池模板: 岑巩县| 遂平县| 双鸭山市| 泸溪县| 桂阳县| 新源县| 乐亭县| 上林县| 台江县| 蒙自县| 安义县| 隆林| 四会市| 牡丹江市| 昌江| 安溪县| 紫阳县| 介休市| 黔西县| 健康| 烟台市| 都江堰市| 体育| 建始县| 河池市| 申扎县| 定日县| 彰化县| 潞城市| 铅山县| 威信县| 桐梓县| 达州市| 阳西县| 赣州市| 虹口区| 宾川县| 云梦县| 通榆县| 易门县| 桂阳县|