Hola
Muchas Gracias por ayudarme Lepe.
Lo que me dices ya lo había probado yo, pero por si acaso lo volví a probar, y nada, sigo sin poder hacer un logoff del Pc; lo único que
me funciona es
ShutDownWindows(EWX_REBOOT); y
ShutDownWindows(EWX_POWEROFF); . No sé que estaré haciendo mal:
Creo un form con 2 botones:
Código Delphi
[-]
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ShutDownWindows(EWX_LOGOFF or EWX_FORCE);
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ShutDownWindows(EWX_FORCE);
end;
function TForm1.ShutDownWindows(Flag: Word): Boolean;
var
TokenPriv: TTokenPrivileges;
H: DWord;
HToken: THandle;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, HToken);
LookUpPrivilegeValue(NIL, 'SeShutdownPrivilege', TokenPriv.Privileges[0].Luid);
TokenPriv.PrivilegeCount := 1;
TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
H := 0;
AdjustTokenPrivileges(HToken, FALSE, TokenPriv, 0, PTokenPrivileges(NIL)^, H);
CloseHandle(HToken);
end;
Result := ExitWindowsEx(Flag, 0);
end;
y el ejecutable Project1.exe lo meto en la carpeta C:\Prueba. Creo una aplicación que arranca Project1.exe con privilegios de administrador:
Código Delphi
[-]
function CreateProcessWithLogonW(
lpUsername,lpDomain,lpPassword: PWideChar;
dwLogonFlags: Dword;
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: Dword;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
const lpStartupInfo: tSTARTUPINFO;
var lpProcessInformation: TProcessInformation): BOOL ; stdcall; external 'advapi32.dll';
begin
FillChar (StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
CreateProcessWithLogonW(StringToOleStr('UsuarioConPermisos'), nil, StringToOleStr('Contraseña'), 0,
StringToOleStr('C:\Prueba\Project1.exe'), nil, 0, nil, StringToOleStr('C:\Prueba'),
StartupInfo, ProcessInfo);
end.
Lo ejecuto, se inicia el form y al pulsar a alguno de los botones lo único que ocurre es que el form se cierra
; el guindols ni se inmuta
...
Muchas gracias y perdonen las molestias.