#include"stdafx.h"#include<string>#include<windows.h>using namespacestd;#defineConvert_str_2_guid (CStr, Stguid) do\{swscanf_s (Constwchar_t*) CStr, L"{%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x}", & (STGUID.DATA1),& (STGUID.DATA2),&(STGUID.DATA3),& (stguid.data4[0]),& (stguid.data4[1]),& (stguid.data4[2]),& (stguid.data4[3]), & (stguid.data4[4]),& (stguid.data4[5]),& (stguid.data4[6]),& (stguid.data4[7]));} while(0);int_tmain (intARGC, _tchar*argv[]) { ConstWstring strGUID = L"{9245fe4a-d402-451c-b9ed-9c1a04247482}"; GUID Stguid= {0}; Convert_str_2_guid (Strguid.c_str (), stguid); wprintf_s (L"%s\r\n", Strguid.c_str ()); wprintf_s (L"{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", stguid.data1,stguid.data2,stguid.data3, stguid.data4[0],stguid.data4[1],stguid.data4[2],stguid.data4[3], stguid.data4[4],stguid.data4[5],stguid.data4[6],stguid.data4[7]); return 0;}
Can you see the problem in the above procedure? See the brothers can be pointed out below!
It is problematic to verify in vs2012 and vs2013 environments anyway. such as GUID {9245fe4a-d402-451c-b9ed-9c1a04247482}, report the following behavior:
---------------------------
Microsoft Visual C + + Runtime Library
---------------------------
Debug error!
Program:d:\cpp_pro\guidtest\debug\guidtest.exe
Module:d:\cpp_pro\guidtest\debug\guidtest.exe
File:
Run-time Check Failure #2-stack around the variable ' stguid ' was corrupted.
(Press Retry to debug the application)
---------------------------
Abort (A) retry (R) ignore (I)
---------------------------
For the time being, there are other methods that can be implemented to avoid the error above, as follows:
#include "stdafx.h" #include <string> #include <windows.h>using namespace std;int _tmain (int argc, _tchar* Argv[]) {Const wstring strGUID = L "{9245fe4a-d402-451c-b9ed-9c1a04247482}"; GUID Stguid = {0}; CLSIDFromString ((LPCOLESTR) Strguid.c_str (), (LPCLSID) &stguid); wprintf_s (L "%s\r\n", Strguid.c_str ()); wprintf_ S (L "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", Stguid.data1,stguid.data2,stguid.data3,stguid.data4[0] , stguid.data4[1],stguid.data4[2],stguid.data4[3],stguid.data4[4],stguid.data4[5],stguid.data4[6],stguid.data4[ 7]); return 0;}
Reversal is easier, can be used sprintf and the like, can also be used stringfromclsid.
Reference:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms680589 (v=vs.85). aspx
C + + string-goto GUID and Invert