konhon

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

          Google

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

          如一個程序要有以下的命令來運行
          runas /env /savecred /user:hhql "c:\qlnetbar\bc2\bc2"
          我現在的問題是如何在Delphi中用代碼來代替 runas /env /savecred /user:hhql 的功能,因為我要監視 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;

          已經測試通過

          代碼修改了一下:

          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。然后等待運行結束后彈出提示對話框。

           

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

          Feedback

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

          主站蜘蛛池模板: 昭苏县| 裕民县| 咸丰县| 嘉峪关市| 汤阴县| 正安县| 宁河县| 共和县| 彰化市| 西青区| 金昌市| 新密市| 海伦市| 泸水县| 卫辉市| 九龙城区| 建水县| 怀仁县| 桐梓县| 锦州市| 邛崃市| 九龙城区| 伊川县| 潼南县| 兰州市| 亳州市| 洪湖市| 岐山县| 玛曲县| 武川县| 长海县| 湖口县| 北海市| 黎平县| 天峨县| 湖南省| 灌南县| 黑龙江省| 承德县| 曲麻莱县| 康定县|