&Agenda
=========================
•What is COM
•Why use COM
•C++ COM
•C# COM
•C++ Console calls C++ COM
•C# calls unmanaged C++
•Managed C++ calls C#
•Import C Plus Plus Dll in .NET
•Mapping type between C++ and C#
•Compiling COM
What is COM
ATL project
COM project
------ATLClass.h------
int
GetSelf(int x);
------ATLClass.cpp------
ATLClass::GetSelf(int x)
{
return x;
}
------CATLSimple.h------
GetSelf(int x)
STDMETHOD(GetSelf)(LONG x, LONG* y);
------CATLSimple.cpp------
STDMETHODIMP CATLSimple::GetSelf(LONG x, LONG* y)
// TODO: Add your implementation code here
*y=x;
return
S_OK;
------.c in COMPS project------
COM_i.c
COM_p.c
unmanaged: C++ console call C++ dll
#include "D:/COM/COM/COM_i.c"
#include "D:/COM/COM/COM_i.h"
int _tmain(int argc, _TCHAR* argv[])
IATLSimple * isobj=NULL;
HRESULT hr=CoInitialize(NULL);
if(SUCCEEDED(hr)){
hr=CoCreateInstance(CLSID_ATLSimple,
NULL,
CLSCTX_INPROC_SERVER,
IID_IATLSimple,
(void **)&isobj);
if(SUCCEEDED(hr))
long l;
isobj->GetSelf(1, &l);
cout<<l<<endl;
isobj->Release();
else
cout<<"Failed."<<endl;
CoUninitialize();
int i;
cin>>i;
return 0;
C# dll project
namespace CSharpClassLibrary
public class CSharpClass
public int GetSelf(int x) { return x; }
C# console project
namespace CSharpConsole
class Program
[DllImport("COM", EntryPoint = "GetSelf")]
public static extern int GetSelf(int x);
static void Main(string[] args)
Console.WriteLine(GetSelf(1));
Console.Read();
Managed: C++ calls C#
#include "stdafx.h"
using namespace System;
using namespace CSharpClassLibrary;
int main(array<System::String ^> ^args)
CSharpClass ^csc=gcnew CSharpClass();
Console::WriteLine(csc->GetSelf(1));
Console::WriteLine(L"Hello World");
Console::Read();
Unmanaged C++
extern "C" _declspec(dllexport) int GetSelf(int x){return x;}
•Component Object Model
•OO
•VB, C++,J++,C#...
•ONE Interface, Multiple Implement
Why use COM?
Clipboard Technology
àDDE
àOLE1.0
àOLE2.0 (COM)
àDCOM
àActiveX
Advantages: CBD, Code Reusability, OOP, Communicate each, Network.
What is metadata
Metadata stored information:
•Description of program set
•Flag(name, version, area, public key)
•Exported type
•Other program which this program set depends on.
•Running needed security permission
•Description of type
•Name, visibility, base, interface of implementation
•Member(method, column, property, event, nested type)
•Property
•Decorated type and members of other descriptive elements
Import C Plus Plus Dll in .NET
Load Dynamically
•[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
public static extern int LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
•[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
public static extern IntPtr GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
•[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
public static extern bool FreeLibrary(int hModule);
// function pointer
delegate int Add(int a, int b);
//1. Dynamically load C++ Dll
int hModule = LoadLibrary(AppDomain.CurrentDomain.BaseDirectory + @"CppInterop.dll");
if (hModule == 0) return;
//2. Read function pointer
IntPtr intPtr = GetProcAddress(hModule, "Add");
//3. Encapsulate function pointer to delegate
Add addFunction = (Add)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(Add));
• Load Statically
•[DllImport("CppInterop", EntryPoint = "ReturnBool", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
• public static extern bool ReturnBool(bool str);