Skip to content

Instantly share code, notes, and snippets.

View Ruthenus's full-sized avatar

Ruslan Kachurovskyi Ruthenus

  • Odesa, Ukraine
View GitHub Profile
@sunmeat
sunmeat / Program.cs
Last active February 24, 2026 13:56
DllImport example
using System.Runtime.InteropServices; // тут знаходиться опис атрибуту DllImport
namespace Sample
{
class Program
{
static void Main()
{
Console.Title = "C++ Strikes Back!";
@sunmeat
sunmeat / Program.cs
Last active December 24, 2025 21:32
callbacks dllimport example
using System.Runtime.InteropServices;
using System.Text;
public delegate bool CallBack(int hwnd, int lParam);
public class OldFunctionsHolder
{
[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack x, int nothing);
@sunmeat
sunmeat / Program.cs
Last active December 24, 2025 21:46
ExactSpelling, SetLastError, CallingConvention, MarshalAs
using System.Runtime.InteropServices;
using System.Text;
// CallingConvention — це перелік, який визначає угоду про виклик функції,
// тобто правила, що визначають, як параметри передаються у функцію, хто очищає стек
// і як передаються повернені значення. Важливість вибору правильної угоди полягає
// у коректній організації обміну даними між керованим і нативним кодом.
// CallingConvention.Cdecl — зазвичай використовується у програмах на C і C++.
// Параметри передаються справа наліво, і сторона, що викликає, очищає стек після виклику функції.
@sunmeat
sunmeat / Program.cs
Last active December 24, 2025 22:19
unsafe code example
// компілювати цей додаток потрібно з параметром /unsafe !!!
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language
// https://www.dotnetperls.com/unsafe
class UnsafelApp
{
public static unsafe void GetValues(int* x, int* y)
{
*x = 6;
*y = 42;
@sunmeat
sunmeat / Program.cs
Last active December 24, 2025 22:41
fixed example
using System.Runtime.InteropServices;
using System.Text;
class Cat
{
public int paws = 4;
// якщо зробити властивість, то не вдасться застосувати оператор &
}
class CopyMaker
@sunmeat
sunmeat / Program.cs
Last active December 24, 2025 22:56
PostMessage example
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string className, string? windowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string? lpszWindow);
@sunmeat
sunmeat / Program.cs
Last active February 24, 2026 17:47
best winforms keylogger C# example
// код для файлу Program.cs, стандартну форму з шаблону треба видалити
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace HookKeylogger
{
public class Program : Form
{
@sunmeat
sunmeat / Program.cs
Last active February 22, 2026 08:19
arcanoid windows EnumWindows GetDesktopWindow GetWindowRect MoveWindow GetClassName example
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using static Program;
public static class DllImports
{
[DllImport("user32.dll")]
public static extern bool EnumWindows(EnumWindowsProc enumFunc, IntPtr lParam);
@sunmeat
sunmeat / Program.cs
Last active February 22, 2026 09:25
приклад на запуск і зупинку процесів в дот нет
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
class Program
{
static void Main()
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Створення процесу notepad.exe...");
@sunmeat
sunmeat / different files.cs
Last active February 22, 2026 13:15
отримання списку процесів, інформація про процес, зміна пріоритету
using System.Diagnostics;
using System.Security.Principal;
using System.Text;
class Program
{
static bool IsRunAsAdministrator()
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator);