【hBifTs】.NET中使用Mapping File 的API :)
在.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
[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
由於原始的API使用到了多個Enum,我們也實現這些個Enum,以方便以後的使用.
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
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
接下來,就是把這些函式進行封裝了:)
這裡,我把這些函式封裝到一個Class中,其Class的定義如下:
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)
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部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-343241/,如需轉載,請註明出處,否則將追究法律責任。
請登入後發表評論
登入
全部評論
相關文章
- 聊聊asp.net中Web Api的使用ASP.NETWebAPI
- .Net Core使用File ProvidersIDE
- ElasticSearch 中的 MappingElasticsearchAPP
- MyBatis mapping.xml中的flushCache和useCache的使用MyBatisAPPXML
- 使用 Kubernetes APIAPI
- Oracle 12C Database File Mapping for Oracle ASM FilesOracleDatabaseAPPASM
- 使用ASP.NET Web API構建RESTful APIASP.NETWebAPIREST
- Redis在.net中的使用(2).net專案中的Redis使用Redis
- 使用aggregation API擴充套件你的kubernetes APIAPI套件
- 在 .NET Core 中構建 REST APIRESTAPI
- File類的使用
- 如何在File Cabinet Pro中對檔案排序排序
- 如何在File Cabinet Pro中對檔案排序?排序
- 使用MyBatis框架,dao層中的類與mapping包中xml配置檔案的關係MyBatis框架APPXML
- Node.js 中 Stream API 的使用Node.jsAPI
- Quartz.Net系列(十三):DateBuilder中的API詳解quartzUIAPI
- IoC在ASP.NET Web API中的應用ASP.NETWebAPI
- N-API中的Promise功能的使用APIPromise
- .NET Core使用 CancellationToken 取消API請求API
- MybatisPlus 中的API 使用總結(CRUD)MyBatisAPI
- Java 8中的Stream API使用指南JavaAPI
- ShellExcute API 在PB11中的使用API
- utl_file.file_type使用
- goalng中net/rpc的使用GoRPC
- .Net Api 之如何使用Elasticsearch儲存文件APIElasticsearch
- 使用.net6 WebApplication打造最小APIWebAPPAPI
- 使用net-snmp API程式設計(轉)API程式設計
- .NET使用Graphql的演示——新一代的API互動API
- ASP.NET Web API與Owin OAuth:使用Access Toke呼叫受保護的APIASP.NETWebAPIOAuth
- 從 MVC 到使用 ASP.NET Core 6.0 的最小 APIMVCASP.NETAPI
- ASP.NET Web API 中使用 swagger 來管理 API 文件ASP.NETWebAPISwagger
- samtools得到mapping中各個位置覆蓋度情況程式,samtools tview的使用APPView
- 如何在Java中使用檔案操作API: java.nio.file.Path?- marcobehlerJavaAPI
- osg使用整理(10):Shadow MappingAPP
- ASP.NET Web API與Owin OAuth:呼叫與使用者相關的Web APIASP.NETWebAPIOAuth
- Angular HTTPClient API 在 SAP 電商雲中的使用AngularHTTPclientAPI
- 簡單聊下.NET6 Minimal API的使用方式API
- 使用 .NET Core 3.x 構建 RESTFUL ApiRESTAPI