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.
lbongsunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0
jkrCommented:
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

0
lbongsunAuthor 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?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Just to elaborate - '.c' files are compiled as plain 'C', where '::' would be syntactially incorrect.
0
lbongsunAuthor Commented:
That just worked fine!

Thanks a lot
0
jkrCommented:
You're most welcome ;o)
0
jkrCommented:
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
0
lbongsunAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.