2012年8月16日 星期四

[C++] Create GUID

You can create GUID by using CoCreateGuid API.
I copy example source code from my reference article for memo as below.


#include "stdafx.h"
#include <objbase.h>
#include <string>

using namespace std;

wstring GetGUID()
{
    _TUCHAR *guidStr = NULL;

    GUID *pguid = new GUID;

    CoCreateGuid(pguid);

    // Convert the GUID to a string
    UuidToString(pguid, (RPC_WSTR*)&guidStr);
    delete pguid;
    return wstring(guidStr);
}

int _tmain(int argc, _TCHAR* argv[])
{
wstring guid = GetGUID();
    wprintf(guid.c_str());
    return 0;
}



Reference:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms688568(v=vs.85).aspx
http://www.dotblogs.com.tw/alonstar/archive/2011/09/05/mfc_32bit_64bit.aspx

沒有留言:

張貼留言