C + + string-goto GUID and Invert

Source: Internet
Author: User
Tags 04x

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.