惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
N
Netflix TechBlog - Medium
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
IT之家
IT之家
博客园 - Franky
雷峰网
雷峰网
博客园 - 聂微东
腾讯CDC
M
MIT News - Artificial intelligence
B
Blog RSS Feed
博客园_首页
罗磊的独立博客
S
SegmentFault 最新的问题
I
InfoQ
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
D
Docker
宝玉的分享
宝玉的分享
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 许明会

异步编程,采用WorkgroupWorker,async和await关键字 OCR图像识别技术-Asprise OCR 关于委托,事件和类的设计准则 JavaScript能干什么? C#泛型代理、泛型接口、泛型类型、泛型方法 Delegate, Method as Parameter. DES对称性加密 利用委托实现异步调用 通过Windows组策略限制证书组织流氓软件的安装运行 枚举\位域\结构综合实验 - 许明会 - 博客园 public static void Invoke (Action action) C#编写WIN32系统托盘程序 SQLServer异步调用,批量复制 Python体验(10)-图形界面之计算器 Python体验(09)-图形界面之Pannel和Sizer Python体验(08)-图形界面之工具栏和状态栏 Python体验(07)-图形界面之菜单 C#利用WIN32实现按键注册 Javascript猜数字游戏
C#的互操作性:缓冲区、结构、指针
许明会 · 2016-03-25 · via 博客园 - 许明会
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace Interop
{
    class Program
    {
        [DllImport("kernel32.dll", EntryPoint = "Beep")]
        public static extern bool MyBeep(uint iFreq, uint iDuration);
        //HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName);
        [DllImport("kernel32.dll")]

        public static extern IntPtr LoadLibrary(string dllName);
        delegate int deleMessageBox(IntPtr hWnd, string text, string caption, uint type);
        //GetProcAddress函数检索指定的动态链接库(DLL)中的输出库函数地址。
        //FARPROC GetProcAddress(
        //      HMODULE hModule, // DLL模块句柄
        //    LPCSTR lpProcName // 函数名
        //  );
        [DllImport("kernel32.dll")]
        public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
        //CharSet = CharSet.Auto
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);
        //DWORD GetCurrentDirectory(DWORD nBufferLength, //sizeofdirectorybuffer
        //LPTSTR lpBuffer   //directorybuffer
        //);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetCurrentDirectory(int BufferLength, System.Text.StringBuilder lpBuffer);
        //LPSTR GetCommandLine()
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern System.IntPtr GetCommandLine();
        //结构体
        //typedef struct{
        //    int wStructSize;
        //    int x;
        //    int y;
        //    int dx;
        //    int dy;
        //    int wMax;
        //    TCHAR rgchMember[2];
        //}HELPWININFO;
        [StructLayout(LayoutKind.Sequential)]
        public struct HELPWININFO
        {
            int wStructSize;
            int x;
            int y;
            int dx;
            int dy;
            int wMax;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
            public char[] rgchMember;
        }
        static void Main(string[] args)
        {
            MyBeep(500, 500);
            //函数需要修改内存缓冲区,必须用StringBuilder,因为String类型是只读的
            StringBuilder sb = new StringBuilder(255);
            GetCurrentDirectory(255, sb);
            Console.WriteLine(sb);
            //使用IntPtr类将返回的字符串保存到string中
            IntPtr ptr = GetCommandLine();
            string cmdline = Marshal.PtrToStringAuto(ptr);
            Console.WriteLine(cmdline);
            //GetProcAddress
            IntPtr ptrKernel32 = LoadLibrary("user32.dll");
            IntPtr ptrProcMessageBox = GetProcAddress(ptrKernel32, "MessageBoxA");
            deleMessageBox messageBox = Marshal.GetDelegateForFunctionPointer(ptrProcMessageBox, typeof(deleMessageBox)) as deleMessageBox;
            messageBox(IntPtr.Zero, @"public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);", "LoadLibrary", 0x40);
            MessageBox(IntPtr.Zero, "Content Here!", "Caption", 0x40);
        }
    }
}