c#调用c++dll共享内存需要函数

本文提供了一系列 C# 中实现跨进程内存映射的示例代码,包括创建、打开文件映射对象、映射文件数据到进程地址空间、释放文件映射等操作。还介绍了用于发送消息的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文连接: http://blog.youkuaiyun.com/richerg85/article/details/7530534


调用函数如下,都是项目中用到的函数,这样用到的时候不需要再照着msdn中c++函数一个一个的修改成c#支持的函数了。

[csharp]  view plain  copy
  1. //创建文件映射  
  2.     [DllImport("kernel32.dll", EntryPoint = "CreateFileMapping", SetLastError = true, CharSet = CharSet.Ansi)]  
  3.         public static extern IntPtr CreateFileMapping(int hFile, IntPtr lpAttribute, uint flProtected, uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);  
  4.     //打开文件映射对象  
  5.         [DllImport("kernel32.dll", EntryPoint = "OpenFileMapping", SetLastError = true, CharSet = CharSet.Auto)]  
  6.         private static extern IntPtr OpenFileMapping(uint dwDesiredAccess, bool bInheritHandle, String lpName);  
  7.     //把文件数据映射到进程的地址空间  
  8.         [DllImport("Kernel32.dll")]  
  9.         private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);  
  10.     //从调用线程的地址空间释放文件数据映像  
  11.         [DllImport("Kernel32.dll", EntryPoint = "UnmapViewOfFile", SetLastError = true, CharSet = CharSet.Auto)]  
  12.         private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);  
  13.     //关闭一个已经打开的文件句柄  
  14.         [DllImport("kernel32.dll", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Auto)]  
  15.         private static extern bool CloseHandle(IntPtr hHandle);  
  16.     //获取最后一个错误信息  
  17.         [DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = true, CharSet = CharSet.Auto)]  
  18.         private static extern uint GetLastError();  
  19.   
  20.         //发送消息需要函数  
  21.         [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]  
  22.         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);  
  23.   
  24.         [DllImport("User32.dll", EntryPoint = "PostMessage", CharSet = CharSet.Ansi)]  
  25.         public static extern bool PostMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);  
  26.     //  
  27.         const int ERROR_ALREADY_EXISTS = 183;  
  28.     //OpenFileMapping和MapViewOf函数中,使用的文件访问权限  
  29.         const int FILE_MAP_COPY = 0x0001;  
  30.         const int FILE_MAP_WRITE = 0x0002;  
  31.         const int FILE_MAP_READ = 0x0004;  
  32.         const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004;  
  33.     //CreateFileMapping函数中,文件映射时,保护属性  
  34.         const int PAGE_READONLY = 0x02;  
  35.         const int PAGE_READWRITE = 0x04;  
  36.         const int PAGE_WRITECOPY = 0x08;  
  37.         const int PAGE_EXECUTE = 0x10;  
  38.         const int PAGE_EXECUTE_READ = 0x20;  
  39.         const int PAGE_EXECUTE_READWRITE = 0x40;  
  40.     //  
  41.         const int INVALID_HANDLE_VALUE = -1;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值