Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate COM Component from C#
#1
I used the following C# code to generate a COM component, and then created a COM object in QM,
but I was unable to use the dot notation to display the object's member methods.
demo: https://i.ibb.co/nsXN810z/mycom.gif

What did I do wrong?

Thank you in advance for any suggestions and help.

Function Com_Demo_T
Code:
Copy      Help
out

__ComActivator ca.Activate("MyCom.X.manifest")
IDispatch mc._create("ComDemo.MyComClass")

Function Com_Demo
Code:
Copy      Help
CsScript x.Compile("" "$qm$\MyCom.dll")
out "MyCom.dll is OK!"

1

__ComActivator_CreateManifest "MyCom.dll" 1
out "MyCom.X.manifest is OK!"

#ret
using System;
using System.Runtime.InteropServices;

namespace ComDemo
{
,// Define interface
,[ComVisible(true)]
,[InterfaceType(ComInterfaceType.InterfaceIsDual)]
,[Guid("12345678-1234-4321-ABCD-1234567890AB")]
,public interface IMyComClass
,{
,,int Add(int a, int b);
,,string Hello(string name);
,}

,// Implement COM class
,[ComVisible(true)]
,[ClassInterface(ClassInterfaceType.None)]
,[Guid("ABCD1234-5678-4321-ABCD-1234567890AB")]
,public class MyComClass : IMyComClass
,{
,,public int Add(int a, int b)
,,{
,,,return a + b;
,,}

,,public string Hello(string name)
,,{
,,,return $"Hello, {name}!";
,,}
,}
}

My idea is to create the class object in the qm main function, and then use the class object's member functions directly in other functions, so that the COM component is only called once. Is this possible?
#2
It is the main purpose of IDispatch. To call functions that are unknow at design/compile time. I did not test, but you can call these functions even if they are not in the list. To add to the list and make calling faster, define the interface in QM code too. Examples in CsScript folder.
#3
Thanks for your help.
It seems that I also need to define the interface again in QM.
With the following code, the member functions can be displayed using the dot notation. However, I want to be able to call the function using "mc.Add" in any QM macro. Is there anything else I need to do to achieve this?

Function Com_Demo_T2
Code:
Copy      Help
interface# IMyComClass :IDispatch
,#Add(a b)
,{12345678-1234-4321-ABCD-1234567890AB}
CsScript x.Load("$qm$\MyCom.dll")
IMyComClass mc=x.CreateObject("ComDemo.MyComClass")


out mc.Add(7,8)

My ultimate goal is to be able to use the loaded class and its functions in any QM macro or function, and for it to continue working even after generating an EXE. Is this possible?

The reason for this idea is that I have too many C# code functions. If I add each function as a separate DLL resource, I have to load each DLL file separately for every function call, and then invoke it, which results in high resource overhead. Therefore, I want to generate all these C# functions into a single DLL, and then execute them using the method mentioned above. This would be much more convenient.
#4
I don't remember many QM features and cannot help much.

Define the interface in some function. Elsewhere:
#compile TheFunction
//and use the interface

Did you measure the Load overhead? If it is slow etc, use a global variable to call it once.
#5
Thank you for the clues you provided, they are very helpful.


Forum Jump:


Users browsing this thread: 1 Guest(s)