0

文字列を 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 関数も試しました)。

4

1 に答える 1

1

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);

これはまず、指定された文字列と0 終端を格納するために必要な文字数を MultiByteToWideChar でチェックし、次に文字列を割り当てて変換します。このコードをコンパイル/テストしていないことに注意してください。タイプミスに注意してください。

于 2011-02-16T10:40:05.563 に答える