Calling a C # COM Object

Ok, I created C # dll, made all its interface and methods all ComVisible (true). Added it to the GAC using gacutil, then registered it using regasm and created a type library tlb file.

Now I have another C # project that I want to do to call this com object, how can I do this? What would it look like to import a COM object and then use its calls?

+2
source share
3 answers

First of all, why do you want to name this C # assembly (which you made comvisible) in another C # project through COM? It's not obligatory...

Ontopic: If you created a tlb file, then you should not do anything special. You can simply refer to the "shell designed to run" the C # assembly that you created.

+1
source

Step 1: Create a wrapper done at runtime. There are two ways:

Method 1: use TlbImp to generate RCW

tlbimp <YourCOMSvr>.tlb /out:<YourCOMSvr>.dll 

using this method, the standard .NET-Interop Marshalling is used (sometimes when it does not work) you need to change the marshalling by following the additional steps

 ildasm <YourCOMSvr>.dll /out:<YourCOMSvr>.il //Modify <YourCOMSvr>.il ilasm <YourCOMSvr>.il /dll 

Method 2. Manually creating a C ++ / Cli project serves as a wrapper for the COM server.

Step 2: C # Code

Link to RCW and use the following code to connect to the COM server

 Type yourComType= Type.GetTypeFromProgID(yourComSvrProgID, serverPcName); var oInterface = (IYourCOMInterface)Activator.CreateInstance(yourComType); //Then start using oInterface 
+1
source

The moment your C # component becomes a component corresponding to the COM class, it also starts behaving like a qualified ActiveX component.

Thus, an easy way to test COM calls is to write a simple HTML page with the JavaScript code below:

 Set MyCOMObj = Server.CreateObject("NAME_OF_COM_EXPOSED_CLASS"); MyCOMObj.My_COM_Method(); 

If you can call such methods without any errors, this means that your COM calls work perfectly.

Thanks and Regards
Anugra Atreya
http://explorecsharp.blogspot.com

-2
source

Source: https://habr.com/ru/post/1399902/


All Articles