Windows API

DllImport Attribute

Allows you to call unmanaged code from a managed application
This attribute is useful when you want to reuse existing unmanaged code in a managed application
The best eample is if you want to make calls to any of the unmanaged windows API methods

   CharSet = System.Runtime.InteropServices.CharSet

DllName -
EntryPoint - Specifies the method to call. If not specified then the default value is the same as the function name
ExactSpelling -
PreserveSig -
CharSet - specifies how strings in the signature (ie parameters and return values) are to appear. Typically either Unicode or ANSI

MessageBox Example

   CharSet = System.Runtime.InteropServices.CharSet.Unicode
int MessageBox(void* hWnd, wchar_t* lpText, wchar_t* lpCaption, unsigned int uType);
Declare Auto Function MBox Lib "user32.dll" Alias "MessageBox"( 
   ByVal hWnd As Integer,
   ByVal txt As String,
   ByVal caption As String,
   ByVal Typ As String) As Integer

More Examples

public const int SWP_NOACTIVATE = 0x0010; 
public const int SWP_NOZORDER = 0x4;

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(
            uint hWnd, // handle to window
            int hWndInsertAfter, // placement-order handle
            int X, // horizontal position
            int Y, // vertical position
            int cx, // width
            int cy, // height
            uint uFlags // window-positioning options

//IntPtr m_WindowHandle = IntPtr.Zero;
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

public static extern bool GetWindowRect(IntPtr hWnd, ref System.Drawing.Rectangle rect);

Unmanaged Datatypes

unsigned intUInt32
char*String* - charset = ANSI
wchar_t*String* - charset = Unicode


This can be used but you should use DllImport
This does not support marshalling of generic objects

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext