.NET Interoperability and Unmanaged Code - PowerPoint PPT Presentation

About This Presentation
Title:

.NET Interoperability and Unmanaged Code

Description:

?????????????? ? ???????????? ???. ???????????? ??. ... ?????? ???????? ?? ?????? ??????? ?? ?????? ? .NET (Common ... 'chunky' ?????? 'chatty' ?????????? ... – PowerPoint PPT presentation

Number of Views:204
Avg rating:3.0/5.0
Slides: 50
Provided by: MartinK158
Category:

less

Transcript and Presenter's Notes

Title: .NET Interoperability and Unmanaged Code


1
(No Transcript)
2
?????????????? ? ???????????? ???
???????????? ?? .NET Framework
http//www.nakov.com/dotnet-project/
?????? ?????
???????????? ???????? CodeAttest
www.codeattest.com
3
?????????? ??????
  • ?????? ???????? ?? ?????? ??????? ?? ?????? ?
    .NET (Common Type System)
  • ?????? ???????? ?? ????? C
  • ?????? ???????? ?? ????? C
  • ?????? ???????? ?? ???????????? COM
  • ?????? ???????? ?? ???????????? ??? Win32 ??? C ?
    C
  • ???????? ?? ?????????? ? .NET Framework

4
??????????
  • ????? ? ???????????????
  • ??????????? ????????? (P/Invoke)
  • ??????? DllImport
  • ????????????? ?? ????? (marshalling)
  • ????????????? ?? ?????????
  • ????????????? ?? ???????
  • ????????????? ?? ??????
  • ??????? MarshalAs
  • ?????????????? ?? ??????? ?? ??????? ?????????

5
?????????? (2)
  • ?????????????? ? COM
  • ????????? ?? COM ????????? ?? ?????????? ???
  • Runtime Callable Wrapper (RCW)
  • ?????????? ?? .NET ????????? ???? COM ?????????
  • COM Callable Wrapper (CCW)
  • ?????????? ??? .NET ?????? ?? ???????? ?? COM
  • ?????????????? ??? C (IJW)
  • ????????? ?? ?????????? ?? .NET ?????? ?? COM

6
????? ? ???????????????
  • Microsoft .NET Framework ? ??????????? ????
    ??????????
  • ?????????? ??????? ?????????? ????? ????????????
    ???
  • ???????????? ?? ???????????? ???
  • ??????????? ?? ????????
  • ?????? ?? ???????
  • ????????? ?? ????????
  • ???? ????? ??? ????????? ??????
  • CLR ????? JVM

7
????? ?? ???????????? ?????????? .NET
.NET Framework
.NET ??????????
.NET Framework Class Libraries
Common Language Runtime (CLR)
Platform Interop
Windows API
COM
Windows Kernel
8
????????????? ?? JVM
Java Platform
Java ??????????
Java Class Libraries
Java Virtual Machine (JVM)
JNI
Windows API
COM
Windows Kernel
9
??????????? ????????? (P/Invoke)
  • ???????? ?? ??????????? ?????? Windows ???????
    (simple flat API)
  • ???????? ?? ????? ???? ?????? ?? ???????????? ???
  • ????????? DllImport ????? ?? ????????? ??
    ???????????? ???
  • ?? ???????? DLL ????????? ????? ?? ?? ???????????
    ? ????

10
??????? DllImport
  • ???????? ?? ?? ?????????? ?? ?????? ?
    ???????????? ???
  • ??????? ?? ????? ??????
  • ??? ???????????? ?? ?????? ????? ?? DLL, ?????
    ??????? ??????
  • ?????? EntryPoint ?????? ????? ?? ??????????
    ?????
  • ?????? CharSet ???????? ??? ?? ?? ???????
    ?????????? ??????
  • ?????? SetLastError ???? ?????????? ?? ?????????
    ?? ??????

11
??? ?????? P/Invoke?
  • ?????? ? ??????? ? ??????? ???????? DLL
    (????????? ?? ???? ??????)
  • ??????? ?????? ?? ?????????, ?????? ??????????? ?
    ? ??????????? ???????
  • ?????? ???????? ?? ?????????????? ???
  • P/Invoke ???? ?? ?????? ?????????? ??
    ?????????????? ???

12
P/Invoke ??????
  • static IntPtr IDI_ASTERISK (IntPtr) 32516
  • DllImport("user32.dll", EntryPoint"LoadIconW",
  • ExactSpellingtrue, CharSetCharSet.Unicode)
  • public static extern IntPtr LoadPredefinedIcon(
  • IntPtr hinst, IntPtr icon)
  • private void Form1_Load(object sender,
    System.EventArgs e)
  • // ????????? ???????????? ?? ?????????? ?????
  • IntPtr hicon LoadPredefinedIcon(IntPtr.Zero,
    IDI_ASTERISK)
  • // ????????? ??? ????? ????? ?? ?????????
    ???????????
  • Icon icon Icon.FromHandle(hicon)
  • // ????????? ??????? ?? ??????? ????????
  • this.Icon (Icon) icon.Clone()

13
????????? DUMPBIN
  • ???????? ?? ?? ????????? ???
  • ???? ? ?? MS Visual C
  • ????????? ????????????? ?? ???????? ??????? ??
    ????? DLL
  • ??????

DUMPBIN /EXPORTS C\WINDOWS\system32\user32.dll
ordinal hint RVA name 446 1BD 0000CBBB
LoadIconA 447 1BE 000188E3 LoadIconW
14
???????????? 1
  • ?????????? ?? P/Invoke

15
????????????? ?? ????? (marshalling)
  • ???????? ????????? ???????? ??????
  • ???????????? ?? ?????????????
  • ????? ?? ??????????? (standard interop
    marshaller)
  • ???? ?? ????? ???????????? ?????
  • ???? ?? ?? ??????? ???????????? ????????

16
????????????? ????????????
???????????? ??? ?????????? ???
HANDLE System.IntPtr
BYTE System.Byte
WORD System.UInt16
DWORD System.UInt32
FLOAT System.Single
LPSTR, LPCSTR, LPWSTR, LPCWSTR System.String ??? System.StringBuilder
17
????????????? ?? ?????????
  • ??????????? ?? ???????? ?? ????????
  • ????????? ??, ?????? ???????????? ??? ????????
    ????????? ?? ???????? ??? ???? ????????
  • ????????? StructLayout ???????? ??? ?? ??
    ??????????? ??????????? ??? ???????????? ???
  • LayoutKind.Sequential
  • LayoutKind.Explicit

DLLFunc(POINT x) ? ManagedFunc(POINT
x) DLLFunc(POINT x) ? ManagedFunc(ref POINT x)
18
????????????? ?? ????????? ??????
  • StructLayout(LayoutKind.Explicit)
  • public struct SYSTEM_INFO
  • FieldOffset(0) public UInt16
    ProcessorArchitecture
  • FieldOffset(4) public UInt32 PageSize
  • FieldOffset(20) public UInt32
    NumberOfProcessors
  • FieldOffset(24) public UInt32
    ProcessorType
  • FieldOffset(28) public UInt32
    AllocationGranularity
  • FieldOffset(30) public UInt16
    ProcessorLevel
  • DllImport("kernel32.dll",
  • EntryPoint"GetNativeSystemInfo")
  • public static extern void GetSysInfo(
  • ref SYSTEM_INFO sysinfo)

19
????????????? ?? ???????
  • ????????? ?? ???????? ?? ?????
  • ????????? ?? ??? ???????? ??? ????? ????????
  • ????????? StructLayout ?? ??????? ? ?? ???????

DLLFunc(Job x) ? ManagedFunc(Job
x) DLLFunc(Job x) ? ManagedFunc(ref Job x)
20
????????????? ?? ??????
  • ?????????? ?????? ? .NET ?? ????????? (immutable)
  • ?????????? ?????? ? ???????????? ??? ?? ??????
    ????? ? ???????
  • ????? ?? ???? ???????? ????????
  • ????????? MarshalAs ????????? ??????????
    ?????????????
  • ?????? ?????????? ??? ? ? ???????????? ???????,
    ?? ???????? ?????? StringBuilder

21
??????? MarshalAs
  • ?????? ?????? ?? ?????????????
  • ??????? ?? ????? ?????????, ?????? ? ????????,
    ?????? ??? ??????????
  • ?? ? ????????????
  • ??? ???????????? ?? ?????? ???????????? ???, ???
    ????? ?? ?? ???????????
  • ?????? SizeConst ?????? ???? ?? ??????? ??
    ????????? ??? ?? ????????

22
????????????? ??????
???????????? C C
struct STOCK TCHAR ID32 char Name WCHAR Location StructLayout(LayoutKind.Sequential, CharSetCharSet.Auto) struct STOCK MarshalAs(UnmanagedType.ByValTStr, SizeConst32) public String ID MarshalAs(UnmanagedType.LPStr) public String Name MarshalAs(UnmanagedType.LPWStr) public String Location
23
????????????? ??????
  • DllImport("kernel32.dll", EntryPoint"GetModuleFi
    leName",
  • ExactSpellingfalse, CharSetCharSet.Auto)
  • public static extern UInt32 GetModuleFileName(IntP
    tr hModule, StringBuilder lpFileName, UInt32
    nSize)
  • STAThread
  • static void Main(string args)
  • StringBuilder moduleName new
    StringBuilder(10)
  • UInt32 uiSize GetModuleFileName(IntPtr.Zero,
  • moduleName, (uint) moduleName.Capacity
    1)
  • moduleName.Length (int) uiSize
  • Console.WriteLine("Executable path 0",
  • moduleName.ToString())

24
?????????????? ?? ??????? ?? ??????? ?????????
  • ????????? ?? ??????? ????????? (callback) ?????
    ?? ?????????? ?? ???????? ?? ????? ???????, ?????
    ??? ??? ????????
  • ??????????????? ??? ??????? ??????? ??
    ???????????? ???
  • ? ???????????? ??? ?? ???????? ?????????? ?? ???
    delegate

BOOL EnumWindows(WNDENUMPROC lpEnumFunc,
LPARAM lParam)
public delegate bool CallBack(int hWnd, int
lParam)
25
???????????? 2
  • ????????????? ?? ?????

26
?????????????? ? COM
  • ????? ? COM?
  • COM Common Object Model ??????????? ?????,
    ????????? ?????? ??? Win32 ??????????
  • COM ????????? ?????????????? ????? Win32
    ?????????? ??????????? ??????????
  • ??????????? IUnknown
  • ???????????? ?? ???????????? ?? ?????? COM
    ??????????
  • AddRef, Release ????????? ???????????? ???
    ?????????? (reference counting)
  • QueryInterface ??????? ????????????, ?????
    ??????????? ???????? (???? reflection)

27
?????????????? ? COM
  • GUID 128-?????? ?????, ????? ????????
    ???????????? COM ?????????, ????????
  • 645FF040-5081-101B-9F08-00AA002F954E
  • ??????????? IDispatch
  • ?????????? ?????? ?? ????? ?? ?????????? ??
    ?????????? ? ???????? ?? ?????????? (????
    reflection ? .NET)
  • In-Process ? Out-of-Process COM ???????
  • In-Process DLL ??????????, ??????? ? ?????????
    ???????????? ?? ???????
  • Out-of-Process ??????? ????????? ??????????
    (EXE), ??????? ? ??????? ??????? ????????????

28
?????????????? ? COM
  • ?????? ?? COM ?????????? STA ? MTA
  • Single-Threaded Apartment ????? ????? ?? ???
    ???????? ??????????, ???? ???? ?????, ???? ?????
    ?? ?????????????
  • Multithreaded Apartment ?????? ????? ????????
    ??? ?????????? (???? ?????), ??????????????? ?
    ?????
  • ?????? ?????????? (Type Libraries)
  • ???????? ???????? ?? COM ?????????? (????
    ??????????? ? ???????????)
  • ????????????? ??????? (.tlb) ???????
  • ??????? ??????, ???????, ????????? ? ??.
  • ActiveX ?????????? ?? COM ??????, ?????
    ????????? ?????? ? ??????? ?????

29
????????? ?? COM ????????? ?? ?????????? ???
  • ???????????? ?? Interop ???????
  • ?????????? ?? Interop ??????? ???? Visual Studio
    .NET
  • ?????????? ?? Interop ??????? ???? tlbimp.exe
  • ????????? ? ???????????? ?????????? ?? Interop
    ???????
  • ?????????? (deployment) ?? Interop ???????

30
Runtime Callable Wrapper (RCW)
  • COM ???????? ?? ????????? ???? ?????? ????????
    RCW
  • ?? ????? COM ????? ???????? ????? ???? RCW
  • ????????? ?????????? ????????????? ?? ???????
  • ???????? ???????????? COM ?????????? IDispatch,
    IErrorInfo, IUnknown

31
????????? ?? COM ????????? ???? RCW
.NET ??????
RCW
.NET ??????
RCW
32
???????????? 3
  • ?????????? ?? COM ???????????? "Microsoft Web
    Browser" ?? VS.NET

33
?????????? ?? .NET ????????? ???? COM ?????????
  • ????????? GuidAttribute ?????? ?????????
    ????????????? ?? COM ?????????? (GUID)
  • ????????? ProgId ?????? ??????? ????????????? ??
    COM ??????????
  • A???????? ?? ?????????? ? Windows Registry ????
    regasm.exe ??? ? VS.NET

GuidAttribute("D069E57A-981F-4841-8D68-E2F2342E92
A2"), ProgId("SomeApplication.SomeClass") pub
lic class SomeClass //
34
?????????? ?? .NET ????????? ???? COM ?????????
  • ???????????? ?? ?????? ??????????
  • ?????????? ?? ???????? ?????????? ???? tlbexp.exe
  • ?????????? ?? ???????? ?? ??????? ????? ????????
    ??????????
  • CoClassAttribute
  • ComVisibleAttribute
  • GuidAttribute

35
COM Callable Wrapper (CCW)
  • .NET ???????????? ???? ?? ????????? ?? COM
    ??????? ???? ????????? ??????
  • ?? ????? .NET ????????? ???????? ????? ???? CCW
  • ?????????? ????????????? ?? ???????
  • ?????? ?????? ???????????? ???????????? COM
    ?????????? IUnknown, IDispatch,

36
????????? ?? .NET ????????? ???? CCW
.NET ?????
COM ??????
CCW
COM ??????
37
?????????? ??? .NET ?????? ?? ???????? ?? COM
  • ????????????? ?????? ?????? ?? ?? public
  • ??????, ????????, ?????? ? ??????? ?????? ?? ??
    public
  • ???????? ?????? ?? ???? ???????? ??????????? ??
    ????????????
  • ???????? ?? ????? ?? ?? ??????????
  • ?????????? ?? ????????? ?? ?????????????
    ?????????
  • ?????????? ???????? ??????

38
???????????? 4
  • ????????? ?? .NET ????????? ???? CCW

39
?????????????? ??? C (IJW)
  • C ????????? ???????? ????????? ?? ????????????
    ??? (It Just Works)
  • ?????????? ?? IJW ?????? ??????????????? ? ?????
    (10-30 x86 ?????????? ?? ????? ?????????)
  • ??????? ????????????? ?? ???????
  • ?? ?? ??????? ????????
  • ????????? ?? ?? ????????? ??-????? ???????? ?
    ??????????????????
  • ???????? ? ?? ??????????, ????? ??????? ????????
    ???????????? ???

40
IJW ????????? ?? C ??????
using ltmscorlib.dllgt include ltstdio.hgt include
ltiostreamgt using namespace std void main()
// Declare unmanaged pointer of type char
const char str "IJW (It Just Works)" //
Call unmanaged function "printf"
printf("s\n", str) // Call unmanaged
function "ostreamoperator ltlt" cout ltlt str
ltlt endl // Call managed function
"ConsoleWriteLine" SystemConsoleWriteLin
e(str)
41
????????? ?? ?????????? ?? .NET ?????? ?? COM
  • ??????????? chunky ?????? chatty ??????????
  • ??????????????? IDisposable ?? ???????????????
    ???????
  • ?????????? ????? ?????????
  • ????????? ????? ?? ?????? ????? ?????? ??
    ???????? ???????
  • ????????????? ??????????????? ?? ?????

42
????????? ?? ?????????? ?? .NET ?????? ?? COM
  • ???? ?? ?????????? SuppressUnmanagedCode ????????
    ?? ???????? ?? ??????? ??????????
  • ??????? ???????? ?? ??????????????
  • ????? ?? CCW
  • ????? ?? ???????????????? (marshallings)
  • ????? ?? ???????? (stubs)
  • ??????????? CLR Spy ?? ?? ???????? ??????????
    ????????

43
???????????? 5
  • ?????????? ?? ?????? ? CLR Spy

44
?????????????? ? ???????????? ???
????????
45
??????????
  1. ??????????????? Windows Forms ??????????, ?????
    ??????? ?????? ? ????????? ? ??????? ???????. ??
    ????? ?????? ?????? ?? ?? ?????? ????????
    ?????????? ?????????????? ?? (PID), ????? ??
    ?????, ?? ????? ? ???????, ?????????? ??, ?????
    ?? ??????????? ? ???????????? ?? ??????? ?????
    (working set). ??????????? Windows API ?????????
    EnumProcesses(), OpenProcess(),
    GetModuleBaseName(), GetPriorityClass(),
    GetProcessWorkingSetSize() ? CloseHandle(), ????
    ?? ????????? ???? P/Invoke . ??????????? ?? ?
    ???????????? kernel32.dll ? psapi.dll.
    ??????????? ?????????????? ? ????????? ?? MSDN ??
    ?? ?????? ??? ?? ????????? ?????????? ???????.
    ?????????????? ?? ???????? ????? ???????????
    ?????????? ?? ?????????.

46
??????????
  1. ??????????????? Windows Forms ??????????, ?????
    ??????????? PDF ????????? ? ??????? ?? COM
    ?????????? "Adobe Acrobat Control for ActiveX".
  2. ???????? Windows Forms ????????, ????? ?????????
    ?????? "?????? ???". ????????? ??????????
    ???????? ???? COM ??????. ????????? HTML
    ????????, ? ????? ?? ????????????? ?????????? ?
    Internet Explorer.
  3. ???????????? ???????? ??????????, ????? ?? ?????
    XML ????, ???????? ?????? ?? ????? ? ??????????
    ?? ???, ???????? MS Excel ????????, ????????
    ?????? ?????????? ??? ??? ?? ???????. ????? ?????
    ?? ?????? ? ???, ????? ? ???????. ?? ?????? ? MS
    Excel ??????????? COM ???????????? "Microsoft
    Office Spreadsheet".

47
?????????? ??????????
  • MSDN Library http//msdn.microsoft.com
  • Interoperating with Unmanaged Code
  • An Overview of Managed/Unmanaged Code
    Interoperability
  • Beyond (COM) Add Reference Has Anyone Seen the
    Bridge?
  • Using the .NET Framework SDK Interoperability
    Tools
  • Calling a .NET Component from a COM Component
  • Microsoft Office and .NET Interoperability
  • The Myth of .NET Purity, Reloaded
  • Platform Invocation Services

48
?????????? ??????????
  • MSDN Magazine http//msdn.microsoft.com/msdnmag/
  • Calling Win32 DLLs in C with P/Invoke
  • Migrating Native Code to the .NET CLR
  • Improving .NET Application Performance and
    Scalability MS Patterns and Practices
    http//msdn.microsoft.com/library/en-us/dnpag/html
    /scalenet.asp
  • Chapter 7 Improving Interop Performance
  • Checklist Interop Performance
  • P/Invoke .NET The Interop wiki!
    http//www.pinvoke.net/

49
?????????? ??????????
  • Microsoft .NET/COM Migration and Interoperability
    http//msdn.microsoft.com/library/default.asp?ur
    l /library/en-us/dnbda/html/cominterop.asp
  • CLR Spy http//www.gotdotnet.com/Community/
    UserSamples/Details.aspx?SampleGuidC7B955C7-231A-
    406C-9FA5-AD09EF3BB37F
Write a Comment
User Comments (0)
About PowerShow.com