Debugging COM DLL registration problems

Regsvr32.exe often times does not give very helpful error messages when it encounters a problem registering a COM DLL. If you’ve ever encountered: ‘Dllname is not an executable file and no registration helper is registered for this file type.‘, you know what I’m talking about. What Regsvr32 is really trying to say is that it encountered a problem when trying to invoke either LoadLibrary() on the DLL or a problem when calling GetProcAddress() on the DllRegisterServer entry point. Problems with LoadLibrary() usually stem from a DLL that this DLL depends on being missing, or having an error during its own initialisation. GetProcAddress() problems usually occur when you forget to export the DllRegisterServer entry point.

Jeff Abraham points out an approach for debugging such problems, create a skeleton application to perform the steps Regsvr32 performs and step through the process to see where things go wrong. Below is my version of this skeleton. As Jeff points out, this problem manifests itself during Windows Mobile deployment with the error message: ‘Registration failed with error 0x8973190e

#include <windows.h> 
#include <tchar.h> 

typedef HRESULT (*DllReg)(void); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
	DWORD err = 0; 
	LPCWSTR dll = argv[1]; 

 	HMODULE module = LoadLibrary(dll); 
	if ( module == NULL ) { 
		err = GetLastError(); 
	} else { 
		DllReg dllReg = (DllReg)GetProcAddress(module,"DllRegisterServer"); 
		if ( dllReg == NULL ) { 
			err = GetLastError(); 
		} else { 
			dllReg(); 
		} 
	} 
	if ( err ) { 
		LPWSTR msg; 
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,0,err,0,(LPWSTR)&msg,0,NULL); 
		wprintf(msg); 
		LocalFree(msg); 
	} 
	return err; 
}
Advertisements