We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

how to get GUID in Visual C++ program

lbongsun
lbongsun asked
on
Medium Priority
1,238 Views
Last Modified: 2012-05-06
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.
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2012
Commented:
Are you using the exact snippet from above? That's odd. BTW, are you using a source file with a '.c' extension? In that case, remove the '::' operators and make that
#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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Just to elaborate - '.c' files are compiled as plain 'C', where '::' would be syntactially incorrect.

Author

Commented:
That just worked fine!

Thanks a lot
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You're most welcome ;o)
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
BTW, as I see you are new to EE - you should close your question once your problem is resolved. See http://www.experts-exchange.com/Community_Support/help.jsp

Author

Commented:
Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.