閑古鳥

オールドプログラマの日記。プログラミングとか病気(透析)の話とか。

ネットワークドライブに割り当てている先のパスを取得する

別のPC(とかLandiskとか)にあるフォルダ(\\foo\bar)をネットワークドライブZ:\に割り当てているという前提で、"Z:"を渡して"\\foo\bar"を得られる関数が欲しい。

ということでググったのだけど、.NETにはないんでしょうか。WNetGetConnection関数を利用すればできるようですが、これに対応する.NETのメソッドが見つかりませんでした。Path.GetFullPathとかDriveInfo辺りからさくっといけるのかなぁと期待していたのですが。

#include <iostream>
#include <string>
#include <cassert>
#include <windows.h>
#include <winnetwk.h>
#pragma comment(lib, "mpr.lib")

std::string GetRemotePath(std::string const& drive)
{
  char path[_MAX_PATH] = {0};
  DWORD len = _countof(path);
  DWORD const ret = ::WNetGetConnectionA(drive.c_str(), path, &len);
  assert(ret == NO_ERROR && "><");
  return path;
}

std::cout << GetRemotePath("Z:") << std::endl;
// => "\\foo\bar"

とりあえずC++で。まあ動けばいいかな。