1
votes

I had ever used the following function to register my 32-bit COM/DLL components on 32-bit Windows platform such as Window XP, and Windows 7. It works fine. But when running on 64-bit Windows 7, the function can't register successfully. The result is as expected!

int RegisterComponent(LPCTSTR lpszDllName)
{   
    // Load the library
    HINSTANCE hLib = LoadLibrary(lpszDllName);

    if (hLib == NULL)
    {  
       return -2;            
    }

    typedef HRESULT (CALLBACK *HCRET)(void);
    HCRET lpfnDllRegisterServer;

    // Find the entry point
    lpfnDllRegisterServer = (HCRET)GetProcAddress(hLib, "DllRegisterServer");

    if (lpfnDllRegisterServer == NULL)
    {  
       return -3;            
    }

    // Call the function by function pointer..
    if (FAILED((*lpfnDllRegisterServer)()))            
    {   
       //DLL Registration failed..
       return -4;            
    }

    FreeLibrary(hLib);

    return 0;
}

I can use the following command to manually register the 32-bit COM componet: C:\Windows\SysWOW64>regsvr32

How to smartly register a 32-bit COM/DLL component programmatically on different targeting platforms (for example, X86 and X64)?

int RegisterComponent(LPCTSTR lpszDllName)
{  
    if ( IsWow64() )
    {  
       ...
    }
    else
    {  
       ... 
    }

    return 0;
}

Thank you very much!

1

1 Answers

5
votes

32-bit DLLs can be loaded into 32-bit process, and 64-bit DLLs can be loaded into 64-bit process. Your code (continuous) can be either 32 or 64 bit. That is, you cannot register a DLL of different bitness without starting proper bitness process.

You have choices there:

  1. Do what regsvr32 does. Have your code in both 32 and 64 bit binaries, and once you detect wrong bitness of the DLL, run a child helper process to take care of that DLL.

  2. Just let regsvr32 do it for you, CreateProcess "regsvr32 the-DLL /s" and it will do registration, or start another child of its own to register different bitness.

  3. (Trivial) have installer do registrations, and those are already aware how to deal with bitnesses.