Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use CreateObject given CLSID

Status
Not open for further replies.

DevonTaig

Programmer
May 2, 2001
73
US
How can I create an instance of an object given its CLSID such as "{D9C78005-E187-4781-BA3C-55892C5922FB}
 
You need to convert the given CLSID into a ProgID and then create the object subsequently. There are a few OLE APIs which do the same. See the following code.
___
[tt]
Option Explicit
Private Declare Function ProgIDFromCLSID Lib "ole32" (pCLSID As Any, lpszProgID As Long) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpszProgID As Long, pCLSID As Any) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyW" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long

Function CLSIDToProgID(ByVal CLSID As String) As String
Dim guid(15) As Byte, pResult As Long
CLSIDFromString StrPtr(CLSID), guid(0)
ProgIDFromCLSID guid(0), pResult
CLSIDToProgID = Space$(lstrlen(pResult))
lstrcpy ByVal StrPtr(CLSIDToProgID), pResult
End Function

Private Sub Form_Load()
Dim CLSID As String, ProgID As String, obj As Object
CLSID = "{72C24DD5-D70A-438B-8A42-98424B88AFB8}" 'WScript.Shell
ProgID = CLSIDToProgID(CLSID)
Set obj = CreateObject(ProgID)
MsgBox obj.currentdirectory
Unload Me
End Sub[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top