COM Interop Presentation comments

简介: &Agenda ========================= •What is COM •Why use COM •C++ COM •C# COM •C++ Console calls C++ COM •C# calls u...

&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

 

 

 

 

 

 

 

 

 

 

#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(); return 0; }

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

  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 );
目录
打赏
0
0
0
0
20
分享
相关文章
Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF
1.常见用法           using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New ApplicationClass();    Microsoft.
2248 0
Beginner’s Tutorial: 3D Line and Border Effects in XAML
This mini-tutorial might be for you if you’re having troubles finding the right line colors to achieve simple 3D effects like these:   The solutio...
1161 0
Microsoft BuleHat 2018 Videos and Slides
https://www.youtube.com/watch?v=jxve5hrtwnI&feature=youtu.
1895 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等