本文介绍了在Delphi中获取网络共享的本地名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



谢谢你提前

解决方案

这里有一个小例子:

 程序测试; 

{$ APPTYPE CONSOLE}

{$ R * .res}

使用
Windows,
SysUtils;

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

函数NetApiBufferFree(缓冲区:指针):DWORD;标准外部netapi
函数NetShareGetInfo(servername,netname:PWideChar; level:DWORD; out bufptr:Pointer):DWORD;标准
外部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;
结束

函数ShareNameToServerLocalPath(const ServerName,ShareName:string):string;
var
ErrorCode:DWORD;
缓冲区:指针;
begin
结果:='';

ErrorCode:= NetShareGetInfo(PWideChar(ServerName),PWideChar(ShareName),2,Buffer);
尝试
如果ErrorCode = NERR_Success然后
结果:= PShareInfo2(缓冲区)^。shi2_path;
finally
NetApiBufferFree(Buffer);
结束
结束

程序主;
begin
Writeln(ShareNameToServerLocalPath('\\\MyServer','MyShare'));
结束

开始
尝试
主;
除了
在E:异常do
开始
ExitCode:= 1;
Writeln(Format('[%s]%s',[E.ClassName,E.Message]));
结束
结束
结束。


How do I get the local name of a network share in Delphi?

I have a Filepath to the local alias of the network share Z:\someFolder\someFile and am able to expand the UNC path \\server\sharename\someFolder\someFile. However, I need the local path on the remote location F:\sharedFolder\someFolder\someFile

Thank you in advance

解决方案

Here's a small example:

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中获取网络共享的本地名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:55