Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: WTSQuerySessionInformationA falscher Parameter

  Alt 25. Okt 2022, 14:07
Delphi-Quellcode:
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}


uses
    System.SysUtils,
    Winapi.Windows;

type
    _WTS_INFO_CLASS = DWORD;
    // _WTS_INFO_CLASS <--- muss vier Byte groß sein (also DWORD), kleiner mit Word (SizeOf == 2) oder
    // Byte (SizeOf == 1) wie die meisten Delphi-Enums geht schief.

const
    WTSInitialProgram: _WTS_INFO_CLASS = 0;
    WTSApplicationName: _WTS_INFO_CLASS = 1;
    WTSWorkingDirectory: _WTS_INFO_CLASS = 2;
    WTSOEMId: _WTS_INFO_CLASS = 3;
    WTSSessionId: _WTS_INFO_CLASS = 4;
    WTSUserName: _WTS_INFO_CLASS = 5;
    WTSWinStationName: _WTS_INFO_CLASS = 6;
    WTSDomainName: _WTS_INFO_CLASS = 7;
    WTSConnectState: _WTS_INFO_CLASS = 8;
    WTSClientBuildNumber: _WTS_INFO_CLASS = 9;
    WTSClientName: _WTS_INFO_CLASS = 10;

type
    TWTSQuerySessionInformationA = function(hServer: THandle; SessionId: DWORD; WTSInfoClass: _WTS_INFO_CLASS; var ppBuffer: Pointer;
        var pBytesReturned: DWORD): BOOL; stdcall;

const
    WTS_CURRENT_SESSION = DWORD(-1);
    WTS_CURRENT_SERVER_HANDLE = 0;

function TestWTS: BOOL;
var
    ppBuffer: Pointer;
    pBytesReturned: DWORD;
    _TWTSQuerySessionInformationA: TWTSQuerySessionInformationA;
begin

    _TWTSQuerySessionInformationA := GetProcAddress(LoadLibraryA(PAnsiChar('wtsapi32.dll')), PAnsiChar('WTSQuerySessionInformationA'));
    Result := TWTSQuerySessionInformationA(_TWTSQuerySessionInformationA)(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientName,
        ppBuffer, pBytesReturned);
    try
        if Result then
        begin
            Writeln('Call to WTSQuerySessionInformation was successful');
        end else
            RaiseLastOSError;
    except
    end;
end;

begin
    try
        TestWTS;
    except
        on E: Exception do
                Writeln(E.ClassName, ': ', E.Message);
    end;
    Readln;

end.
  Mit Zitat antworten Zitat