I use a Microsoft example for a COM-Server: Out-of-process C# COM server (CSExeCOMServer)
A simple Console COM Client should just start up the Server and use its Methods, Properties and Events.
static void Main(string[] args)
{
Type type = Type.GetTypeFromCLSID(new Guid(SimpleObject.ClassId));
if (type == null)
{
throw new Exception("ClientObject component is not registered");
}
ISimpleObject comObject = (ISimpleObject)Activator.CreateInstance(type);
string what = comObject.HelloWorld();
float fvalue = (float) 2.22;
comObject.FloatProperty = fvalue;
fvalue = (float)3.33;
fvalue = comObject.FloatProperty;
}
This actually works directly. The server came up in 32bit due to its configured Platform target x86. But I have a 64bit OS and now I wanted to get it run in 64bit. The registration is done in post build event of the server. So I changed the registration (using the Framework64 regasm.exe) so that the server will be also registered in 64bit. Then I changed the Platform target to AnyCPU. If I start the client now it hangs in the line
ISimpleObject comObject = (ISimpleObject)Activator.CreateInstance(type);
But I can see in the task manager that the server was properly started up in 64bits. After a time of 1min or so I get the following Exception:
Retrieving the COM class factory for component with CLSID {DB9935C1-19C5-4ED2-ADD2-9A57E19F53A3} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at ConsoleClient.Program.Main(String[] args) in C:\Source\User\MaLe\Tests\CSExeCOMServer\C#\ConsoleClient\Program.cs:line 17 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
If I start the server exe by hand and try to connect to it by COM again I can see in the task manager that the Client starts up a second server rather than connecting to the first one. This one is indeed in 64bit but the exception occurs and after closing the client the second server closes as well. If I force the Server to be 32bit in AnyCPU with Prefere 32-bit flag everything works fine but in 32bit. If I set the platform target to x64 everything works fine also and in 64bit. So it doesnt work only in AnyCPU configuration if it starts up in 64bit.
Does anyone have an idea what I do wrong? I really need a COM-Server to run in AnyCPU configuration even with 64bit. I actually do not want to have seperate 32bit and 64bit exe's. Which worked also when I tested it by the way.
Thank you for your help.