Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > API and Miscellaneous Code Examples

To convert a Class ID to a prog ID and back use the following routines

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
To convert a Class ID to a prog ID and back use the following routines Note, a demonstration routine can be found at the bottom of this post: Private Declare Function ProgIDFromCLSID Lib "ole32.dll" (CLSID As Any, lProgID As Long) As Long Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lProgID As Long, CLSID As Any) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (vDest As Any, vSource As Any, ByVal lBytes As Long) Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal lpszProgID As Long, pCLSID As Any) As Long Private Declare Function StringFromCLSID Lib "ole32.dll" (pCLSID As Any, lpszProgID As Long) As Long Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long) 'Purpose : Converts a string Class ID (inc. braces) into as string Prog ID 'Inputs : sCLSID The Class ID to find the Prog ID of. 'Outputs : Returns the Prog ID as a string 'Notes : 'Revisions : Function CLSIDToProgID(ByVal sCLSID As String) As String Dim lFirstChar As Long, lLastChar As Long, iChar As Integer Dim abGuid(15) As Byte 'Convert string to a binary CLSIDFromString StrPtr(sCLSID), abGuid(0) 'Convert to a string and get pointer to result ProgIDFromCLSID abGuid(0), lFirstChar lLastChar = lFirstChar 'Find end of string Do CopyMemory iChar, ByVal lLastChar, 2 If iChar Then lLastChar = lLastChar + 2 End If Loop While iChar CLSIDToProgID = Space$((lLastChar - lFirstChar) \ 2) CopyMemory ByVal StrPtr(CLSIDToProgID), ByVal lFirstChar, (lLastChar - lFirstChar) End Function 'Purpose : Converts a string Prog ID (inc. braces) into as string Class ID 'Inputs : sProgID The Prog ID to find the Class ID of. 'Outputs : Returns the Class ID as a string 'Notes : 'Revisions : Function ProgIDToCLSID(ByVal sProgID As String) As String Dim lFirstChar As Long, lLastChar As Long Dim iChar As Integer Dim abGuid(15) As Byte 'Get CLSID CLSIDFromProgID StrPtr(sProgID), abGuid(0) 'Convert to a string and get pointer to result StringFromCLSID abGuid(0), lFirstChar 'Find end of string lLastChar = lFirstChar Do CopyMemory iChar, ByVal lLastChar, 2 If iChar Then lLastChar = lLastChar + 2 End If Loop While iChar ProgIDToCLSID = Space$((lLastChar - lFirstChar) \ 2) CopyMemory ByVal StrPtr(ProgIDToCLSID), ByVal lFirstChar, (lLastChar - lFirstChar) End Function 'Demonstration routine Sub Test() Dim sProgID 'Get the Prog ID for Excel sProgID = CLSIDToProgID("{00024500-0000-0000-C000-000000000046}") Debug.Print sProgID 'Get the Class ID for Excel (as above) Debug.Print ProgIDToCLSID(sProgID) End Sub