程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#的互操作性:緩沖區、結構、指針

C#的互操作性:緩沖區、結構、指針

編輯:C#基礎知識

 

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);
        }
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved