C++ WNetUseConnection系统错误代码1113不存在映射
我正在尝试将字符串转换为wchar\t字符串,以便在WNetUseConnection函数中使用它。 基本上它是一个unc名称,看起来像这样C++ WNetUseConnection系统错误代码1113不存在映射,c++,winapi,unc,C++,Winapi,Unc,我正在尝试将字符串转换为wchar\t字符串,以便在WNetUseConnection函数中使用它。 基本上它是一个unc名称,看起来像这样“\\remoteserver”。 我得到一个返回码1113,它被描述为: Unicode字符没有映射 存在于目标多字节代码中 页面 我的代码如下所示: std::string serverName = "\\uncDrive"; wchar_t *remoteName = new wchar_t[ serverName.size() ]; Multi
“\\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;
char_t remoteName[] = L"\\\\uncName";
一切正常。但由于稍后在服务器上,user和pwd将是我作为字符串获取的参数,因此我需要一种转换它们的方法(也尝试了mbstowcs函数并获得了相同的结果)。MultiByteToWideChar不会用当前代码0-终止转换后的字符串,因此在转换后的“\uncDrive”后面会出现垃圾字符 使用以下命令:
std::string serverName = "\\uncDrive";
int CharsNeeded = MultiByteToWideChar(CP_ACP, 0, serverName.c_str(), serverName.size() + 1, 0, 0);
wchar_t *remoteName = new wchar_t[ CharsNeeded ];
MultiByteToWideChar(CP_ACP, 0, serverName.c_str(), serverName.size() + 1, remoteName, CharsNeeded);
首先使用MultiByteToWideChar检查存储指定字符串和0终止需要多少个字符,然后分配字符串并转换它。请注意,我没有编译/测试这段代码,请注意拼写错误