GUID与CString转换函数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/q_023/article/details/65442262

//GUID直接生成
GUID guidItem = GUID_NULL;
::CoCreateGuid(&guidItem);

//生成有/无减号GUID字符串
CString CreateGuidString(BOOL bRemoveSpec)
{
	CString strGUID = _T("");   
	GUID guid = GUID_NULL;
	::CoInitialize(NULL);

	if (S_OK == ::CoCreateGuid(&guid))   
	{   
		if (bRemoveSpec)
		{
			strGUID.Format(_T("%08X%04X%04x%02X%02X%02X%02X%02X%02X%02X%02X")   
				, guid.Data1   
				, guid.Data2   
				, guid.Data3   
				, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3]
			, guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]		  
			);
		}
		else
		{
			strGUID.Format(_T("%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X")
				, guid.Data1   
				, guid.Data2   
				, guid.Data3   
				, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3]
			, guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]
			);   
		}
	}

	::CoUninitialize();
	return strGUID;
}

//GUID转CString
CString	GuidToString( UUID *pUuid)
{
	CString   sTemp = _T("");
	unsigned short * pUuidString = NULL;
	RPC_STATUS ret = UuidToString( pUuid, &pUuidString);
	if( pUuidString ) 
	{
		sTemp= (LPTSTR) pUuidString ;
		RpcStringFree((unsigned short**) &pUuidString );
	} 
	else
		sTemp = _T("");
	sTemp.MakeUpper();
	return sTemp;
}

//CString转GUID
GUID  GuidFromString( LPCTSTR lpszUuid)
{
	GUID    guidGet = GUID_NULL;
	CString	strGuid = CString(lpszUuid);
	UuidFromString( (unsigned short*)((LPCTSTR)(strGuid.Left(36))), &guidGet );
	return guidGet;
}

展开阅读全文

不存在从 "CString" 到 "char *" 的适当转换函数求教

05-13

原来代码里面是COPini::WriteString("配置信息", "厚度", P[0],“setinfo.ini”)rn这样可直接在指定目录下生成setinfo.ini配置文件rn现在我是想把系统时间作为文件名来保存但是直接这样弄不行,我也查了很多资料有人说用下面这些方法把CString转换为char*rn[code=c]CTime t = CTime::GetCurrentTime();rn CString tm = t.Format("'%Y/%m/%d-%H/%M/%S'");rn tm =tm + ".ini";rnCOPini::WriteString("配置信息", "厚度", P[0],tm)//tm的参数类型为char *;[/code]rn我是想把系统时间作为文件名来保存但是直接这样弄不行,我也查了很多资料有人说用下面这些方法把CString转换为char*rnrn1.传给未分配内存的const char* (LPCTSTR)指针. rn CString cstr(asdd);rn const char* ch = (LPCTSTR)cstr;rn ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.rnrn2.传给未分配内存的指针.rn CString cstr = "ASDDSD";rn char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);rn cstr.ReleaseBuffer();rn //修改ch指向的值等于修改cstr里面的值.rn //PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.rnrn3.第二种用法。把CString 值赋给已分配内存的char *。rn CString cstr1 = "ASDDSD";rn int strLength = cstr1.GetLength() + 1;rn char *pValue = new char[strLength];rn strncpy(pValue, cstr1, strLength);rn之后试过转换类型还是不行rn[code=c]CTime t = CTime::GetCurrentTime();rn CString tm = t.Format("'%Y/%m/%d-%H/%M/%S'");rn tm =tm + ".ini";rn CString time(tm);rn char *pvalue =(LPTSTR)(LPCTSTR)tm; rnCOPini::WriteString("配置信息", "厚度", P[0],pvalue)[/code]rn那个大神能帮我解决下 论坛

没有更多推荐了,返回首页