[ACCEPTED]-Create Out-Of-Process COM in C#/.Net?-com
We too had some issues many years ago with 42 regasm and running the COM class as a Local 41 EXE Server.
This is a bit of a hack and I'd 40 welcome any suggestions to make it more 39 elegant. It was implemented for a project 38 back in the .NET 1.0 days and has not been 37 touched since then!
Basically it performs 36 a regasm style of registration each time 35 the application starts (it needs to be run 34 once to make registry entries before the 33 COM object is instantiated in the COM container 32 application).
I've copied the following important 31 bits from our implementation and renamed 30 a few classes to illustrate the example.
The 29 following method is called from the Form 28 Loaded event to register the COM class(renamed 27 to MyCOMClass
for this example)
private void InitialiseCOM()
{
System.Runtime.InteropServices.RegistrationServices services = new System.Runtime.InteropServices.RegistrationServices();
try
{
System.Reflection.Assembly ass = Assembly.GetExecutingAssembly();
services.RegisterAssembly(ass, System.Runtime.InteropServices.AssemblyRegistrationFlags.SetCodeBase);
Type t = typeof(MyCOMClass);
try
{
Registry.ClassesRoot.DeleteSubKeyTree("CLSID\\{" + t.GUID.ToString() + "}\\InprocServer32");
}
catch(Exception E)
{
Log.WriteLine(E.Message);
}
System.Guid GUID = t.GUID;
services.RegisterTypeForComClients(t, ref GUID );
}
catch ( Exception e )
{
throw new Exception( "Failed to initialise COM Server", e );
}
}
For the type in question, MyCOMObject
, will 26 need some special attributes to be COM compatible. One 25 important attribute is to specify a fixed 24 GUID otherwise each time you compile the 23 registry will fill up with orphaned COM 22 GUIDs. You can use the Tools menu in VisualStudio 21 to create you a unique GUID.
[GuidAttribute("D26278EA-A7D0-4580-A48F-353D1E455E50"),
ProgIdAttribute("My PROGID"),
ComVisible(true),
Serializable]
public class MyCOMClass : IAlreadyRegisteredCOMInterface
{
public void MyMethod()
{
}
[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
AttributeCollection attributes = TypeDescriptor.GetAttributes(t);
ProgIdAttribute ProgIdAttr = attributes[typeof(ProgIdAttribute)] as ProgIdAttribute;
string ProgId = ProgIdAttr != null ? ProgIdAttr.Value : t.FullName;
GuidAttribute GUIDAttr = attributes[typeof(GuidAttribute)] as GuidAttribute;
string GUID = "{" + GUIDAttr.Value + "}";
RegistryKey localServer32 = Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}\\LocalServer32", GUID));
localServer32.SetValue(null, t.Module.FullyQualifiedName);
RegistryKey CLSIDProgID = Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}\\ProgId", GUID));
CLSIDProgID.SetValue(null, ProgId);
RegistryKey ProgIDCLSID = Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}", ProgId));
ProgIDCLSID.SetValue(null, GUID);
//Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}\\Implemented Categories\\{{63D5F432-CFE4-11D1-B2C8-0060083BA1FB}}", GUID));
//Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}\\Implemented Categories\\{{63D5F430-CFE4-11d1-B2C8-0060083BA1FB}}", GUID));
//Registry.ClassesRoot.CreateSubKey(String.Format("CLSID\\{0}\\Implemented Categories\\{{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}}", GUID));
}
[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
AttributeCollection attributes = TypeDescriptor.GetAttributes(t);
ProgIdAttribute ProgIdAttr = attributes[typeof(ProgIdAttribute)] as ProgIdAttribute;
string ProgId = ProgIdAttr != null ? ProgIdAttr.Value : t.FullName;
Registry.ClassesRoot.DeleteSubKeyTree("CLSID\\{" + t.GUID + "}");
Registry.ClassesRoot.DeleteSubKeyTree("CLSID\\" + ProgId);
}
}
The InitialiseCOM
method 20 in the main form uses RegistrationServices
to register the type. The 19 framework then uses reflection to find the 18 method marked with the ComRegisterFunction
attribute and calls 17 that function with the type being registered.
The 16 ComRegisterFunction
marked method, hand creates the registry 15 settings for a Local EXE Server COM object 14 and this can be compared with regasm if 13 you use REGEDIT
and find the keys in question.
I've 12 commented out the three \\Registry.ClassesRoot.CreateSubKey
method calls as 11 this was another reason we needed to register 10 the type ourselves as this was an OPC server 9 and third party OPC clients use these implemented 8 categories to scan for compatible OPC servers. REGASM 7 would not add these in for us unless we 6 did the work ourselves.
You can easily see 5 how this works if you put break points on 4 the functions as it is starting.
Our implementation 3 used an interface that was already registered 2 with COM. For your application you will 1 either need to :-
- Extend the registration methods listed above to register the interface with COM
- Or create a separate DLL with the interface definition and then export that interface definition to a type library and register that as discussed in the StackOverflow link you added in the question.
The official answer is in KB article How to develop an out-of-process COM component by using Visual C++, Visual C#, or Visual Basic .NET.
0
IMO, one of the ways through which this 10 can be done is to create a normal COM Dll 9 as per the method you mentioned in the link 8 and then after registering your COM dll, change 7 it to a surrogate DLL. This can be done 6 very easily through OLEView utility, although 5 you can do it manually as well by changing 4 registry entries as well through the method 3 mentioned at http://msdn.microsoft.com/en-us/library/ms686606(VS.85).aspx.
By making this a surrogate 2 DLL, it will run in it's own dllhost.exe 1 and hence will be out-of-process.
ActiveX.NET, is a true Out-Of-Proc (EXE) COM Server 8 implementation in C#.NET. This one has a 7 cleaner implementation compared to the original 6 CSExeCOMServer, published in Code.MSDN.
ActiveX.NET has features 5 like it does use a .NET Message Pump (instead 4 of native) and uses MEF Plugin model, so 3 that the EXE Server is decoupled and can 2 be shared among multiple COM Plugins, which 1 can be developed independently
One option is serviced components - i.e. host it in COM+ as 1 the shell exe. See also the howto here.
You can use RegistrationServices.RegisterTypeForComClients, which 2 is the managed equivalent of CoRegisterClassObject 1 - for sample code see here.
It could be done using umanaged ATL framework 6 and plumbing it with the managed code (simple 5 by changing the result project properties 4 to /clr).
Here are illustrative snippets:
.H-part:
\#include < vcclr.h >
\#using < MyCSharpModule.dll >
using namespace System;
class ATL_NO_VTABLE MyCSharpProxyServer :
public CComObjectRootEx< CComMultiThreadModel >,
.....
{
HRESULT FinalConstruct();
STDMETHODIMP LoadMyFile(BSTR FileName);
.....
gcroot<MyCSNamespace::MyCSharpClass^> m_CSClass;
}
.CPP-part:
using namespace System::Collections;
using namespace MyCSNamespace;
HRESULT MyCSharpProxyServer::FinalConstruct()
{
m_CSClass = gcnew MyCSharpClass();
}
STDMETHODIMP MyCSharpProxyServer::LoadMyFile(BSTR FileName)
{
try {
int hr = m_CSClass->LoadFile(gcnew String( FileName));
return hr;
}
catch( Exception^ e ) {
return E_FAIL;
}
}
The 3 C# part (MyCSharpClass class) lives in a 2 separate project with output type Class 1 Library.
The visual studio project "CSExeCOMServer" that 2 you can find here (All-in-One), gives a full 1 example.
Use the flag REGCLS_MULTIPLEUSE with CoRegisterClassObject() to 2 make the coclass a multiuse class. Here 1 is more info: http://support.microsoft.com/kb/169321.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.