Megabyte |
17-10-2003 09:54 207841 |
Вот пример на Дельфи:
Код:
Unit RunAsExt;
Interface
Uses Windows, SysUtils;
function CreateProcessWithLogonW(
lpUserName: PWideChar;
lpDomain: PWideChar;
lpPassword: PWideChar;
dwLogonFlags: DWORD;
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PChar;
const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation
): BOOL; stdcall;
Function RunAs(Username, Password, Command: String): String;
Implementation
function CreateProcessWithLogonW; external advapi32 name 'CreateProcessWithLogonW';
Function RunAs(Username, Password, Command: String): String;
Var
si: TStartupInfo;
pi: TProcessInformation;
pUser, pPass, pDomain, pProgram: array [0..255] of WChar;
LastError: DWORD;
ResultString: String;
Begin
ZeroMemory(@si, SizeOf(si));
si.cb:=SizeOf(si);
ZeroMemory(@pi, SizeOf(pi));
StringToWideChar(UserName, pUser, 255);
StringToWideChar(PassWord, pPass, 255);
StringToWideChar('', pDomain, 255);
StringToWideChar(Command, pProgram, 255);
CreateProcessWithLogonW(
pUser,
pDomain,
pPass,
1, //LOGON_WITH_PROFILE,
pProgram,
nil,
CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or
CREATE_NEW_PROCESS_GROUP or CREATE_SEPARATE_WOW_VDM,
nil,
nil,
si,
pi
);
LastError:=GetLastError;
Case LastError of
0: ResultString:='Success!';
86: ResultString:='Wrong password';
1326: ResultString:='Wrong username or password';
1327: ResultString:='Logon failureЧuser account restriction';
1385: ResultString:='Logon failureЧthe user has not been granted the requested logon type at this computer.';
2: ResultString:='File not found';
5: ResultString:='Acced denied';
else
ResultString:='Error '+IntToStr(LastError);
end;
Result:=ResultString;
End;
END.
|