程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#調用c++dll共享內存需要函數

c#調用c++dll共享內存需要函數

編輯:C#入門知識

調用函數如下,都是項目中用到的函數,這樣用到的時候不需要再照著msdn中c++函數一個一個的修改成c#支持的函數了。

[csharp]
//創建文件映射 
    [DllImport("kernel32.dll", EntryPoint = "CreateFileMapping", SetLastError = true, CharSet = CharSet.Ansi)] 
        public static extern IntPtr CreateFileMapping(int hFile, IntPtr lpAttribute, uint flProtected, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName); 
    //打開文件映射對象 
        [DllImport("kernel32.dll", EntryPoint = "OpenFileMapping", SetLastError = true, CharSet = CharSet.Auto)] 
        private static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, String lpName); 
    //把文件數據映射到進程的地址空間 
        [DllImport("Kernel32.dll")] 
        private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap); 
    //從調用線程的地址空間釋放文件數據映像 
        [DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile", SetLastError = true, CharSet = CharSet.Auto)] 
        private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress); 
    //關閉一個已經打開的文件句柄 
        [DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Auto)] 
        private static extern bool CloseHandle(IntPtr hHandle); 
    //獲取最後一個錯誤信息 
        [DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = true, CharSet = CharSet.Auto)] 
        private static extern uint GetLastError(); 
 
        //發送消息需要函數  www.2cto.com
        [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] 
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
 
        [DllImport("User32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Ansi)] 
        public static extern bool PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam); 
    // 
        const int ERROR_ALREADY_EXISTS = 183; 
    //OpenFileMapping和MapViewOf函數中,使用的文件訪問權限 
        const int FILE_MAP_COPY = 0x0001; 
        const int FILE_MAP_WRITE = 0x0002; 
        const int FILE_MAP_READ = 0x0004; 
        const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004; 
    //CreateFileMapping函數中,文件映射時,保護屬性 
        const int PAGE_READONLY = 0x02; 
        const int PAGE_READWRITE = 0x04; 
        const int PAGE_WRITECOPY = 0x08; 
        const int PAGE_EXECUTE = 0x10; 
        const int PAGE_EXECUTE_READ = 0x20; 
        const int PAGE_EXECUTE_READWRITE = 0x40; 
    // 
        const int INVALID_HANDLE_VALUE = -1; 


摘自 richerg85的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved