COM with .NET interoperability (primary)
COM and .NET calls COM components from .NET, if you use vs.net, you will be very easy, you only need to add the corresponding COM reference, the compilation tool is quietly put in the background. "Change" .NET assembly. And from traditional language call calls. NET components are not as convenient. Therefore, I have organized a few procedures for the success of personal debugging hope to have some help to everyone, and there is a nonsense less to enter the topic.
First, call the .NET component from script and other scripts such as VBScript
First we can write a .NET DLL as follows
// the first file: netserver.cs
Using system;
Using system.reflection;
Using system.Runtime.InteropServices;
[Assembly: AssemblyKeyFile ("key.snk")]]]
Namespace CSHARPSERVER
{
/ / The default is ClassInterFaceType.autodispatch, which only generates the Dispatch interface in this mode.
// can only be used with COM customers using Script, VB, etc.
[ClassInterFacettribute (ClassInterFactype.Autodu)]
Public Class Sharpobject
{
PRIVATE STRING M_STRNAME;
Public Sharpobject () {}
Public String Name // Property: Name, Get / Set
{
Get {return m_strname;}
Set {m_strname = value;
}
}
}
// the second file: Test.vbs
DIM OBJ
Set obj = creteObject ("csharpserver.sharpobject")
Obj.name = "chang ming"
Msgbox "My Name IS" & obj.name
Compile these two files as follows, in order to clearly use the command line tool (command line tool environment can be from the beginning -> Microsoft Visual Studio .NET -> Visual Studio .NET Tools -> Visual Studio .NET Command Enter the prompt)
1, generate key files to sign the assembly strong name
Sn -k key.snk
2, use strong name signature, compile become a class library,
CSC / T: Library NetServer.cs
3, generate type library
TLBEXP NetServer.dll /out:netServer.tlb
4, register DLL
Regasm netserver.dll
5, move into the GAC global assembly cache
Gacutil -i NetServer.dll
6, call the test script
WScript Test.vbs
There are several places that need attention here, 1, must be signed to the assembly, let it have a strong name. 2, you must use the REGASM registration assembly, which will add the appropriate items in the registry. 3, you must move the signed Strong Name Program Set into the global assembly cache (GAC). 4, you must install Scriptengine first, Microsoft's script execution engine. This is a set of script calls .NET assembly, huh, is it simple? J
Second, call the .NET component from C / C
Still a program, huh, huh, the program is my life:) // file1 name: netserver.cs
Using system;
Using system.reflection;
Using system.Runtime.InteropServices;
[Assembly: AssemblyKeyFile ("key.snk")]]]
Namespace CSHARPSERVER
{
Public interface IObject // Declaration Interface
{
Double Sub (Double C, Double D);
}
//[Classinterfacettribute (ClassInterFaceType.Autodu)]
Public Class Sharpobject: IOBJECT
{
PRIVATE STRING M_STRNAME;
Public Sharpobject () {}
Public String Name // Property: Name, Get / Set
{
Get {return m_strname;}
Set {m_strname = value;
}
Public Double Add (Double A, Double B)
{
Console.WriteLine ("The Answer IS {0}", A B);
RETURN A B;
}
Public Double Sub (Double C, Double D) / / Implementing Interface Method
{
Console.writeline ("THE ANSWER IS {0}", C-D);
Return C-D;
}
}
}
// file2 name: comclient.cpp
#include
#include
#include
#pragma Warning (Disable: 4278)
#import "netserver.tlb" NO_NAMESPACE NAMED_GUIDS
Int main (int Argc, char * argv [])
{
IOBJECT * CPI = NULL;
int RetVal = 1;
// Initialize COM and Create An Instance of The InterfaceImplement Class:
Coinitialize (NULL);
HRESULT HR = CocreateInstance (CLSID_SHARPOBJECT,
NULL,
CLSCTX_INPROC_SERVER,
IID_IOBJECT,
Reinterpret_cast
IF (Failed (HR))
{
Printf ("COULDN '' '' x x", hr);
}
Else
{
Printf ("Calling function./n");
Retval = 0;
COUT << "10-4 =" << CPI-> SUB (10, 4) << ENDL;
Printf ("Returned from function./N");
CPI-> Release (); // Release COM object
}
// be a good citizen and clean up COM:
Couninitialize ();
Return RetVal;
}
The compilation method is still as before 1, generates a key file, used to sign the assembly strong name
Sn -k key.snk
2, use strong name signature, compile become a class library,
CSC / T: Library NetServer.cs
3, generate type library // This step is very important
TLBEXP NetServer.dll /out:netServer.tlb
4, register DLL
Regasm netserver.dll
5, move into the GAC global assembly cache
Gacutil -i NetServer.dll
6, compile test procedure
CL Comclient.cpp
7, execute Comclient.exe
Description:
Calling COM in C / C is invoking some, first call the COM library function Coinitialize (null); initialization, then call HRESULT HR = CocreateInstance (CLSID_SHARPOBJECT,
NULL,
CLSCTX_INPROC_SERVER,
IID_IOBJECT,
Reinterpret_cast
Where CLSID_SHARPOBJECT is the class ID of the Sharpobject class (COM class) It is used by the tool to uniquely identify the SharPObject class, IID_IOBJECT uniquely identifies the IObject interface, if CoCreateInstance creates a COM object, then Failed (HR) will be false, get it Once the COM object is pointed, it can be used to call the method in the COM object.
Welcome to COM and .NET brothers and sisters and I discuss, msn: tlping@msn.com