• Advertisement
Sign in to follow this  

(Fixed Thanks All)Help Getting a string form a GUID

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. is there a easyer way to get a string from a GUID then this way.

Well I did Try to search msdn but nothing came up.

This is cool UuidToString thanks I'll try it out.



[color="#008000"][color="#008000"]//------------------------------------------------------------------------------------

//this will return the passed ID as a string

//--------------------------------------------------------------------------------------

std::string GetUnqueIDString(GUID *guid)

{

std::string strguid =

[color="#a31515"][color="#a31515"]"InValid";

[color="#008000"][color="#008000"]// Convert CLSID to String for printing

[color="#008000"] [color="#0000ff"][color="#0000ff"]wchar_t *uuid=0;

HRESULT hr = StringFromCLSID(*guid, &uuid);



[color="#0000ff"][color="#0000ff"]if(FAILED(hr))

[color="#0000ff"][color="#0000ff"]return strguid;

[color="#008000"][color="#008000"]// Convert to a char*

[color="#008000"] size_t origsize = wcslen(uuid) + 1;



[color="#0000ff"][color="#0000ff"]const size_t newsize = 256;

size_t convertedChars = 0;



[color="#0000ff"][color="#0000ff"]char nstring[newsize] = {[color="#a31515"][color="#a31515"]'\0'};

wcstombs_s(&convertedChars, nstring, origsize, uuid, _TRUNCATE);



CoTaskMemFree(uuid);

[color="#008000"][color="#008000"]// Release string

[color="#008000"] strguid = nstring;



[color="#0000ff"][color="#0000ff"]return strguid;

}

[color="#008000"][color="#008000"]//end GetUnqueIDString

[color="#008000"][color="#008000"] ///////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////



OR this one





[color="#008000"][color="#008000"]//------------------------------------------------------------------------------------

//this will return the passed ID as a string

//--------------------------------------------------------------------------------------

std::string GetUnqueIDString(GUID *guid)

{

std::string strguid =

[color="#a31515"][color="#a31515"]"InValid";

[color="#008000"][color="#008000"]// Convert CLSID to String for printing

[color="#008000"] [color="#0000ff"][color="#0000ff"]wchar_t *uuid=0;

HRESULT hr = StringFromCLSID(*guid, &uuid);



[color="#0000ff"][color="#0000ff"]if(FAILED(hr))

[color="#0000ff"][color="#0000ff"]return strguid;

std::ostringstream stm ;



[color="#0000ff"][color="#0000ff"]const std::ctype<[color="#0000ff"][color="#0000ff"]char>& ctfacet = std::use_facet< std::ctype<[color="#0000ff"][color="#0000ff"]char> >( stm.getloc() ) ;

[color="#0000ff"][color="#0000ff"]for( size_t i=0 ; i< wcslen(uuid) ; ++i )

stm << ctfacet.narrow( uuid, 0 ) ;

strguid = stm.str();



[color="#0000ff"][color="#0000ff"]return strguid;

}

[color="#008000"][color="#008000"]//end GetUnqueIDString

[color="#008000"][color="#008000"] ///////////////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////

Share this post


Link to post
Share on other sites
Advertisement

You can use the boost::uuid library instead, which has a much easier string conversion mechanism.


Well, windows GUID conversion to string is quite easy when you know it:
UuidToString

ankhd: The function receives an UUID instead of a GUID as parameter but, as you can see here, they are indeed the same.

Keep in mind this:


Remarks
An application calls UuidToString to convert a binary UUID to a string UUID. The RPC run-time library allocates memory for the string returned in the StringUuid parameter. The application is responsible for calling RpcStringFree to deallocate that memory.


[/quote]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement