Link to home
Start Free TrialLog in
Avatar of lbongsun
lbongsun

asked on

how to get GUID in Visual C++ program

I'm using Visual Studio 2005 and programming to generate some XML file using C++. This XML file needs GUID, so I used following code to generate it:

GUID guid;
::CoInitialize(0);
::CoCreateGuid(&guid);

Then the compiler gave me following error:

error C2143: syntax error :: missing ';' before ':'

Any help is appreciated.
Avatar of jkr
jkr
Flag of Germany image

Are you sure you got the right headers? The following compiles fine:


#include <objbase.h>
 
#pragma comment(lib,"ole32.lib")
 
void main () {
 
  GUID guid;
  ::CoInitialize(0);
  ::CoCreateGuid(&guid);
 
}

Open in new window

Or, a complete example along with formatting and output:
#include <objbase.h>
#include <stdio.h>
 
#pragma comment(lib,"ole32.lib")
 
void PrintGUID(GUID& guid) {
 
  printf("{%08lX-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}\r\n",
		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]);
}
 
void main () {
 
  GUID guid;
  ::CoInitialize(0);
  ::CoCreateGuid(&guid);
 
  PrintGUID(guid);
 
  CoUninitialize();
}

Open in new window

Avatar of lbongsun
lbongsun

ASKER

Thanks for the solution. But, with your code, I'm still having the same error message. Does this mean the compiler cannot find the ole32.lib?
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just to elaborate - '.c' files are compiled as plain 'C', where '::' would be syntactially incorrect.
That just worked fine!

Thanks a lot
You're most welcome ;o)
BTW, as I see you are new to EE - you should close your question once your problem is resolved. See https://www.experts-exchange.com/Community_Support/help.jsp
Thanks!