c++ - 如何将 UNC 转换为本地路径

标签 c++ windows winapi visual-c++ filesystems

我正在寻找一种方法来为给定的 UNC 路径获取相应的本地路径。微软提供了一个小库CheckLCL以此目的。并非所有 Windows 版本都支持此库。有人知道这方面的任何开源方法吗?

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

最佳答案

在 MSDN 中进行一些谷歌搜索和挖掘后,我有以下解决方案:

#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;
}

关于c++ - 如何将 UNC 转换为本地路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316927/

相关文章:

C++11:如何获取 C++14 模板化映射::查找

c# - CreateProcessWithTokenW - C# 中的用法示例

c++ - 如何重新加载隐式链接的dll

c++ - glReadPixel(一次通过与通过点循环)

c++ - Visual C++ 中的浮点精度

C++ 打印 chrono::duration 的天数、小时数、分钟数等

c++ - 确定 CPU 缓存中值的值和/或地址

c# - 更改默认 Windows 键盘快捷键

python - python ctypes中的未知数组长度

c++ - 在 BOOST Graph 中过滤现有的 filtered_graph