Sign in to follow this  
ankhd

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

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 [b]UuidToString thanks I'll try it out.

[/b]

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

//this will return the passed ID as a string

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

[/color][/size][/color][/size][size="2"]std::string GetUnqueIDString(GUID *guid)

{

std::string strguid =

[/size][size="2"][color="#a31515"][color="#a31515"]"InValid"[/color][/color];[/size]

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

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

[size="2"] HRESULT hr = StringFromCLSID(*guid, &uuid);



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

[size="2"][color="#0000ff"][color="#0000ff"]return[/color][/color] strguid;[/size]

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

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



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

[size="2"] size_t convertedChars = 0;



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

[size="2"] wcstombs_s(&convertedChars, nstring, origsize, uuid, _TRUNCATE);



CoTaskMemFree(uuid);

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

[color="#008000"] [/color][size="2"] strguid = nstring;



[/size][size="2"][color="#0000ff"][color="#0000ff"]return[/color][/color] strguid;[/size]

[size="2"] }

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

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

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



[/color][/size][/color][/size]OR this one





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

//this will return the passed ID as a string

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

[/color][/size][/color][/size][size="2"]std::string GetUnqueIDString(GUID *guid)

{

std::string strguid =

[/size][size="2"][color="#a31515"][color="#a31515"]"InValid"[/color][/color];[/size]

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

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

[size="2"] HRESULT hr = StringFromCLSID(*guid, &uuid);



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

[size="2"][color="#0000ff"][color="#0000ff"]return[/color][/color] strguid;[/size]

[size="2"] std::ostringstream stm ;



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

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

[size="2"] stm << ctfacet.narrow( uuid[i], 0 ) ;

strguid = stm.str();



[/size][size="2"][color="#0000ff"][color="#0000ff"]return[/color][/color] strguid;[/size]

[size="2"] }

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

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

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

[/color][/size][/color][/size]

Share this post


Link to post
Share on other sites
You can use the [url=http://www.boost.org/doc/libs/1_46_1/libs/uuid/uuid.html] boost::uuid[/url] library instead, which has a much easier string conversion mechanism.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1303000484' post='4799326']
You can use the [url="http://www.boost.org/doc/libs/1_46_1/libs/uuid/uuid.html"] boost::uuid[/url] library instead, which has a much easier string conversion mechanism.
[/quote]

Well, windows GUID conversion to string is quite easy when you know it:
[url="http://msdn.microsoft.com/en-us/library/aa379352%28v=vs.85%29.aspx"]UuidToString[/url]

ankhd: The function receives an UUID instead of a GUID as parameter but, as you can see [url="http://msdn.microsoft.com/en-us/library/aa379358%28v=vs.85%29.aspx"]here[/url], they are indeed the same.

Keep in mind this:
[quote]

[b]Remarks[/b]
An application calls [b]UuidToString[/b] to convert a binary [url="http://msdn.microsoft.com/en-us/library/aa379358%28v=vs.85%29.aspx"][b]UUID[/b][/url] to a string [b]UUID[/b]. The RPC run-time library allocates memory for the string returned in the [i]StringUuid[/i] parameter. The application is responsible for calling [url="http://msdn.microsoft.com/en-us/library/aa378483%28v=vs.85%29.aspx"][b]RpcStringFree[/b][/url] to deallocate that memory.


[/quote]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this