C # mixed with DLL and COM (1) -C # Call the function exported in C ++ written unmanaged DLL

xiaoxiao2021-03-23  208

C # Call the function exported in C write unmanaged DLL

Platform Invoke is a service (service) that enables managed code to invoke a unmanaged function (Service) implemented in the DLL, such as those in the Win32 API. It positions (locate) and calls the exported function, which is required to cross the parameters (Integers, Strings, Arrays, Structures, etc.) when needed. Platform Invoke is running the export function and set parameters in the runata-dependent metadata. The following figure illustrates this process:

Here is the process of calling the DLL unmanaged function

1. Specify the function in the DLL, you want to make a function of a function and the DLL containing this function Note the difference between the ANSI and Unicode version functions can also change the name of the function in the DLL, such as transforming Messageboxa to MsgBox

using System.Runtime.InteropServices; public class Win32 {[DllImport ( "user32.dll", EntryPoint = "MessageBoxA")] public static extern int MsgBox (int hWnd, String text, String caption, uint type);}

2. Create a class to include functions in the DLL. You can use an existing class, or create a separate class for each non-hosting function, or create a class to a category class in a managed class ( Wrapping) Common DLL function is an effective way to encapsulate related functions, although it is not necessary in some cases. Providing a packaging class is a convenient way because the definition of the DLL function is very troublesome and easy to make mistakes. If you use Visual Basic or C #, you must declare the DLL function within the CLASS or Visual Basic Module, which defines a Static method for each of the DLL functions you want to call. Definitions can include other information:, for example, a Calling Convention, a Calling Convention (CONFENG), if this information is ignored, will use the default setting. A list of complete statements and default settings. See Creating Prototypes in Managed Code. Once the package is complete, you can call these methods as calling other Static methods. Platform Invoke automates the following callout function When you design a managed class for Platform Invoke, consider the relationship between classes and DLL functions. For example: • Declaring a DLL function in an existing class • Creating a separate class for each DLL function, making the function independently and easy to find • Create a class containing a series of related DLL functions to logically group and reduce overhead

3. Creating a function signature in the managed code [C #] uses DLLIMPORTATTRIBUTE to make DLL and functions. Tag method as static and extern.

4. [C #] using System.Runtime.InteropServices; [StructLayout (LayoutKind.Sequential)] public struct Point {public int x; public int y;} [StructLayout (LayoutKind.Explicit)] public struct Rect {[FieldOffset (0) ] public int [; [fieldoffset (4)] public int top; [Fieldoffset (8)] public int right; [Fieldoffset (12)] public int bottom;} Class Win32API {[DLLIMPORT ("user32.dll")] public static extern bool PtInRect (ref Rect r, Point p);} [C #] [StructLayout (LayoutKind.Sequential)] public class MySystemTime {public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds;} class Win32API {[DllImport ( "Kernel32.dll")] public static extern void GetSystemTime (MySystemTime st);} case over the callback function is used in a repetitive task execution . Another usual usage is Enumeration function, such as EnumFontFamilies, Enumprinters, and EnumWindows implementing a callback function describes how to use P / Invoke in a hosted application using the Handle of each window on this machine.

In particular, this example uses the EnumWindows function to handle the list of windows and, a host-called callback function (Named Callback) is used to print the value of the window of the window [C #] use system; use system.Runtime.InterOpServices; public delegate Bool Callback (int hwnd, int lParam); public class EnumReportApp {[DllImport ( "user32")] public static extern int EnumWindows (CallBack x, int y); public static void Main () {CallBack myCallBack = new CallBack (EnumReportApp.Report) ENUMWINDOWS (MyCallback, 0);} Public Static Bool Report (int hwnd, int lparam) {console.write ("Window Handle IS"); console.writeLine (hwnd); Return true;}} Realize a callback function (1 Between implementations, see the EnumWindows function. Enumwindows (Signature) is as follows: BOOL EnumWindows (WndenumProc Lpenc, LParam LPARAM) One clue is this function that requires a Callback parameter lPenumFunc (2). Create a managed function. The example declares a delegate type (Called Callback) contains two parameters (hWnd and lparam). (3). Create a proxy (delegate) and transfer it as an EnumWindows parameter to ENUMWINDOWS, Platform Invoke automatic conversion delegate is a callback function . (4). Determines the garbage collector before the callback function is completed, it will not be recycled when you pass the Delegate as a parameter, or during the call to the structure that contains Delegate, he will not be recycled, so In the example below, the Callback function is completed before the function call returns, so the managed caller does not require additional work. If, Callback can be called after the function call returns, the managed caller must ensure that Delegate is not collected before the callback function is completed. See Interop Marshaling • Implementing Callback Functions callback function is to help unmanaged DLL functions in hosted applications. The following example illustrates the elements of the callback function and how to implement the callback in the hosted code. The callback function foundation Calls the function in the DLL in the managed code, creating a definition of the managed manager in a DLL and then calls it, which is a very direct function that uses the DLL to require some additional steps. First, you must use the lookup to determine if a function needs a callback, then create a backup function in your hosted code, finally call the DLL function, pass the pointer to the callback function. The following figure depicts this process declaration to pass Classes to a member of a member to a non-host DLL, because the membership of the class have a fixed layout (Layout).

The following example illustrates how to pass the member of the MYSystemTime class sequence definition to the GetSystemTime function getSystemTime in the user32.dll (Signature) is as follows Void getSystemTime (SystemTime * SystemTime); call

DLL

Function

.

Call your hosted class

As you call other methods, pass the situation of the structure and the power of the callback function.

Transmitting structures

(Passing Structures)

Disclaimer

The following example shows how to define one in the hosted code

Point

with

RECT

Structure, then pass them as parameters to

User32.dll

middle

PtinRect

function.

PtinRect

Function signature (

Signature

)as follows

Bool PtinRect (Const Rect * LPRC, POINT PT);

Note that one must be passed

RECT

Quote, because this function needs a pointing

RECT

Type pointer

转载请注明原文地址:https://www.9cbs.com/read-130438.html

New Post(0)