gpt4 book ai didi

delphi - Delphi中获取网络共享的本地名称

转载 作者:行者123 更新时间:2023-12-02 02:41:08 25 4
gpt4 key购买 nike

如何在 Delphi 中获取网络共享的本地名称?

我有一个到网络共享的本地别名 Z:\someFolder\someFile 的文件路径并且能够扩展 UNC 路径 \\server\sharename\someFolder\someFile 。但是,我需要远程位置上的本地路径 F:\sharedFolder\someFolder\someFile

提前谢谢

最佳答案

这是一个小例子:

program test;

{$APPTYPE CONSOLE}

{$R *.res}

uses
Windows,
SysUtils;

const
netapi = 'netapi32.dll';
NERR_Success = 0;
STYPE_DISKTREE = 0;
STYPE_PRINTQ = 1;
STYPE_DEVICE = 2;
STYPE_IPC = 3;
STYPE_TEMPORARY = $40000000;
STYPE_SPECIAL = $80000000;

function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external netapi;
function NetShareGetInfo(servername, netname: PWideChar; level: DWORD; out bufptr: Pointer): DWORD; stdcall;
external netapi;

type
PShareInfo2 = ^TShareInfo2;
TShareInfo2 = record
shi2_netname: PWideChar;
shi2_type: DWORD;
shi2_remark: PWideChar;
shi2_permissions: DWORD;
shi2_max_uses: DWORD;
shi2_current_uses: DWORD;
shi2_path: PWideChar;
shi2_passwd: PWideChar;
end;

function ShareNameToServerLocalPath(const ServerName, ShareName: string): string;
var
ErrorCode: DWORD;
Buffer: Pointer;
begin
Result := '';

ErrorCode := NetShareGetInfo(PWideChar(ServerName), PWideChar(ShareName), 2, Buffer);
try
if ErrorCode = NERR_Success then
Result := PShareInfo2(Buffer)^.shi2_path;
finally
NetApiBufferFree(Buffer);
end;
end;

procedure Main;
begin
Writeln(ShareNameToServerLocalPath('\\MyServer', 'MyShare'));
end;

begin
try
Main;
except
on E: Exception do
begin
ExitCode := 1;
Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
end;
end;
end.

关于delphi - Delphi中获取网络共享的本地名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com