Last active
February 24, 2026 13:56
-
-
Save sunmeat/a9a470b7f1f2681a3e33e620c7c9d4d4 to your computer and use it in GitHub Desktop.
DllImport example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; // тут знаходиться опис атрибуту DllImport | |
| namespace Sample | |
| { | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Console.Title = "C++ Strikes Back!"; | |
| MessageBox(IntPtr.Zero, "Hello from DLL!", "П45", 0); | |
| MsgBx(0, "DLL Function Alias", "", 0); | |
| // ==================================================================================== | |
| var h = GetStdHandle(STD_OUTPUT_HANDLE); | |
| for (int i = 0; i < 10; i++) | |
| { | |
| COORD position = new COORD((short)(Rand() % 20), (short)(Rand() % 20)); | |
| SetConsoleCursorPosition(h, position); | |
| Console.WriteLine("test!"); | |
| Thread.Sleep(100); | |
| } | |
| // ==================================================================================== | |
| int screenWidth = GetSystemMetrics(SM_CXSCREEN); | |
| int screenHeight = GetSystemMetrics(SM_CYSCREEN); | |
| Console.WriteLine(screenWidth + " " + screenHeight); | |
| for (int i = 0; i < 150; i++) | |
| { | |
| SetCursorPosition(Rand() % screenWidth, Rand() % screenHeight); | |
| Thread.Sleep(1); | |
| } | |
| // ==================================================================================== | |
| IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, "C++ Strikes Back!"); | |
| SetWindowText(hwnd, "!!! NEW TITLE !!!"); | |
| Thread.Sleep(5000); | |
| // пошук вікна з презентацією, показати Microsoft Spy++ !!! | |
| hwnd = FindWindowByClass("PP12FrameClass", IntPtr.Zero); | |
| SetWindowText(hwnd, "!!! Презентація !!!"); | |
| hwnd = FindWindowByCaption(IntPtr.Zero, "Snipping Tool"); | |
| SetWindowText(hwnd, "!!! Ножиці !!!"); | |
| } | |
| // іменований параметр CharSet дозволяє задавати набір символів, що використовується файлом DLL. | |
| // при написанні С++ додатків зазвичай не задається явно використання MessageBoxA або MessageBoxW | |
| // — завдяки pragma, компілятор вже знає, який набір символів застосовується — ANSI або Unicode. | |
| // на С# ви можете задати цільовий набір символів для атрибуту Dlllmport, який буде вказувати, яку версію MessageBox викликати. | |
| [DllImport("user32.dll", CharSet = CharSet.Ansi)] | |
| static extern int MessageBox(IntPtr hWnd, string message, string caption, int options); | |
| // якщо необхідно назвати свій внутрішній метод на С# не так, як називається функція DLL, | |
| // то це дозволяє інший іменований параметр атрибуту Dlllmport - EntryPoint | |
| [DllImport("user32.dll", EntryPoint = "MessageBoxA")] | |
| static extern int MsgBx(IntPtr hWnd, string message, string caption, int options); | |
| // ==================================================================================== | |
| [DllImport("user32.dll", EntryPoint = "SetCursorPos", SetLastError = true)] | |
| static extern bool SetCursorPosition(int X, int Y); | |
| [DllImport("user32.dll")] | |
| static extern int GetSystemMetrics(int nIndex); | |
| const int SM_CXSCREEN = 0; // ширина екрану | |
| const int SM_CYSCREEN = 1; // висота экрану | |
| [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] | |
| public static extern bool SetWindowText(IntPtr hwnd, string lpString); | |
| [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] | |
| static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); | |
| [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Auto)] | |
| static extern IntPtr FindWindowByClass(string lpClassName, IntPtr lpWindowName); | |
| [DllImport("msvcrt.dll", EntryPoint = "rand")] | |
| public static extern int Rand(); | |
| // ==================================================================================== | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern bool SetConsoleCursorPosition(IntPtr hConsoleOutput, COORD dwCursorPosition); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern IntPtr GetStdHandle(int nStdHandle); | |
| const int STD_OUTPUT_HANDLE = -11; | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct COORD | |
| { | |
| public short X; | |
| public short Y; | |
| public COORD(short x, short y) | |
| { | |
| X = x; | |
| Y = y; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment