COM objects have been the basis of Windows Programming for many years. It is inevitable that the existing COM objects still need to be used during the transition to. net. "Interoperability encapsulation processing" is a packaging process. When you move parameters and return values to or from a COM object, in this process, these parameters and returned values are packaged as equivalent data types.
The Common Language Runtime Library exposes COM objects by calling the runtime callable wrapper (RCW) proxy. Although RCW seems to be a common object in the. NET client, its main function is to block the calls passed between the. NET client and the COM object. At the same time,. NET provides InterOP assembly, which serves as a bridge between managed and unmanaged code and maps COM Object members to equivalent. Net hosted members.
The following describes various call methods for dll/COM compiled using VC ++ 6.0 and vs2005 (C #).
1. dll compiled by VC is called in VC
(1) write the DLL program in VC
Create a "Win32 dynamic-Link Library" Project-> "a DLL project that can export some symbols"-> complete
Add variables, functions, and classes
# Ifdef myvcdll_exports
# Define myvcdll_api _ declspec (dllexport)
# Else
# Define myvcdll_api _ declspec (dllimport)
# Endif
/// // User-Defined Function /////////
Myvcdll_api bool myfunction (void );
/// // Custom variable /////////
Extern myvcdll_api int myvar;
//// // Custom class ///////////
Class myvcdll_api myccalculator
{
Public:
Int subtract (int A, int B );
Int add (int A, int B );
Myccalculator ();
Virtual ~ Myccalculator ();
};
(2) Compile the DLL calling program in VC
# Include "stdafx. H"
# Include "myvcdll \ myvcdll. H"
# Include "myvcdll \ myccalculator. H"
# Pragma comment (Lib, "myvcdll \ debug \ myvcdll. lib ")
Int main (INT argc, char * argv [])
{
// Call the DLL variable
Printf ("the variable value in the called DLL is: % d \ n", myvar );
// Call the DLL Function
Printf ("the returned value of the function myfunction () in the called DLL is % d \ n", myfunction ());
// Call the DLL class
Myccalculator mycal;
Printf ("calling the DLL calculation class to execute addition: % d + % d = % d \ n", 5, 3, mycal. Add (5, 3 ));
Printf ("Call the calculation class in DLL to perform subtraction: % d-% d = % d \ n", 5, 3, mycal. Subtract (5, 3 ));
Printf ("end! \ N ");
Return 0;
}
2. com compiled by VC is called in VC
(1) Compile com program with VC
Create "alt com Appwizard" Project-> "dynamic link library (DLL)"-> complete
"New alt object..."-> "Simple Object"
"Add method..."-> Add ([in] int A, [in] int B, [out] int result)
"Add method..."-> subtract ([in] int A, [in] int B, [out] int result)
Build a project and register com
(2) Compile Com calling program using VC
# Include "stdafx. H"
# Include "myvccom \ myvccom. H"
# Include "myvccom \ myvccom_ I .c"
Int main (INT argc, char * argv [])
{
// Declare the hresult and imycalculator interface pointers
Hresult hr;
Imycalculator * imycal = NULL;
// Initialize com
HR = coinitialize (0 );
// Use the succeeded macro and check whether an interface pointer can be obtained
If (succeeded (HR ))
{
HR = cocreateinstance (clsid_mycalculator, null, clsctx_inproc_server,
Iid_imycalculator, (void **) & imycal );
// If the operation succeeds, the add method is called; otherwise, the corresponding error message is displayed.
If (succeeded (HR ))
{
Int result;
Imycal-> Add (5, 3, & result );
Printf ("Call com mycalculator add method: 5 + 3 = % d \ n", result );
Imycal-> subtract (5, 3, & result );
Printf ("Call com mycalculator subtract method: 5-3 = % d \ n", result );
Imycal-> release ();
}
Else
{
Printf ("cocreateinstance failed! \ N ");
}
}
// Release com
Couninitialize ();
Printf ("programe end! \ N ");
Return 0;
}
3. dll compiled by VC is called in
(1) write the DLL program in VC
Create a "Win32 dynamic-Link Library" Project-> "a DLL project that can export some symbols"-> complete
Add function
# Ifdef myvcdll_exports
# Define myvcdll_api _ declspec (dllexport)
# Else
# Define myvcdll_api _ declspec (dllimport)
# Endif
//// // Custom function ////////////
Extern "C" myvcdll_api char * myfunction1 (void );
Extern "C" myvcdll_api int myfunction2 (void );
(2) vs write a DLL call program
Copy the DLL file compiled by VC to the DEBUG directory of the vs calling program.
Using system. runtime. interopservices;
[Dllimport ("myvcdll. dll")]
Unsafe static extern char * myfunction1 ();
[Dllimport ("myvcdll. dll")]
Static extern int myfunction2 ();
Note: Check "generate"-> "allow Insecure code" in project properties.
4. com compiled by VC is called in
(1) Compile com program with VC
Create "alt com Appwizard" Project-> "dynamic link library (DLL)"-> complete
"New alt object..."-> "Simple Object"
"Add method..."-> Add ([in] int A, [in] int B, [out] int result)
"Add method..."-> subtract ([in] int A, [in] int B, [out] int result)
Build a project and register com
(2) vs compile a Com calling program
In "project"-> "Reference", find and add the registered "com"
Using myvccomlib;
Mycalculator mycal = new mycalculator ();
Int result;
Mycal. Add (5, 3, out result );
MessageBox. Show (string. Format ("{0} + {1} = {2}", 5, 3, result ));
5. com compiled by vs is called in VC
(1) vs compile com programs
Create a "project"-> "class library"
"Project Properties"-> "generate"-> "register for com InterOP" Check
Modify "assemblyinfo. cs"-> "[Assembly: comvisible (true)]"
Using system. runtime. interopservices;
// Interface
[Interfacetype (cominterfacetype. interfaceisdual)]
[GUID ("4c0c5188-83ec-4f52-8dbd-dfc3732cf8de")]
Public interface imycalculator
{
[Dispid (1)]
Int add (int A, int B );
[Dispid (2)]
Int substract (int A, int B );
}
// Class
[Progid ("mycalculator. DOTNET")]
[Classinterface (classinterfacetype. None)]
[GUID ("FB17DD89-9BF9-431a-9620-705F7D3E9AFB")]
Public class mycalculator: imycalculator
{
Public int add (int A, int B)
{
Return A + B;
}
Public int substract (int A, int B)
{
Return A-B;
}
}
(2) Compile Com calling program using VC
Copy the. TLB file of the com dll to the project directory.
# Include "stdafx. H"
// 1. Import Type Library TLB
# Import "myvscom. TLB"
Using namespace myvscom;
Int main (INT argc, char * argv [])
{
// 2. initialize com
Coinitialize (null); // you can change the value of null to 0.
// 3. Generate a smart pointer -- namespace: interface PTR pobject (_ uuidof (namespace: Class )))
Myvscom: imycalculatorptr pcal (_ uuidof (myvscom: mycalculator )));
// 4. Call methods in COM
Int result = pcal-> Add (5, 3 );
Printf ("Call the com class method compiled by vs (C #): % d + % d = % d \ n", 5, 3, result );
Result = pcal-> substract (5, 3 );
Printf ("Call the com class method written by vs (C #): % d-% d = % d \ n", 5, 3, result );
// 5. Release the environment
Couninitialize ();
Printf ("programe end! \ N ");
Return 0;
}