c#调用c++dll共享内存需要函数
调用函数如下,都是项目中用到的函数,这样用到的时候不需要再照着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.zzzyk.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的专栏
补充:软件开发 , C# ,