Recent

Author Topic: How to obtain data from the FreePascal 'modules'?  (Read 2778 times)

runefist

  • Newbie
  • Posts: 1
How to obtain data from the FreePascal 'modules'?
« on: August 23, 2017, 12:10:24 pm »
Hey peeps,

I am totally new to Pascal, I've read some stuff about it but I have not really 'programmed' in this language, yet.
My client found https://github.com/graemeg/freepascal/blob/master/packages/winunits-jedi/src/jwawinsta.pas this file,
he saw line 236 ~ 262 which looked interesting to him. Now I would like to know how to obtain that data?

This might be a dumb question, but I might be able to figure it out myself, but asking it to some professionals which already know
the answer might be a smarter thing to do :D

I was using Lazarus as IDE, but I've downloaded the 'Free Pascal IDE' because I thought I would need that...?
I hope someone can explain me (detailed) of how to get the information from that file at line 236 ~ 262, and simply print the info.

Kind regards,
runefist
« Last Edit: August 23, 2017, 12:12:37 pm by runefist »

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to obtain data from the FreePascal 'modules'?
« Reply #1 on: August 23, 2017, 08:06:10 pm »
My client found https://github.com/graemeg/freepascal/blob/master/packages/winunits-jedi/src/jwawinsta.pas this file,
he saw line 236 ~ 262 which looked interesting to him. Now I would like to know how to obtain that data?
Which fields are interesting?
This structure is not documented by Microsoft, and in this form is incorrect (at least in Windows 7).
Here is an example of using a documented function.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$MODE OBJFPC}
  5. {$LONGSTRINGS ON}
  6.  
  7. uses Windows, JwaWtsApi32, SysUtils;
  8.  
  9. const
  10.   USERNAME_LENGTH = 20;
  11.  
  12. type
  13.   {$MINENUMSIZE 4}
  14.   TWTSConnectStateClass = (
  15.     WTSActive,              // User logged on to WinStation
  16.     WTSConnected,           // WinStation connected to client
  17.     WTSConnectQuery,        // In the process of connecting to client
  18.     WTSShadow,              // Shadowing another WinStation
  19.     WTSDisconnected,        // WinStation logged on without client
  20.     WTSIdle,                // Waiting for client to connect
  21.     WTSListen,              // WinStation is listening for connection
  22.     WTSReset,               // WinStation is being reset
  23.     WTSDown,                // WinStation is down due to error
  24.     WTSInit);               // WinStation in initialization
  25.  
  26.   PWtsInfoA = ^TWtsInfoA;
  27.   TWtsInfoA = record
  28.     State: TWTSConnectStateClass;
  29.     SessionId: DWORD;
  30.     IncomingBytes: DWORD;
  31.     OutgoingBytes: DWORD;
  32.     IncomingFrames: DWORD;
  33.     OutgoingFrames: DWORD;
  34.     IncomingCompressedBytes: DWORD;
  35.     OutgoingCompressedBy: DWORD;
  36.     WinStationName: array[0..WINSTATIONNAME_LENGTH - 1] of AnsiChar;
  37.     Domain: array[0..DOMAIN_LENGTH - 1] of AnsiChar;
  38.     UserName: array[0..USERNAME_LENGTH] of AnsiChar;
  39.     ConnectTime: TFileTime;
  40.     DisconnectTime: TFileTime;
  41.     LastInputTime: TFileTime;
  42.     LogonTime: TFileTime;
  43.     CurrentTime: TFileTime;
  44.   end;
  45.  
  46. function FileTimeToStr(const R: TFileTime): string;
  47. var
  48.   LocTime: TFileTime;
  49.   SysTime: TSystemTime;
  50. begin
  51.   if FileTimeToLocalFileTime(@R, @LocTime) and FileTimeToSystemTime(@R, @SysTime) then
  52.     Result := DateTimeToStr(SystemTimeToDateTime(SysTime))
  53.   else
  54.     Result := '';
  55. end;
  56.  
  57. var
  58.   PInfo: PWtsInfoA;
  59.   InfoSize: DWORD;
  60. begin
  61.   if WTSQuerySessionInformationA(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION,
  62.     TWtsInfoClass(24), PInfo{%H-}, InfoSize{%H-}) then
  63.   begin
  64.     Writeln('Domain\UserName: ', PInfo^.Domain, '\', PInfo^.UserName);
  65.     Writeln('LogonTime: ', FileTimeToStr(PInfo^.LogonTime));
  66.     Writeln('CurrentTime: ', FileTimeToStr(PInfo^.CurrentTime));
  67.     WTSFreeMemory(PInfo);
  68.     Readln;
  69.   end;
  70. end.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to obtain data from the FreePascal 'modules'?
« Reply #2 on: August 23, 2017, 08:25:56 pm »
This structure is not documented by Microsoft, and in this form is incorrect (at least in Windows 7).
Forgive my ignorance, but here perhaps ? Although not documented in the usual (online) sense.

Always better to use functions that are better documented though.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to obtain data from the FreePascal 'modules'?
« Reply #3 on: August 24, 2017, 07:25:56 pm »
Forgive my ignorance, but here perhaps ?
It's not so good. Here is an example:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4. {$MODE OBJFPC}
  5. {$LONGSTRINGS ON}
  6. {$MINENUMSIZE 4}
  7.  
  8. uses Windows, SysUtils;
  9.  
  10. const
  11.   SERVERNAME_CURRENT = 0;
  12.   LOGONID_CURRENT = ULONG(-1);
  13.   WINSTATIONNAME_LENGTH = 32;
  14.   DOMAIN_LENGTH = 17;
  15.   USERNAME_LENGTH = 20;
  16.   WinStationInformation = 8;
  17.  
  18. type
  19.   TWinStationStateClass = (State_Active, State_Connected, State_ConnectQuery,
  20.     State_Shadow, State_Disconnected, State_Idle, State_Listen, State_Reset,
  21.     State_Down, State_Init);
  22.  
  23.   TWinStationName = array[0..WINSTATIONNAME_LENGTH] of WideChar;
  24.  
  25.   TProtocolCounters = record
  26.     WdBytes: ULONG;
  27.     WdFrames: ULONG;
  28.     WaitForOutBuf: ULONG;
  29.     Frames: ULONG;
  30.     Bytes: ULONG;
  31.     CompressedBytes: ULONG;
  32.     CompressFlushes: ULONG;
  33.     Errors: ULONG;
  34.     Timeouts: ULONG;
  35.     AsyncFramingError: ULONG;
  36.     AsyncOverrunError: ULONG;
  37.     AsyncOverflowError: ULONG;
  38.     AsyncParityError: ULONG;
  39.     TdErrors: ULONG;
  40.     ProtocolType: USHORT;
  41.     Length: USHORT;
  42.     Reserved: array[0..99] of ULONG;
  43.   end;
  44.  
  45.   TCacheStatistics = record
  46.     ProtocolType: USHORT;
  47.     Length: USHORT;
  48.     Reserved: array[0..19] of ULONG;
  49.   end;
  50.  
  51.   TProtocolStatus = record
  52.     Output: TProtocolCounters;
  53.     Input: TProtocolCounters;
  54.     Cache: TCacheStatistics;
  55.     AsyncSignal: ULONG;
  56.     AsyncSignalMask: ULONG;
  57.   end;
  58.  
  59.   TWinStationInformation = record
  60.     ConnectState: TWinStationStateClass;
  61.     WinStationName: TWinStationName;
  62.     LogonId: ULONG;
  63.     ConnectTime: TFileTime;
  64.     DisconnectTime: TFileTime;
  65.     LastInputTime: TFileTime;
  66.     LogonTime: TFileTime;
  67.     Status: TProtocolStatus;
  68.     Domain: array[0..DOMAIN_LENGTH] of WideChar;
  69.     UserName: array[0..USERNAME_LENGTH] of WideChar;
  70.     CurrentTime: TFileTime;
  71.   end;
  72.  
  73. // Wrapper for RpcWinStationQueryInformation
  74. function WinStationQueryInformationW(hServer: THandle; SessionId: DWORD;
  75.   WinStationInformationClass: Cardinal; pWinStationInformation: PVOID;
  76.   WinStationInformationLength: DWORD; out pReturnLength: DWORD): BOOL;
  77.   stdcall; external 'winsta.dll';
  78.  
  79. function FileTimeToStr(const R: TFileTime): string;
  80. var
  81.   LocTime: TFileTime;
  82.   SysTime: TSystemTime;
  83. begin
  84.   if FileTimeToLocalFileTime(@R, @LocTime) and FileTimeToSystemTime(@R, @SysTime) then
  85.     Result := DateTimeToStr(SystemTimeToDateTime(SysTime))
  86.   else
  87.     Result := '';
  88. end;
  89.  
  90. var
  91.   R: TWinStationInformation;
  92.   Len: DWORD;
  93. begin
  94.   if WinStationQueryInformationW(SERVERNAME_CURRENT, LOGONID_CURRENT,
  95.     WinStationInformation, @R, SizeOf(R), Len) then
  96.   begin
  97.     Writeln('Domain\UserName: ', UnicodeString(R.Domain), '\', UnicodeString(R.UserName));
  98.     Writeln('LogonTime: ', FileTimeToStr(R.LogonTime));
  99.     Writeln('CurrentTime: ', FileTimeToStr(R.CurrentTime));
  100.     Readln;
  101.   end;
  102. end.
Only R.ConnectState not empty.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to obtain data from the FreePascal 'modules'?
« Reply #4 on: August 25, 2017, 05:09:58 am »
Thank you ASerge.

Indeed, there seem to be some major issues with regards to documentation and actual practice  :'(.

The structures as declared inside Jedi's jwawinsta unit seem to do a much better job for me when tested with xp.

In that case, i'm sorry about the noise. tbh, i hadn't checked because of the many OS related changes.

If even the official documentation requires documentation....  >:D   ;D

 

TinyPortal © 2005-2018