wxString to GUID

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
brownb2
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Aug 30, 2008 6:41 am

wxString to GUID

Post by brownb2 »

Does anybody know how to do parse a wxString as a GUID? Using wxCmdLineParser I have a wxString from the command line of the format {3F2504E0-4F89-11D3-9A0C-0305E82C3301} that I guess I could somehow cast to raw memory and into a GUID but that's just horrible and probably not locale/string format friendly.

Any help is appreciated.
Jophin
Knows some wx things
Knows some wx things
Posts: 41
Joined: Sun Apr 05, 2009 1:53 am
Location: Maharashtra , India

Post by Jophin »

If you already have a GUID as a string {3F2504E0-4F89-11D3-9A0C-0305E82C3301} and want to obtain each fields then use split funtion posted in
http://forums.wxwidgets.org/viewtopic.p ... ring+split

// An example (not tested) to use this spilt function
wxString myGUID;
wxArrayString myGUIDfileds; // String array
myGUID = "3F2504E0-4F89-11D3-9A0C-0305E82C3301";
Split( myGUID, myGUIDfileds, "-"); // "-" is the field separator which will split the string

/*
The array myGUIDfileds will hold data as shown
myGUIDfileds[0] = 3F2504E0
myGUIDfileds[1] = 4F89
myGUIDfileds[2] = 11D3
myGUIDfileds[3] = 9A0C
myGUIDfileds[4] = 9A0C-0305E82C3301
*/




If you wants to generate a new GUID string ; read post http://forums.wxwidgets.org/viewtopic.p ... highlight=


Wiki for more information on Globally Unique Identifier http://en.wikipedia.org/wiki/Globally_Unique_Identifier
Jophin K.
Maharashtra , India
brownb2
In need of some credit
In need of some credit
Posts: 7
Joined: Sat Aug 30, 2008 6:41 am

Post by brownb2 »

I cheated:

UuidFromString((UCHAR*)(LPCTSTR)myWxString,&myGuid);

I have no idea if this is "safe" but it seems to work - the string is in a GUID format without braces.

TBH I know that the string is being cast to a long pointer to a const char string, and then to unsigned char ptr but no idea how it works (i.e why I couldn't just cast to a UCHAR*)

BTW this took me nearly 5 hours to work out... in Java and most languages all I need to do is something like "new Guid(somestring)", why is it C++ APIs tend to be so obtuse especially on Win32?
leiradella
I live to help wx-kind
I live to help wx-kind
Posts: 172
Joined: Sun Sep 07, 2008 9:49 pm
Location: Rio de Janeiro, Brazil

Post by leiradella »

brownb2 wrote:UuidFromString((UCHAR*)(LPCTSTR)myWxString,&myGuid);
I'd be surprised if it works as expected...

If myWxString is a pointer (i.e. wxString*) then you're not converting the string characters to an UUID. You're just passing the address where the instance lives in memory and UuidFromString is happily creating an UUID reading bytes from memory.

If it really works, it looks like the (LPCTSTR) cast is making myWxString return a char * which is then cast to UCHAR* which is fed to UuidFromString. But I couldn't find anywhere in the docs that a simple cast of a wxString would be enough to get a pointer to its characters.

So, did you validate that the UUID really corresponds to the string?

Cheers,

Andre
Post Reply