Class not registered error from PHP

匆匆过客 提交于 2019-12-10 17:17:49

问题


We've created a C# class library assembly and made it COM visible to be able to call its methods from PHP. This used to work fine, but now we wanted to install it on a Windows Server 2008 server and we keep walking into the error "Class not registered".

To rule out any dependency problems I made a tiny little test class library in C#. The class library is built for Any CPU and it is COM visible (also set COMVisible to true in AssemblyInfo.cs). The test class library only contains one class with one method. The class is called TestLib and the namespace is also called TestLib. The method is called Test and only returns a string.

What we have done is the following: - built the TestLib.dll - copied it to the Windows Server 2008 machine - registered the dll with: regasm /codebase TestLib.dll - the regasm tool returns a success message - in PHP we simply try to create a new COM instance:

try
{
   $test = new COM("TestLib.TestLib");
}
catch (Exception $e)
{
   die($e->getMessage());
}
  • when we call this test script from either the browser or the commandline (php -f test.php) we get the error "Class not registered" in both cases

I also tried adding TestLib to the GAC by using gacutil -i, but to no avail; still the class not registered error.

Then I tried compiling the testlibrary with .NET 2.0 instead of 4.0 as the target framework, same result. The .NET framework 4.0 is installed on the server by the way.

Any ideas?


回答1:


Okay, so after some more research I figured it out. The php.exe process is 32 bit. The COM visible assembly is compiled for Any CPU so it should be accessible to both 32 and 64 bit applications.

The problem is that on a 64 bit OS php.exe, and any 32 bit process for that matter, searches in HKEY_CLASSES_ROOT\Wow6432Node\CLSID instead of HKEY_CLASSES_ROOT\CLSID and in HKEY_LOCAL_MACHINE\Software\Classes\Wow6432Node\CLSID instead of HKEY_LOCAL_MACHINE\Software\Classes\CLSID. The registry entries in the Wow6432 keys aren't created by regasm that is shipped with .NET framework v4 on Windows Server 2008. On Windows 7 they are created, don't ask me why.

It also turned out that if I create a little test assembly for .NET v2.0 and register it with regasm that ships with .NET framework v2.0 that it does create the Wow6432Node entries on Windows 2008. Strange.

So my solution is to create a basic registry file on the server using:

regasm /regfile MyClassLib.dll

This creates a file MyClassLib.reg with only the 'normal' 64 bit entries. Then I exported the Wow6432Node keys from a Windows 7 machine and added it to that .reg file. Now when I import that reg file into the registry on Windows 2008 everything works fine.

For more info on the Wow6432Node entries check out: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx

Hope this saves someone else some time and headaches.




回答2:


If you are trying to call a 32-bit COM DLL on 64-bit Windows, you will need to register it.

  1. Copy your 32-bit DLL to C:\Windows\sysWOW64
  2. Run C:\Windows\sysWOW64\regsvr32.exe your_com_32.dll

A bit more info with screenshots.



来源:https://stackoverflow.com/questions/10929925/class-not-registered-error-from-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!