Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TCHAR to GUID (How to)

90 views
Skip to first unread message

Fernando Ortigosa

unread,
Jun 4, 2003, 8:04:30 AM6/4/03
to
I retrieve a CLSID (From an ActiveX typelib entry) from registry storing it
in a TCHAR, but I need to store it in a GUID so I can compare it with
another GUID I retrieve from a file in the HDD. Is there any way to convert
from TCHAR to GUID or inverse?


Craig Powers

unread,
Jun 4, 2003, 9:46:11 AM6/4/03
to

From GUID to char* via a stream:

// This is supposed to reset the state on the ostream after its done,
// but I'm not 100% certain it does so correctly, nor am I 100% certain
// that it doesn't wipe out error state info.
std::ostream& operator<<(std::ostream& lhs, GUID const & rhs)
{
long nFlags = lhs.flags();
std::streamsize nWidth = lhs.width();
char cFill = lhs.fill();

lhs << std::hex << std::setfill('0') ;
lhs << std::setw(8) << rhs.Data1 << "-"
<< std::setw(4) << rhs.Data2 << "-" << rhs.Data3 << "-";
lhs << std::setw(2) << (unsigned long) rhs.Data4[0] << (unsigned
long) rhs.Data4[1] << "-";

for (int i = 2; i < 8; ++i)
lhs << std::setw(2) << (unsigned long) rhs.Data4[i];

lhs.flags(nFlags);
lhs.width(nWidth);
lhs.fill(cFill);

return lhs;
}

Then, you can use it like this:
GUID guid; // From wherever it is you get it

std::ostringstream oss;
oss << guid;

std::string s_guid = oss.str();


// Or, if you absolutely must have a char* (not recommended):
LPSTR sz = new char[39]; // I recommend double-checking the length

strncpy(sz, oss.str().c_str(), 39);

// ...

delete[] sz;

--
Craig Powers
MVP - Visual C++

Fernando Ortigosa

unread,
Jun 4, 2003, 10:14:24 AM6/4/03
to
Thanks a lot...

By reading this I know how few C++ I know.

*Bows*

"Craig Powers" <eni...@hal-pc.org> escribió en el mensaje
news:3EDDF823...@hal-pc.org...

CheckAbdoul

unread,
Jun 4, 2003, 10:17:47 AM6/4/03
to
See if you can use the CLSIDFromString() API. If you are using MFC then
take a look at the AfxGetClassIDFromString() global function.

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------


"Fernando Ortigosa" <fernando...@e2000fi.com> wrote in message
news:e6UJPGpK...@TK2MSFTNGP12.phx.gbl...

Fernando Ortigosa

unread,
Jun 4, 2003, 12:03:56 PM6/4/03
to
YES! It worked!

Hmmm. Just Adding:

USES_CONVERSION;
CLSIDFromString(T2OLE(tchTypeCLSID),&Info.guid);
where tchTypeCLSID is a TCHAR containing the CLSID string
and Info is an structure (where I store some typelib info) whose guid member
belongs to the GUID data type

Apart of this I had to add:
#include <atlbase.h>
#include <atlcom.h>
#include <atlconv.h>
using namespace ATL;

"CheckAbdoul" <check...@nospam.mvps.org> escribió en el mensaje
news:#17vHRqK...@TK2MSFTNGP12.phx.gbl...

Craig Powers

unread,
Jun 4, 2003, 12:17:20 PM6/4/03
to
Craig Powers wrote:
>
> // This is supposed to reset the state on the ostream after its done,
> // but I'm not 100% certain it does so correctly, nor am I 100% certain
> // that it doesn't wipe out error state info.
> std::ostream& operator<<(std::ostream& lhs, GUID const & rhs)
> {
> long nFlags = lhs.flags();
> std::streamsize nWidth = lhs.width();
> char cFill = lhs.fill();
>
> lhs << std::hex << std::setfill('0') ;
> lhs << std::setw(8) << rhs.Data1 << "-"
> << std::setw(4) << rhs.Data2 << "-" << rhs.Data3 << "-";
> lhs << std::setw(2) << (unsigned long) rhs.Data4[0] << (unsigned
> long) rhs.Data4[1] << "-";
>
> for (int i = 2; i < 8; ++i)
> lhs << std::setw(2) << (unsigned long) rhs.Data4[i];
>
> lhs.flags(nFlags);
> lhs.width(nWidth);
> lhs.fill(cFill);
>
> return lhs;
> }

Just noticed, this doesn't put in bracketing '{' and '}'. If you
need them, that should be added.

e.g.
// This line before any of the other insertions above...
lhs << '{';

// This line after the insertions but before the cleanup...
lhs << '}';

For my purposes, I was generating __declspec(uuid) strings, so I
didn't want the braces.

Fernando Ortigosa

