Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi WTSQuerySessionInformationA falscher Parameter (https://www.delphipraxis.net/211705-wtsquerysessioninformationa-falscher-parameter.html)

Gruber_Hans_12345 25. Okt 2022 11:03

WTSQuerySessionInformationA falscher Parameter
 
Stehe gerade am Schlauch, mir liefert der Aufruf false zurück und der RaiseLastOsError sagt falscher Paramter.

Delphi-Quellcode:
   
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);
var ppBuffer: Pointer;
pBytesReturned: DWORD
begin
    _TWTSQuerySessionInformationA := GetProcAddress(LoadLibrary(PAnsiChar('wtsapi32.dll')), PAnsiChar('WTSQuerySessionInformationA'));
    Result := TWTSQuerySessionInformationA(_TWTSQuerySessionInformationA)(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientName, ppBuffer, pBytesReturned);
    try
        if not Result then
            RaiseLastOSError;
    except
    end;
end;
Ich vermute das ich irgendwo einen Nebeneffekt bekomme, da wenn ich es in einem 0815 testprogramm den selben code starte er durchläuft
Was könnte mein Programm da "verbrechen" das ich dann so einen Fehler bekomme? - wer ne idee?

TiGü 25. Okt 2022 13:07

AW: WTSQuerySessionInformationA falscher Parameter
 
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.

Gruber_Hans_12345 25. Okt 2022 15:22

AW: WTSQuerySessionInformationA falscher Parameter
 
Oh cool danke - darauf bin ich nicht gekommen aber logisch im nachhinein.

Danke noch mal funkt so

himitsu 25. Okt 2022 15:47

AW: WTSQuerySessionInformationA falscher Parameter
 
Delphi-Quellcode:
{$MinEnumSize 4}
https://docwiki.embarcadero.com/RADS..._size_(Delphi)


PS: statt der ANSI-Versionen A vielleicht langsam auch mal auf Unicode W umsteigen?
Delphi-Quellcode:
TWTSQuerySessionInformationW = function(hServer: THandle; SessionId: DWORD; WTSInfoClass: _WTS_INFO_CLASS; var ppBuffer: Pointer; var pBytesReturned: DWORD): BOOL; stdcall;

_TWTSQuerySessionInformationW := GetProcAddress(LoadLibrary('wtsapi32.dll'), 'WTSQuerySessionInformationW');
Der Name in ppBuffer ist dann natürlich Unicode (PWideChar) und nicht mehr ANSI.


Die Funktion gibt es nun schon seit Vista/WindowsServer2008.
Da alles vor Windows 10 bereits tot ist, könnte man das auch statisch einbinden, anstatt dynamisch,
aber falls man will, dann ginge es auch als Delayed-Loading.

https://docwiki.embarcadero.com/Code...ading_(Delphi)
Imports und Hooks kann man sich sparen, weil ist seit Jahren integriert.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:27 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz