Delphi:NetShareGetInfo及其路径为空
我想获得共享的路径,我是这样做的:Delphi:NetShareGetInfo及其路径为空,delphi,directory,share,Delphi,Directory,Share,我想获得共享的路径,我是这样做的: 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
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为空。
这一份额当然存在
我做错了什么?您正在调用的函数没有填充您提供的记录。它不会填充您提供的字符串缓冲区。相反,它为结构分配内存,并返回该结构的地址 您的代码应该是:
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;
这里可能有一些错误,因为我看不到您对
NetShareGetInfo
、NetApiBufferFree
和TShareInfo2
的声明。但问题的本质正如我所描述的。我的错误是缺少^ShareInfo2:^TShareInfo;是的,事实上我在回答中就是这么说的