Delphi: NetShareGetInfo 及其路径为空

Delphi: NetShareGetInfo and its path is empty

我想获取分享的路径,我是这样操作的:

Type
  TShareInfo2 = record
    shi2_netname: LPTSTR;
    shi2_type: DWORD;
    shi2_remark: LPTSTR;
    shi2_permissions: DWORD;
    shi2_max_uses: DWORD;
    shi2_current_uses: DWORD;
    shi2_path: LPTSTR;
    shi2_passwd: LPTSTR;
  End;

Function NetShareGetInfo(servername: LPWSTR; netname: LPWSTR; level: DWORD; bufptr: LPBYTE): DWORD; stdcall;

Var
  ShareInfo2: TShareInfo2;
  PChNetName, PChPath: array [0..255] of WideChar;
  ShareResult: Integer;
Begin
  With ShareInfo2 Do Begin
    shi2_netname := @PChNetName;
    shi2_type := 0;
    shi2_remark := nil;
    shi2_permissions := 0;
    shi2_max_uses := DWORD(-1);
    shi2_current_uses := 0;
    shi2_path := @PChPath;
    shi2_passwd := nil;
  End;
  ShareResult := NetShareGetInfo(Nil, PChar(FOld.sShareName), 2, @ShareInfo2);

  ShowMessage('Result='  + IntToStr(ShareResult)      +
              'PChPath=' + WideCharToString(PChPath)  );
End;

ShareResult 为 0,但 PChPath 为空。 份额当然存在​​。

我做错了什么?

您调用的函数没有填充您提供的记录。它不会填充您提供的字符串缓冲区。相反,它为结构分配内存,returns 那个的地址。

您的代码应该是:

Var
  ShareInfo2: ^TShareInfo2;
  ShareResult: Integer;
Begin
  ShareResult := NetShareGetInfo(Nil, PChar(FOld.sShareName), 2, @ShareInfo2);
  if Result = 0 then begin
    ShowMessage(WideCharToString(ShareInfo2.PChPath));
    NetApiBufferFree(ShareInfo2);
  end;
End;

这里可能有一些错误,因为我看不到你对NetShareGetInfoNetApiBufferFreeTShareInfo2的声明。但是问题的实质正如我所描述的那样。