Solved

Using CoCreateGuid from Visual Basic

Posted on 1998-07-08
5
938 Views
Last Modified: 2013-11-25
Hello...

I would like to create and work with GUID's from visual basic. I would like to call CoCreateGuid, and receive a pointer to a UUID, however this seems a bit tricky since the predefined UUID uses unsupported variant types (unsigned long is unsupported?)

If you could provide me with an example of creating a new GUID and converting it to a string, then i'd be happy...

(I could create a DLL in c/c++ that did the job, but if its possible from VB, then i'd prefer that).

Have a nice day, and thank you!
0
Comment
Question by:mtoft
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:mtoft
Comment Utility
Adjusted points to 250
0
 
LVL 1

Accepted Solution

by:
gissing earned 250 total points
Comment Utility
The declaration you need is:

Declare Function CoCreateGuid Lib "ole32.dll" (ByVal pguid As String) As Long

call it from code like this:

Dim hResult As Long
Dim sGuidBuf As String

    sGuidBuf = String(16, 0)
    hResult = CoCreateGuid(sGuidBuf)

If hResult = 0 then your sGuidBuf string buffer contains the 16 byte values of the GUID. The guid structure is a long, two unsigned shorts and eight chars (which gives the 16 bytes). In vb, put the numerics into longs since vb doesn't support unsigned types. Combine that with the Hex$ function, and you can translate the 16-bytes in your buffer into a more familiar looking GUID.
0
 
LVL 2

Author Comment

by:mtoft
Comment Utility
Impressive how one can "stretch" the API...

Thank you!
0
 
LVL 2

Author Comment

by:mtoft
Comment Utility
Impressive how one can "stretch" the API...

Thank you!
0
 
LVL 1

Expert Comment

by:gissing
Comment Utility
You're welcome.

Having giving it further thought, you don't have to put the byte values into longs before calling Hex$.

For example, try this:

Dim sMyGuid As String

    sMyGud = ""
    sMyGud = sMyGuid & Hex$(Asc(Mid$(sGuidBuf,4,1)))) & Hex$(Asc(Mid$(sGuidBuf,3,1))))& Hex$(Asc(Mid$(sGuidBuf,2,1))))& Hex$(Asc(Mid$(sGuidBuf,1,1))))

which will start you off with the first long in the guid structure as Hex. (remember the bytes come at you in the buffer with the least significant byte first)

Glad to have been of some help...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now