如何将UNC转换为本地路径 [英] How to convert UNC to local path

查看:205
本文介绍了如何将UNC转换为本地路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个方法来获得给定UNC路径的相应本地路径。 Microsoft提供了一个小型库 CheckLCL 以此目的。所有Windows版本都不支持此库。有人知道任何开源方法吗?

I'm looking for a method to get corresponding local path for a given UNC path. Microsoft provides a small library CheckLCL for this purpose. This library is not supported on all Windows versions. Does anybody know any open source method for this?

还有MAPI函数 ScLocalPathFromUNC ,但不知道它是否适用于所有平台。

There is also MAPI function ScLocalPathFromUNC, but am not sure if it works on all platforms.

推荐答案

我有以下解决方案:

#include <Crtdbg.h>    // for debug stuff
#include "Winnetwk.h"  // for WNetGetUniversalName()
#include "Lm.h"        // for NetShareGetInfo()
#include "pystring.h"  // from http://code.google.com/p/pystring

#pragma comment( lib, "Mpr.lib" )       // for WNetGetUniversalName()
#pragma comment( lib, "Netapi32.lib" )  // for NetShareGetInfo()

//-----------------------------------------------------------------------------
// converts x:\\folder -> \\\\server\\share\\folder
bool ConvertLocalPathToUNC(const char* szFilePath, std::string& strUNC)
{
  // get size of the remote name buffer
  DWORD dwBufferSize = 0;
  char szBuff[2];
  if (::WNetGetUniversalName(szFilePath, UNIVERSAL_NAME_INFO_LEVEL, szBuff, &dwBufferSize) == ERROR_MORE_DATA)
  {
    // get remote name of the share
    char* buf = new char[dwBufferSize];
    UNIVERSAL_NAME_INFO* puni = (UNIVERSAL_NAME_INFO*) buf;

    if (::WNetGetUniversalName(szFilePath, UNIVERSAL_NAME_INFO_LEVEL, buf, &dwBufferSize) == NO_ERROR)
    {
      strUNC = puni->lpUniversalName;
      delete [] buf;
      return true;
    }
    delete [] buf;
  }

  return false;
}

//-----------------------------------------------------------------------------
// converts \\\\server\\share\\folder -> x:\\folder
bool ConvertUNCToLocalPath(const char* szUNC, std::string& strLocalPath)
{
  // get share name from UNC
  std::string strUNC(szUNC);
  std::vector< std::string > vecTokens;
  pystring::split(strUNC, vecTokens, _T("\\"));

  if (vecTokens.size() < 4)
    return false;

  // we need wchar for NetShareGetInfo()
  std::wstring strShare(vecTokens[3].length(), L' ');
  std::copy(vecTokens[3].begin(), vecTokens[3].end(), strShare.begin());

  PSHARE_INFO_502  BufPtr;
  NET_API_STATUS   res;
  if ((res = NetShareGetInfo(NULL, const_cast<LPWSTR>(strShare.c_str()), 502, (LPBYTE*) &BufPtr)) == ERROR_SUCCESS)
  {
    // print the retrieved data.
    _RPTF3(_CRT_WARN, _T("%ls\t%ls\t%u\n"), BufPtr->shi502_netname, BufPtr->shi502_path, BufPtr->shi502_current_uses);

    std::wstring strPath(BufPtr->shi502_path);
    strLocalPath.assign(strPath.begin(), strPath.end());

    // build local path
    for (size_t i = 4; i < vecTokens.size(); ++i)
    {
      if (!pystring::endswith(strLocalPath, _T("\\")))
        strLocalPath += _T("\\");
      strLocalPath += vecTokens[i];
    }

    // Validate the value of the shi502_security_descriptor member.
    if (IsValidSecurityDescriptor(BufPtr->shi502_security_descriptor))
      _RPTF0(_CRT_WARN, _T("It has a valid Security Descriptor.\n"));
    else
      _RPTF0(_CRT_WARN, _T("It does not have a valid Security Descriptor.\n"));

    // Free the allocated memory.
    NetApiBufferFree(BufPtr);
  }
  else
    return false;

  return true;
}

这篇关于如何将UNC转换为本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