I followed the steps mentioned in the article,
Created a 64 bit COM DLL (MyCOMdll.dll) and created a Class COMServer as below,
namespace MyCOMdll
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("SOMGUID")]
public class COMServer
{
public ComServer() { }
public void TestMethod()
{
MathClass mathObj = new MathClass();
mathObj.Calc();
}
}
}
Then registered MyCOMdll.dll using the 64bit version of Regasm using below command,
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe MyCOMdll.dll /codebase
Then added the below registry entries as described in the above article,
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\AppID\{GUIDOFCOMSERVER}]
"DllSurrogate"=""
[HKEY_CLASSES_ROOT\CLSID\{GUIDOFCOMSERVER}]
"AppID"="{GUIDOFCOMSERVER}"
Then from a 32 bit Console application calling the TestMethod as below,
// Access COM Object through registered Class Id
Type ComType = Type.GetTypeFromProgID("MyCOMdll.ComServer");
// Create an instance of the COM object
// This will invoke the default constructor of class ComServer
object ComObject = Activator.CreateInstance(ComType);
// Calling the Method "TestMethod" from 64-Bit COM server
ComType.InvokeMember("TestMethod", BindingFlags.InvokeMethod, null, ComObject, null);
This client code works fine without any issue when it is used from 32 bit console application. I tried to use this same code from a 32 bit windows service then it fails with exception in this line,
MathClass mathObj = new MathClass();
Is there any special settings to be done when a 64 bit outproc surrogate dll is consumed from Windows Service?
The dependencies of MathClass.dll was missed in the path. Now I have added the directory path (where all the dependencies are available) to PATH variable, it started working. I am not sure why this issue didn't come for console application.