I have a very old (VC++ 5.0) proprietary DLL which I need to use from C# (Visual Studio 2010). The example specifies that to access this component I need to call CreateDispatch("application")
which indicates towards OLE.
The following is the example 开发者_JS百科code (C++):
IComponentServer Server;
Server.CreateDispatch("Component.Server");
I added a Reference through Visual Studio to the TLB file I have, and I can import its namespace successfully, but IComponentServer
does not have any method called CreateDispatch
.
What is the right approach to create the instance of an OLE component through C#?
If you have either the CLSID or ProgID you can use the following set of methods.
var type = Type.GetTypeFromProgID(progIdString);
var obj = Activator.CreateInstance(type);
var server = (IComponentServer)obj;
MFC's CreateDispatch
creates COM objects based on a CLSID or ProgId string. You can instantiate COM objects directly from C# code.
Assuming the Visual Studio reference gives you Interop.Component.dll:
IComponentServer server = new Interop.Component.ServerClass();
精彩评论