&Agenda
=========================
=========================
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;}
àDDE
àOLE1.0
àOLE2.0 (COM)
àDCOM
àActiveX
Advantages: CBD, Code Reusability, OOP, Communicate each, Network.
What is metadata
Metadata stored information:
public static extern int LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
public static extern IntPtr GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
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