文字列を wchar_t 文字列に変換して、WNetUseConnection 関数で使用しようとしています。基本的には、このような unc 名"\\remoteserver"
です。戻りコード 1113が返されます。これは次のように説明されています。
対象のマルチバイト コード ページに Unicode 文字のマッピングが存在しません。
私のコードは次のようになります。
std::string serverName = "\\uncDrive";
wchar_t *remoteName = new wchar_t[ serverName.size() ];
MultiByteToWideChar(CP_ACP, 0, serverName.c_str(), serverName.size(), remoteName, serverName.size()); //also doesn't work if CP_UTF8
NETRESOURCE nr;
memset( &nr, 0, sizeof( nr ));
nr.dwType = RESOURCETYPE_DISK;
nr.lpRemoteName = remoteName;
wchar_t pswd[] = L"user"; //would have the same problem if converted and not set
wchar_t usrnm[] = L"pwd"; //would have the same problem if converted and not set
int ret = WNetUseConnection(NULL, &nr, pswd, usrnm, 0, NULL, NULL, NULL);
std::cerr << ret << std::endl;
興味深いのは、remoteName が次のようにハード コードされている場合です。
char_t remoteName[] = L"\\\\uncName";
すべて正常に動作します。しかし、後でサーバー上で、user と pwd は文字列として取得するパラメーターになるため、それらを変換する方法が必要です (同じ結果で mbstowcs 関数も試しました)。