M
M
MaxLich2017-11-13 17:36:07
Component Object Model
MaxLich, 2017-11-13 17:36:07

How to create a COM object in a C# program?

Hello. I'm a javaist, and in Sharp neither ear nor snout, but I need to create a com object on Sea Sharp. The object is used to communicate with equipment. Hardware developers provide a dll-library, I registered it. It remains to get it from the program itself, and then work with it. The manual says this:


To register a COM object, do the following:
1. Register PERCo_S20_SDK.DLL using regsvr32.exe (regsvr32.exe PERCo_S20_SDK.dll).
2. In the software that works with the S-20, create a COM object: CreateOleObject('PERCo_S20_SDK.ExchangeMain') or CreateComObject('{E74FA501-350F-43CF-8C15-D831778FD465}').
3. For further work, use the methods of the created COM object.

I can't figure out how to implement the second point. Can anyone elaborate for a newbie!?
UPD: I found an example on the sharp for working with this equipment, here is the link: www.cyberforum.ru/csharp-net/thread1578682.html
A piece of code from there:
CoExchangeMain oPERCo = new CoExchangeMain();
 int iRet = oPERCo.SetConnect(Server, Port, Login, Passwd);
 if (iRet != 0)
 {
    throw new ApplicationException("Ошибка подключения");
 } else {
    Console.WriteLine("Успешно подключились");
    bConnected = true;
 }

That is, here the first line creates the object I need!?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cicatrix, 2017-11-13
@MaxLich

It's called Runtime Callable Wrapper or RCW.
In most cases, after registering a COM component (number 1 on your list), it's enough to simply add a reference to this component (Add Reference) to the project and import the namespace.
From myself, I’ll just add that working with COM can lead to memory leaks - the garbage collector does not always cope there, so clean up the memory after yourself after work.
"Under the hood", if on the fingers, then the CLR will generate a "wrapper" of your COM object (wrapper assembly), with which you will work. No CreateObject needs to be done (this is more relevant for PowerShell or VBS).

D
d-stream, 2017-11-13
@d-stream

Well, it’s more likely that a more correct term cannot be created here - because it confuses and at first provokes thoughts in the direction of "how to write ocx in c#" -))
And actually:
public PERCo_S20_SDK myobject = new PERCo_S20_SDK();
and continue to use

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question