OptionExplicit ' ' Brad Martinez, http://www.mvps.org ' Declare Function LoadTypeLib Lib "oleaut32" _ (ByVal szFileName AsString, _ lplptlib As Any) AsLong' lplptlib As Long Declare Function RegisterTypeLib Lib "oleaut32" _ (ByVal ptlib As Any, _ ByVal szFullPath AsString, _ ByVal szHelpDir AsString) AsLong Declare Function UnRegisterTypeLib Lib "oleaut32" _ (GUID As GUID, _ ByVal wVerMajor AsLong, _ ByVal wVerMinor AsLong, _ ByVal lcid AsLong, _ ByVal SYSKIND As SYSKIND) AsLong PublicConst S_OK =0' indicates successful HRESULT ' "Error accessing the OLE registry." Typically means that ' the GUID passed to UnRegisterTypeLib wasn't found in ' the registry (i.e the typelib was already unregistered) PublicConst TYPE_E_REGISTRYACCESS =&H8002801C Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength AsLong) Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags AsLong, ByVal uBytes AsLong) AsLong Declare Function LocalSize Lib "kernel32" (ByVal hMem AsLong) AsLong Declare Function LocalFree Lib "kernel32" (ByVal hMem AsLong) AsLong ' LocalAlloc uFlags values PublicConst LMEM_FIXED =&H0 PublicConst LMEM_ZEROINIT =&H40 PublicConst LPTR = (LMEM_FIXED Or LMEM_ZEROINIT) Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _ (ByVal dwFlags As FM_dwFlags, _ lpSource As Any, _ ByVal dwMessageId AsLong, _ ByVal dwLanguageId AsLong, _ ByVal lpBuffer AsString, _ ByVal nSize AsLong, _ Arguments As Any) AsLong Public Enum FM_dwFlags ' FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100 ' FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000 ' FORMAT_MESSAGE_FROM_HMODULE = &H800 ' FORMAT_MESSAGE_FROM_STRING = &H400 FORMAT_MESSAGE_FROM_SYSTEM =&H1000 FORMAT_MESSAGE_IGNORE_INSERTS =&H200 FORMAT_MESSAGE_MAX_WIDTH_MASK =&HFF End Enum ' FormatMessage dwLanguageId value PublicConst LANG_USER_DEFAULT =&H400& ' ' Registers the specified typelib. ' sTypelibPath - typelib's path, either explicit, or relative if the system can find it ' sHelpPath - typelib's help file path, should be explicit ' fSilent - specifies that a messagebox will not be shown indicating the result of the function ' Returns True on success, False otherwise. PublicFunction RegTypelib(sTypelibPath AsString, _ Optional sHelpPath AsString= vbNullChar, _ Optional fSilent AsBoolean=False) AsBoolean Dim hr AsLong ' Dim lptlb As Long Dim itlb As ITypeLib hr = LoadTypeLib(StrConv(sTypelibPath, vbUnicode), itlb) If (hr = S_OK) Then hr = RegisterTypeLib(itlb, StrConv(sTypelibPath, vbUnicode), _ StrConv(sHelpPath, vbUnicode)) EndIf If (fSilent =False) Then If (hr = S_OK) Then MsgBox"Successfully registered "& sTypelibPath RegTypelib =True Else MsgBox"Failed to register "& sTypelibPath & _ vbCrLf & vbCrLf & GetAPIErrStr(hr), vbExclamation EndIf EndIf EndFunction ' Unregisters the specified typelib. ' sTypelibPath - typelib's path, either explicit, or relative if the system can find it ' fSilent - specifies that a messagebox will not be shown indicating the result of the function ' Returns True on success, False otherwise. PublicFunction UnregTypelib(sTypelibPath AsString, _ Optional fSilent AsBoolean=False) AsBoolean Dim hr AsLong Dim itlb As ITypeLib Dim lptlba AsLong Dim tlba As TLIBATTR hr = LoadTypeLib(StrConv(sTypelibPath, vbUnicode), itlb) If (hr = S_OK) Then ' can't do this since VB DWORD aligns the struct !!! (it has 3 WORD members) ' If itlb.GetLibAttr(tlba) = S_OK Then ' allocate memory for the TLIBATTR struct lptlba = LocalAlloc(LPTR, Len(tlba)) hr = Err.LastDllError If lptlba Then ' Fill the struct's pointer hr = itlb.GetLibAttr(lptlba) If (hr = S_OK) Then ' Fill the struct from its pointer ' VB doesn't DWORD align the struct on this call... (?) MoveMemory tlba, ByVal lptlba, Len(tlba) ' Unregister the typelib using the info from the TLIBATTR struct With tlba hr = UnRegisterTypeLib(.GUID, .wMajorVerNum, .wMinorVerNum, .lcid, .SYSKIND) EndWith ' Don't do this since we're de-allocating ' below what we allocated above ' Call itlb.ReleaseTLibAttr(tlba) ' Set itlb = Nothing EndIf Call LocalFree(lptlba) EndIf' lptlba EndIf' LoadTypeLib If (fSilent =False) Then If (hr = S_OK) Then MsgBox"Successfully unregistered "& sTypelibPath UnregTypelib =True ElseIf (hr = TYPE_E_REGISTRYACCESS) Then MsgBox"Type library is not registered: "& sTypelibPath Else MsgBox"Failed to unregister "& sTypelibPath & _ vbCrLf & vbCrLf & GetAPIErrStr(hr), vbExclamation EndIf EndIf UnregTypelib = (hr = S_OK) EndFunction ' Returns the system-defined description of an API error code PublicFunction GetAPIErrStr(dwErrCode AsLong) AsString Dim sErrDesc AsString*256' max string resource len If FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or _ FORMAT_MESSAGE_IGNORE_INSERTS Or _ FORMAT_MESSAGE_MAX_WIDTH_MASK, _ ByVal 0&, dwErrCode, LANG_USER_DEFAULT, _ ByVal sErrDesc, 256, 0) Then GetAPIErrStr =Left$(sErrDesc, InStr(sErrDesc, vbNullChar) -1) EndIf EndFunction