PInvoke
Apparence
Obtenir les fonctions d'un DLL C/C++
Utilisez DLL Export Viewer ou PEBrowse pour afficher les méthodes exportées par cette DLL.
Obtenir les signatures des fonctions C/C++
- Consultez le fichier .h
- DLL Windows : Windows Application UI Development
Convertir les signatures C/C++ en signatures C#
PInvoke Interop Assistant Onglet "SigImp Translate Snippet" → collez la signature et les structures C/C++ dans "Native Code Snippet".
Obtenir les signatures .NET
Exemple
Prenons la DLL « C:\Windows\System32\user32.dll » :
Voici les exports donnés par PE Browse :
- ...
- InvalidateRect (1957) (0x00021381)
- ...
Ne possédant pas le fichier .h, une recherche sur internet : InvalidateRect + MSDN nous donne cette signature :
Fichier:Csharp.png |
BOOL InvalidateRect(
HWND hWnd,
const RECT* lpRect,
BOOL bErase
);
|
---|
L'outil "PInvoke Interop Assistant" génère le code suivant :
Fichier:Csharp.png |
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", EntryPoint="InvalidateRect")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InvalidateRect(IntPtr hWnd, ref RECT lpRect, [MarshalAs(UnmanagedType.Bool)] bool bErase);
|
---|
Signatures récupérés sur internet :
Fichier:Csharp.png |
// PINVOKE.NET
static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InvalidateRect(IntPtr hWnd, IntPtr rect, [MarshalAs(UnmanagedType.Bool)] bool erase);
|
---|