文字列をGUIDに変換する。変換にはWindowsAPIのUuidFromString関数を利用します。
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <locale.h>
#include <rpc.h>
#pragma comment( lib, "Rpcrt4.lib" )
/*
GUIDを文字列に変換する
*/
std::wstring GuidToString
(
GUID* pGuid
)
{
std::wstring oGuidString;
RPC_WSTR waString;
// GUIDを文字列へ変換する
if ( RPC_S_OK == ::UuidToString( pGuid, &waString ) ) {
// GUIDを結果にセット
oGuidString = (WCHAR*)waString;
// GUID文字列の破棄
RpcStringFree( &waString );
}
// GUIDを返す
return( oGuidString );
}
/*
文字列をGUIDに変換する
*/
BOOL GuidFromString
(
GUID* pGuid
, std::wstring oGuidString
)
{
// 文字列をGUIDに変換する
if ( RPC_S_OK == ::UuidFromString( (RPC_WSTR)oGuidString.c_str(), (UUID*)pGuid ) ) {
// 変換できました。
return( TRUE );
}
return( FALSE );
}
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
/*
ロケールを日本に設定
これを設定するだけで、標準出力に日本語が表示される
ようになります。
*/
setlocale( LC_ALL, "Japanese" );
GUID oGuid;
// GUIDを文字列へ変換
GuidFromString( &oGuid, L"11223344-5566-7788-99aa-bbccddeeff11" );
// 標準出力へ出力する
std::wcout << GuidToString( &oGuid ).c_str() << std::endl;
// 正常終了
return( 0 );
}
11223344-5566-7788-99aa-bbccddeeff11