Einzelnen Beitrag anzeigen

Benutzerbild von Remko
Remko

Registriert seit: 10. Okt 2006
Ort: 's-Hertogenbosch, Die Niederlande
222 Beiträge
 
RAD-Studio 2010 Arc
 
#9

AW: Erkennen von Remote Desktop

  Alt 1. Nov 2010, 23:54
Oder gibt es etwas einfacheres?
Da müßtest du mal Remko fragen (der ist auch Benutzer hier, aber dann ggf. auf Englisch fragen ).

Es könnte durchaus eine der WTS*-Funktionen besser geeignet sein, aber ich müßte mich erst einlesen und er weiß es vielleicht ohne Nachgucken.
Sadly there is not Wts* or even WinStation* function that can do this, but I tested the code below and that's the only way I see to enumerate all Client Drives:

Delphi-Quellcode:
uses
  JwaWinType, JwaWinNetWk; // Jedi Apilib, can also use JwaWindows instead of Single Units

const
  WNNC_NET_TERMSRV = $00360000;

type
  TNetResourceArray = array[0..ANYSIZE_ARRAY-1] of NETRESOURCE;
  PNetResourceArray = ^TNetResourceArray;

procedure TForm3.Button1Click(Sender: TObject);
var
  NetResPtr: PNetResourceArray;
  ProviderName: array[0..MAX_PATH] of Char;
  BufSize: DWORD;
  hEnum: DWORD;
  Count: DWORD;
  Res: Cardinal;
  i: Integer;
begin
  // Get Provider Name (should be 'Microsoft Terminal Services')
  BufSize := Length(ProviderName) * SizeOf(Char);
  ZeroMemory(@ProviderName[0], BufSize);
  Res := WNetGetProviderName(WNNC_NET_TERMSRV, ProviderName, BufSize);
  if Res <> NO_ERROR then
  begin
    // Handle the Error (call WNetGetLastError for more info), raise exception..
  end;

  Memo1.Lines.Add(Format('ProviderName=%s', [ProviderName]));
  Res := WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0, nil, hEnum);
  if Res = NO_ERROR then
  begin

    // return as many entries as possible
    Count := DWORD(-1);

    // Allocate a some memory, 16 KB is typical size per docs
    BufSize := 16384;
    Memo1.Lines.Add(Format('Allocate %d Bytes', [BufSize]));
    GetMem(NetResPtr, BufSize);
    Res := WNetEnumResource(hEnum, Count, NetResPtr, BufSize);

    while Res = ERROR_MORE_DATA do
    begin
      Inc(BufSize, SizeOf(NETRESOURCE));
      Memo1.Lines.Add(Format('Allocate %d Bytes', [BufSize]));
      ReAllocMem(NetResPtr, BufSize); // Alloc more...
      Count := DWORD(-1);
      Res := WNetEnumResource(hEnum, Count, NetResPtr, BufSize);
    end;

    if Res = NO_ERROR then
    begin
      Memo1.Lines.Add(Format('Count=%d', [Count]));
      for i := 0 to Count - 1 do
      begin
        if lstrcmpi(NetResPtr^[i].lpProvider, @ProviderName[0]) = 0 then
        begin
          Memo1.Lines.Add(Format('Found TSClient Drive: %s', [NetResPtr^[i].lpRemoteName]));
        end
        else begin
          Memo1.Lines.Add(Format('Found non TSClient Drive: %s', [NetResPtr^[i].lpRemoteName]));
        end;
      end;
    end
    else begin
      Memo1.Lines.Add(Format('WNetEnumResource returned %d', [Res]));
    end;

    FreeMem(NetResPtr);
    WNetCloseEnum(hEnum);
  end
  else begin
    Memo1.Lines.Add(Format('WNetOpenEnum returned %d', [Res]));
  end;
end;

end.
On my testsystem the output is:
ProviderName=Microsoft Terminal Services
Allocate 16384 Bytes
Count=3
Found TSClient Drive: \\tsclient\Z
Found TSClient Drive: \\tsclient\C
Found TSClient Drive: \\tsclient\D
See my blog blog
See our Jedi blog

Geändert von Remko ( 2. Nov 2010 um 00:10 Uhr)
  Mit Zitat antworten Zitat