在.NET中,进行程序间通信,可以使用的方法很多,比如.NET Remoting,WebService.等等..
但是使用上述方法太过于麻烦.不是很轻便..
在Win32中,一般要完成上述功能,方法也很多,一个比较通用的方法就是直接使用内存映射文件.Mapping File.
.NET FX1.0提供了P/Invoke,可能通过这个来直接调用Win32的API.这里我来演示一下这个方法.
要使用P/Invoke,我们得加入这个命名空间: System.Runtime.InteropServices
然后使用DllImportAttribute来导入我们所要用的Win32 API:

DllImport#region DllImport
[StructLayout(LayoutKind.Sequential)]
private struct SECURITY_ATTRIBUTES
{
int nLength;
int lpSecurityDescriptor;
bool bInheritHandle;
} 
[DllImport("kernel32",CharSet = CharSet.Auto)]
private static extern int CreateFileMapping(
int hFile,
int lpSecurityAttributes,
int flProtect,
int dwMaximumSizeHigh,
int dwMaximumSizeLow,
[MarshalAs(UnmanagedType.LPTStr)] string lpName
);
[DllImport("kernel32",CharSet = CharSet.Auto)]
private static extern int OpenFileMapping(
int dwDesiredAccess,
bool bInheritHandle,
[MarshalAs(UnmanagedType.LPTStr)] string lpName
);
[DllImport("kernel32",CharSet = CharSet.Auto)]
private static extern bool UnmapViewOfFile(int lpBaseAddress);
[DllImport("kernel32",CharSet = CharSet.Auto)]
private static extern void CloseHandle(int Handle);
[DllImport("kernel32",CharSet = CharSet.Auto)]
private static extern int MapViewOfFile(
int hFileMappingObject,
int dwDesiredAccess,
int dwFileOffsetHigh,
int dwFileOffsetLow,
int dwNumberOfBytesToMap
);
#endregion
Mapping File Enum Info#region Mapping File Enum Info
public enum ProtectionTypes:int
{
PageReadOnly = 0x2,
PageReadWrite = 0x4,
PageWriteCopy = 0x8
}
public enum SectionTypes:int
{
SecCommit = 0x8000000,
SecImage = 0x1000000,
SecNoCache = 0x10000000,
SecReserve = 0x4000000
}
public enum AccessTypes:int
{
Copy = 0x1,
Read = 0x4,
Write = 0x2,
Full = 0xF001F
}
#endregion
SharedMemory#region SharedMemory
public class SharedMemory : IDisposable
{ 
Private & Public Variable#region Private & Public Variable
private const int INVALID_HANDLE_VALUE = -1;
private int hMap = INVALID_HANDLE_VALUE;
private int lpAddr = 0;
private int size;
private int length;
public int MapHandle
{
get
{return hMap;}
}
public int Address
{
get
{return lpAddr;}
}
public int Size
{
get
{ return size;}
}
public int Length
{
get
{ return length;}
}
#endregion

Constructor#region Constructor
public SharedMemory()
{}
public SharedMemory(int Size,string Name) : this( Size,Name,ProtectionTypes.PageReadWrite,SectionTypes.SecCommit,AccessTypes.Full)
{}
public SharedMemory(int Size, string Name, ProtectionTypes Protection, SectionTypes Section, AccessTypes Access )
{
Create(Size, Name, Protection, Section, Access);
}
#endregion

Common Function#region Common Function
public void Create( int Size,string Name)
{
Create( Size,Name,ProtectionTypes.PageReadWrite,SectionTypes.SecCommit,AccessTypes.Full);
}
public void Create( int Size, string Name,ProtectionTypes Protection, SectionTypes Section, AccessTypes Access)
{
Dispose();
size = Size;
hMap = CreateFileMapping(INVALID_HANDLE_VALUE, 0, (int)Protection|(int)Section, 0, Size, Name);
if(hMap ==0)
{
throw new ApplicationException("Can't Create MappingFile " + Name);
}
lpAddr = MapViewOfFile(hMap, (int)Access, 0, 0, Size);
}

public void Open(int Size, string Name)
{this.Open(Size, Name, 0, AccessTypes.Read);}
public void Open(int Size, string Name, AccessTypes Access)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-343241/ ,如需转载,请注明出处,否则将追究法律责任。