COM Interop Presentation comments

来源:互联网 发布:mysql in share mode 编辑:程序博客网 时间:2024/06/03 11:16

&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------

 

int

 

 

ATLClass::GetSelf(int x)

{

 

 

 

return x;

}

 

------CATLSimple.h------

 

 

 

 

 

int

 

 

GetSelf(int x)

{

 

 

return 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

 

 

 

 

 

 

 

 

 

 

C# dll project

C# console project

Managed: C++ calls C#

Unmanaged C++

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);

 Œ Load Dynamically

// 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);
原创粉丝点击