unread,
Jun 4, 2003, 12:32:00 PM6/4/03
to
Hehe, thanks again.

I have solved it at this time. But thank you very much for your time and for
your FAST answer.

Thanks a lot.

"Craig Powers" <eni...@hal-pc.org> escribió en el mensaje
news:3EDE1B90...@hal-pc.org...

CheckAbdoul

unread,
Jun 4, 2003, 12:33:57 PM6/4/03
to
Hi Fernando,

I do not think you need all the atl includes here. Just try

#include <objbase.h>

TCHAR* sGuid = { TEXT("{AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE}") };
CLSID clsId = {0};
CLSIDFromString(sGuid,&clsId);

--
Cheers
Check Abdoul [ VC++ MVP ]
-----------------------------------

"Fernando Ortigosa" <fernando...@e2000fi.com> wrote in message

news:OyRCDMrK...@TK2MSFTNGP12.phx.gbl...

Fernando Ortigosa

unread,
Jun 4, 2003, 12:44:04 PM6/4/03
to
Just tested...
The problem is that CLSIDFromString must receive a LPOLESTR as first
argument.
And I must convert my TCHAR to that thing...

Anyway. Could you tell me if I am including too much stuff in my project? =>
Thanks <=

"CheckAbdoul" <check...@nospam.mvps.org> escribió en el mensaje

news:#MoJNdrK...@TK2MSFTNGP11.phx.gbl...

Roy Fine

unread,
Jun 4, 2003, 1:46:49 PM6/4/03
to
Fernando

then you may want to use the _bstr_t helper class to get from the char * representation to a wide character format.

Something like this:

/* ****************************************** */
GUID myGuid;
char *strGUID[900];
GetGuidCharFromSomewhere(strGUID);
_bstr_t bstrBUID(strGUID);
CLSIDFromString ((wchar_t *)bstrGUID,&myGuid);
...
DoSomethingWithMyNewGuid(myGuid);
/* ****************************************** */


regards
roy fine


"Fernando Ortigosa" <fernando...@e2000fi.com> wrote in message news:ukQCeirK...@TK2MSFTNGP12.phx.gbl...

Craig Powers

unread,
Jun 4, 2003, 2:33:21 PM6/4/03
to
Fernando Ortigosa wrote:
>
> YES! It worked!
>
> Hmmm. Just Adding:
>
> USES_CONVERSION;
> CLSIDFromString(T2OLE(tchTypeCLSID),&Info.guid);
> where tchTypeCLSID is a TCHAR containing the CLSID string
> and Info is an structure (where I store some typelib info) whose guid member
> belongs to the GUID data type
>
> Apart of this I had to add:
> #include <atlbase.h>
> #include <atlcom.h>
> #include <atlconv.h>
> using namespace ATL;

You shouldn't need anything more than <atlconv.h> to use the conversion
macros. I don't think you even need the namespace -- since they're
macros, namespaces aren't an issue.

Fernando Ortigosa

unread,
Jun 5, 2003, 3:29:30 AM6/5/03
to
The <atlconv.h> include seems to not to be enough, because it seems to need
from macros defined in other .h files. But I bet you are right with the
namespace thing.

It is a bit of "bad work" from my part. As I didnt know which files I have
to include to "enable" that function, and it looked like ATL ( I have to
read several tutorials... :(, C++ has changed very much since my beloved 16
bits C++ ), so I created a new ATL project and copied the #include lines to
my current project.

I think that I will try with only <atlbase.h> and <atlconv.h>.

Thanks for the advice and the help.

"Craig Powers" <eni...@hal-pc.org> escribió en el mensaje
news:3EDE3B71...@hal-pc.org...

Fernando Ortigosa

unread,
Jun 5, 2003, 3:33:31 AM6/5/03
to
First of all. Thank you very much. I didnt know that there were that MANY
helping people here. And everybody looks to know infinite more things than I
do :(.

The thing you told me is a good thing to know, but it isnt my case. Anyway.
I do want to thank you for your interest and I will keep this on mind if I
ever need it (I bet I will).

Thanks

--
_____________________________

Fernando Jose Ortigosa Alvarez
<fernando...@e2000fi.com>

E2000 Financial Investments S.A.
Area de Servicios de Información
_____________________________

"Roy Fine" <John...@nomail.org> escribió en el mensaje
news:uIH$HFsKDH...@TK2MSFTNGP09.phx.gbl...

Fernando Ortigosa

unread,
Jun 5, 2003, 3:59:24 AM6/5/03
to
Yes, with <atlbase.h> and <atlconv.h> is far enough.

Thanks a lot!

"Fernando Ortigosa" <fernando...@e2000fi.com> escribió en el mensaje
news:##uNQRzKD...@TK2MSFTNGP11.phx.gbl...

0 new messages