Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using CoCreateGuid from Visual Basic

Posted on 1998-07-08
5
Medium Priority
?
1,193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 2

Author Comment

by:mtoft
ID: 1465228
Adjusted points to 250
0
 
LVL 1

Accepted Solution

by:
gissing earned 1000 total points
ID: 1465229
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
ID: 1465230
Impressive how one can "stretch" the API...

Thank you!
0
 
LVL 2

Author Comment

by:mtoft
ID: 1465231
Impressive how one can "stretch" the API...

Thank you!
0
 
LVL 1

Expert Comment

by:gissing
ID: 1465232
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

618 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