Discussion:
Tool to Convert stringbased guid to GUID C struct ?
(too old to reply)
Morten Christensen
2003-10-29 00:43:58 UTC
Permalink
I lack a simple tool to convert guids in string form to C-struct guids
of type GUID. Anyone found one ?
Kim Gräsman
2003-10-28 16:01:08 UTC
Permalink
Hi Morten,

Surprisingly enough, CLSIDFromString [1] converts any GUID-formed string to
a GUID.
There's also UuidFromString [2], but that requires your linking with
Rpcrt4.lib.

[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_3b3b.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rpc/rpc/uuidfromstring.asp
--
Best regards,
Kim Gräsman
Morten Christensen
2003-10-29 18:23:42 UTC
Permalink
Hi Kim,

Thanks, I was only aware of [2] ([1] seems to be better). However, I
was more looking for a tool (i.e. cmdline utility) that would print
out the struct to stdout (being to lazy to reinvent the wheel :-)).
Post by Kim Gräsman
Hi Morten,
Surprisingly enough, CLSIDFromString [1] converts any GUID-formed string to
a GUID.
There's also UuidFromString [2], but that requires your linking with
Rpcrt4.lib.
[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmf_a2c_3b3b.asp
[2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rpc/rpc/uuidfromstring.asp
Kim Gräsman
2003-10-29 09:29:36 UTC
Permalink
Morten,
However, I was more looking for a tool
(i.e. cmdline utility) that would print
out the struct to stdout
In what format? Do you want it in a form that you can cut and paste into
your code?
--
Best regards,
Kim Gräsman
Morten Christensen
2003-10-29 18:37:44 UTC
Permalink
Post by Kim Gräsman
Morten,
However, I was more looking for a tool
(i.e. cmdline utility) that would print
out the struct to stdout
In what format? Do you want it in a form that you can cut and paste into
your code?
Yes - strictly speaking I could just call the converter APIs on the
string, but I prefer to have explicit, readable CONSTANTS like this
(from the genguid tool):

static const GUID MyGuid = {// {C52D9DEC-FF85-4336-B09A-60C3D4482F31}
0xc52d9dec, 0xff85, 0x4336,
{ 0xb0, 0x9a, 0x60, 0xc3, 0xd4, 0x48, 0x2f, 0x31 }
};

I matter of taste, I guess.
Kim Gräsman
2003-10-29 09:50:49 UTC
Permalink
Hey Morten,
Post by Morten Christensen
Yes - strictly speaking I could just call the converter APIs on the
string, but I prefer to have explicit, readable CONSTANTS like this
Of course, I couldn't help myself from the tool challenge... Here goes:

--
// defguid.cpp : Defines the entry point for the console application.
//

#include "stdio.h"
#include <atlbase.h>

int main(int argc, char* argv[])
{
HRESULT hr;

USES_CONVERSION;

if(argc < 2 || argc > 3)
{
printf("Usage: defguid <guid string> [name]\n");
return 1;
}

const char* pszName = "unspecified";
if(argc == 3)
{
pszName = argv[2];
}

GUID guid;
hr = CLSIDFromString(A2OLE(argv[1]), &guid);
if(FAILED(hr))
{
printf("Failed to parse GUID string\n");
return hr;
}

printf("DEFINE_GUID(%s, 0x%08X, 0x%04X, 0x%04X, 0x%02X, 0x%02X, 0x%02X,
0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02x);\n",
pszName, 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]);

return 0;
}
--

Hope that helps.
--
Best regards,
Kim Gräsman
Morten Christensen
2003-10-29 19:41:54 UTC
Permalink
Hi Kim,

Thanks for the single-most useful reply in a newsgroup ever! :-)

Since, we are already "reinventing the wheel" (surely somebody else
most have made conversion tools ?), I took the liberty to make some
changes (see below),

Thanks again.

Chers,
Morten

-------

// StringToGUID.cpp :

#include "stdafx.h"
#include "stdio.h"
#include <atlbase.h>

int main(int argc, char* argv[])
{
HRESULT hr;

USES_CONVERSION;

if(argc < 2 || argc > 3)
{
printf("Usage: StringToGUID <guid string> [name]\n");
return 1;
}

const char *strGUID = argv[1];

const char* pszName = "<<name>>";
if(argc == 3)
{
pszName = argv[2];
}

GUID guid;
hr = CLSIDFromString(A2OLE(strGUID), &guid);
if(FAILED(hr))
{
printf("Failed to parse GUID string\n");
return hr;
}

printf("static const GUID %s = { // %s\n 0x%08X, 0x%04X, 0x%04X,\n {
0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02x
}\n};\n",
pszName, strGUID, 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]);

return 0;
}
Post by Kim Gräsman
Hey Morten,
Post by Morten Christensen
Yes - strictly speaking I could just call the converter APIs on the
string, but I prefer to have explicit, readable CONSTANTS like this
--
// defguid.cpp : Defines the entry point for the console application.
//
#include "stdio.h"
#include <atlbase.h>
int main(int argc, char* argv[])
{
HRESULT hr;
USES_CONVERSION;
if(argc < 2 || argc > 3)
{
printf("Usage: defguid <guid string> [name]\n");
return 1;
}
const char* pszName = "unspecified";
if(argc == 3)
{
pszName = argv[2];
}
GUID guid;
hr = CLSIDFromString(A2OLE(argv[1]), &guid);
if(FAILED(hr))
{
printf("Failed to parse GUID string\n");
return hr;
}
printf("DEFINE_GUID(%s, 0x%08X, 0x%04X, 0x%04X, 0x%02X, 0x%02X, 0x%02X,
0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02x);\n",
pszName, 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]);
return 0;
}
--
Hope that helps.
Loading...