#include "stdafx.h"
#include <string>
#include <windows.h> using namespace std; #define CONVERT_STR_2_GUID(cstr, stGuid) do\
{\
swscanf_s((const wchar_t*)cstr, L"{%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x}",\
&(stGuid.Data1),&(stGuid.Data2),&(stGuid.Data3),\
&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]),\
&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]),&(stGuid.Data4[]));\
}while(); int _tmain(int argc, _TCHAR* argv[])
{
const wstring strGuid = L"{9245fe4a-d402-451c-b9ed-9c1a04247482}";
GUID stGuid = {}; 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[],stGuid.Data4[],stGuid.Data4[],stGuid.Data4[],
stGuid.Data4[],stGuid.Data4[],stGuid.Data4[],stGuid.Data4[]); return ;
}

上面这段程序能看出问题来么? 看出来的兄弟可以在下面指出来!

反正在vs2012及vs2013环境中验证是有问题的。如GUID {9245fe4a-d402-451c-b9ed-9c1a04247482},报下面现象:

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

---------------------------
中止(A) 重试(R) 忽略(I)
---------------------------

暂时记在这,其实还有其它方法可以实现,可以避免上面出错, 如下:

#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;
}

反转就更容易了,可以用sprintf之类的,也可以用StringFromCLSID

参考:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms680589(v=vs.85).aspx

c++ string 转GUID及反转的更多相关文章

  1. c# string 转 GUID

    提供两种方法 1.try...catch... /* * string TO guid */ private static bool ToGuid(string str) { Guid gv = ne ...

  2. string转换为guid类型 split

    string str = "{"+context.Request["ID"]+"}"; KpiUser.ID = new Guid(str) ...

  3. (String)将一个String里面的单词反转

    e.g.  i love java    return   java love i public static String reverseStr(String str) { String[] str ...

  4. 344. Reverse String 最基础的反转字符串

    [抄题]: [暴力解法]: 时间分析: 空间分析: [奇葩输出条件]: [奇葩corner case]: [思维问题]: 还停留在 i < len / 2的阶段,不行,应该是指针对撞问题了 [一 ...

  5. asp.net MVC Model 类的主键 int类型、string类型、GUID类型。

    在使用asp.net mvc进行定义 模型类的时候,一般情况下,我们都会定义一个属性为 int iD{get;set;} 或为int ClassNameID {get;set;},在这种情况下 1.I ...

  6. Guid的使用

    今天在公司做修改功能时,老大让使用部分更新,但是表中的主键是UNIQUEIDENTIFIER类型,它会在我们添加纪录时,默认生成一个unicode码, 但是我现在必须要将获取到的已经是string类型 ...

  7. Java之控制反转和依赖注入

    1.简介 依赖注入和控制反转,目的是为了使类与类之间解耦合,提高系统的可扩展性和可维护性,下面通过一个例子来引入这一概念. 2.案例 1)一般情况下的类耦合 Main.java public clas ...

  8. SQL Server中的GUID

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  9. 自定义GUID类

    今天,同事发了个邮件说需要一个内部使用的GUID类,而不是System.Guid 结构体.什么意思呢? 例如public void Test(string guidPara) 这个方法的参数是个str ...

随机推荐

  1. 无状态服务(stateless service)

    一.定义 无状态服务(stateless service)对单次请求的处理,不依赖其他请求,也就是说,处理一次请求所需的全部信息,要么都包含在这个请求里,要么可以从外部获取到(比如说数据库),服务器本 ...

  2. Windows XP下安装和配置Apache2.2.22服务器+PHP5+Mysql5

    原文:http://www.chinaz.com/web/2012/0516/252021.shtml 随着PHP网站的流行,国内越来越多的站长使用php开发网站或者使用相关的php开源网站(例如:D ...

  3. NPM使用

    安装路径修改: 4.配置npm的全局模块存放路径和cache路径 输入以下命令 npm config set prefix  “D:\Program Files\node\node-global” n ...

  4. 无侵入方面编程-用HttpModule+SoapExtension监视页面执行参数(一)

    先简单介绍一下项目吧,我们这个项目是用VS2003开发的,老早一个项目.WEB前端机+业务处理(WebService层)+数据库分别布置在不同的计算机上. 现在老总有一个需求,要统计出每个页面的执行时 ...

  5. EDE,DEDE网站搬家,DEDECMS搬家教程,一看就会

    无聊做个小教程:DEDE搬家,本人提供搬家 先来说下DEDE 大家会安装吧,不多说,看图 一.进入织梦DedeCms的后台备份数据库 步骤:系统 –> 数据库备份/还原 -> 全选有所织梦 ...

  6. shell 统计 awk

    time awk '{a[$1]++}END{for(i in a){printf("%d\t%s\n",a[i],i)}}' access.log | sort -nr | he ...

  7. Ext.MessageBox

    Ext.require([ 'Ext.window.MessageBox', 'Ext.tip.*' ]); Ext.onReady(function(){ Ext.MessageBox.confir ...

  8. (medium)LeetCode 221.Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  10. 2015 Multi-University Training Contest 6 Cake

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m soda and today i ...