#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. Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

    Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...

  2. NeHe OpenGL教程 第三十七课:卡通映射

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. 安装Nginx+uWSGI+Django环境

    Ubuntu Server 12.04 安装Nginx+uWSGI+Django环境 今天要介绍的是利用APT源直接apt-get install安装配置我们所需要的环境,首先按惯例先安装MySQL和 ...

  4. php 预定义接口

    Traversable Traversable { } 作用:检测一个类是否可以使用 foreach 进行遍历的接口. php代码中不能用.只有内部的PHP类(用C写的类)才可以直接实现Travers ...

  5. VI下删除所有内容

    让光标在一个位置 , 如果全删就放在问价开始的位置 , 在非编辑的状态下输入dG(注意大小写) , 这样光标之后的所有行都会被删掉

  6. unset是不能清除保存在本地电脑上的cookie的,用于session就可以(弄了半天原来是这样)

    unset($_COOKIE["historyWord[$wordId]"]); 这样是不行的,unset只是将变量在脚本运行时注销,但是cookie是写在客户端的,下一次还是可以 ...

  7. 那些不能错过的Xcode插件

      来源:http://www.cocoachina.com/applenews/devnews/2013/0918/7022.html 古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即 ...

  8. shell如何传递外部参数给文件

    shell里面如何传递参数: sh test.sh zhang 20 那test.sh里面咋接受参数呢? #!/usr/bin/env sh name=$1 age=$2 echo "nam ...

  9. 20145305 《Java程序设计》第8周学习总结

    教材学习内容总结 1.NIO使用频道来衔接数据节点,可以设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记,提供clear().rewind().flip().compact()等高级操作 2.想要 ...

  10. [ActionScript 3.0] AS3.0 动态加载显示内容

    可以将下列任何外部显示资源加载到 ActionScript 3.0 应用程序中: 在 ActionScript 3.0 中创作的 SWF 文件 — 此文件可以是 Sprite.MovieClip 或扩 ...