Answer the question
In order to leave comments, you need to log in
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.
CoExchangeMain oPERCo = new CoExchangeMain();
int iRet = oPERCo.SetConnect(Server, Port, Login, Passwd);
if (iRet != 0)
{
throw new ApplicationException("Ошибка подключения");
} else {
Console.WriteLine("Успешно подключились");
bConnected = true;
}
Answer the question
In order to leave comments, you need to log in
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).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question