c#和c++互操作(平台调用相关)
[DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void Init();
[DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ScreenCapture(IntPtr img);
C#调用应该尽量避免用引用型的数组传递,因为c#里的数组不是连续的内存空间,c#里如果数组类型是简单类型那内存就是连续的。如果内存是引用类型那内存不是连续的,只是一个引用地址的数组,传给c++后c++操作连续内存空间c#里是得不到的。
C#的简单值类型数组传给c++的时候可以在c++端直接写指针,在c#端写数组,如下是成立的
int ScreenCapture(Pixcel (* image)[][][], int (* xyDataP)[], double (* scaleDataP)[]){
}
[DllImport("ScreenCaptureLib.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int ScreenCapture(IntPtr img, int[] xyData, double[] scaleData);
但是只能做到单项传递,即c#数组传给c++,如果在c++中改变c#数组中的值,c#中的值是不会跟着变得。
如果想在c++改c#中的数组,只能用IntPtr p = Marshal.AllocHGlobal(3 * 20 * 240 * 240);申请空间,然后把IntPtr传给c++,ScreenCapture(p,xyData,scaleData);,然后用Marshal.Copy(p, imageData, 0, 20 * 240 * 240 * 3);,把值拷贝给c#数组。
参照最后一个项目:http://www.cnblogs.com/wangjixianyun/archive/2013/04/10/3012556.html
传参:
转自:http://blog.csdn.net/Mittermeyer/article/details/1586867
///原型是:HMODULE LoadLibrary(LPCTSTR lpFileName);
///</summary>
///<param name="lpFileName">DLL 文件名</param>
///<returns>函数库模块的句柄</returns>
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string lpFileName);
/// int GetClassName(HWND hWnd, LPTSTR lpClassName, int nMaxCount);
///</summary>
[DllImport("user32",CharSet=CharSet.Ansi)]
public static extern Int32 GetClassName(IntPtr hwnd, Byte[] lpClassName, Int32 nMaxCount);
Byte[] abClassName = null;
Int32 dwRet = 0;
abClassName = new Byte[100];
dwRet = GetClassName(this.Handle, abClassName, 100);
sClassName = System.Text.ASCIIEncoding.ASCII.GetString(abClassName,0,dwRet);
MessageBox.Show(sClassName);
///原形:BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);
///</summary>
[DllImport("kernel32.dll")]
public extern static Int32 ReadFile(IntPtr hFile, Byte[] buffer,Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped);
/// BOOL GetCharWidth(HDC hdc,UINT iFirstChar,UINT iLastChar,LPINT lpBuffer);
///</summary>
[DllImport("gdi32")]
public static extern Int32 GetCharWidth(HDC hdc, Int32 wFirstChar, Int32 wLastChar, int32[] lpBuffer);
///原形:HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest);
///</summary>
///<param name="pGuidSrc"></param>
///<param name="pGuidDest"></param>
///<returns></returns>
[DllImport("Dsound.dll")]
private static extern Int32 GetDeviceID(ref Guid pGuidSrc, ref Guid pGuidDest);
///原形:
/// typedef struct tagPAINTSTRUCT {
/// HDC hdc;
/// BOOL fErase;
/// RECT rcPaint;
/// BOOL fRestore;
/// BOOL fIncUpdate;
/// BYTE rgbReserved[32];
/// } PAINTSTRUCT;
///</summary>
public struct PAINTSTRUCT
{
public IntPtr hdc;
public Boolean fErase;
public RECT rcPaint;
public Boolean fRestore;
public Boolean fIncUpdate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]public Byte[] rgbReserved;
}
///<summary>
///原形:HDC BeginPaint( HWND hwnd, LPPAINTSTRUCT lpPaint);
///</summary>
[DllImport("user32")]
public static extern IntPtr BeginPaint(IntPtr hwnd, ref PAINTSTRUCT lpPaint);
///原形:
/// typedef struct tagACCEL {
/// BYTE fVirt;
/// WORD key;
/// WORD cmd;
/// } ACCEL, *LPACCEL;
///</summary>
public struct ACCEL
{
public Byte fVirt;
public UInt16 key;
public UInt16 cmd;
}
///<summary>
///原形:int CopyAcceleratorTable(HACCEL hAccelSrc,LPACCEL lpAccelDst,int cAccelEntries);
///</summary>
///<returns></returns>
[DllImport("user32")]
public static extern Int32 CopyAcceleratorTable(IntPtr hAccelSrc, ACCEL[] lpAccelDst, Int32 cAccelEntries);
private struct CmBoxInfo
{
public static CmBoxInfo Empty = new CmBoxInfo();
public byte MajorVersion;
public byte MinorVersion;
public ushort BoxMask;
public uint SerialNumber;
public ushort BoxKeyId;
public ushort UserKeyId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CM_PUBLIC_KEY_LEN)] public byte[] BoxPublicKey;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CM_PUBLIC_KEY_LEN)] public byte[] SerialPublicKey;
public uint Reserve;
public void Init()
{
BoxPublicKey = new byte[CM_PUBLIC_KEY_LEN];
Debug.Assert(BoxPublicKey != null);
SerialPublicKey = new byte[CM_PUBLIC_KEY_LEN];
Debug.Assert(SerialPublicKey != null);
}
}
///<summary>
///原型:int CMAPIENTRY CmGetBoxes(HCMSysEntry hcmse, unsigned long idPort, CMBOXINFO *pcmBoxInfo, unsigned int cbBoxInfo)
///</summary>
[DllImport("xyz.dll")]
private static extern Int32 CmGetBoxes(IntPtr hcmse, CmGetBoxesOption idPort,IntPtr pcmBoxInfo, Int32 cbBoxInfo);
CmAccess cma = new CmAccess();
CmBoxInfo[] aBoxList = null;
Int32 dwBoxNum = 0, dwLoop = 0,dwBoxInfoSize = 0;
IntPtr pBoxInfo = IntPtr.Zero;
dwBoxNum = m_pCmGetBoxes(hcmBoxes, CmGetBoxesOption.AllPorts, IntPtr.Zero, 0);
if (dwBoxNum > 0)
{
aBoxList = new CmBoxInfo[dwBoxNum];
if (aBoxList != null)
{
dwBoxInfoSize = Marshal.SizeOf(aBoxList[0]);
pBoxInfo = Marshal.AllocHGlobal(dwBoxInfoSize * dwBoxNum);
if (pBoxInfo != IntPtr.Zero)
{
dwBoxNum = m_pCmGetBoxes(hcmBoxes, CmGetBoxesOption.AllPorts, pBoxInfo, dwBoxNum);
for (dwLoop = 0; dwLoop < dwBoxNum; dwLoop++)
{
aBoxList[dwLoop] = (CmBoxInfo)Marshal.PtrToStructure((IntPtr)((UInt32)pBoxInfo + dwBoxInfoSize * dwLoop), CmBoxInfo.Empty.GetType());
}
Marshal.FreeHGlobal(pBoxInfo);
pBoxInfo = IntPtr.Zero;
}
else
{
aBoxList = null;
}
}
}
///原形:typedef BOOL (CALLBACK *LPDSENUMCALLBACKA)(LPGUID, LPCSTR, LPCSTR, LPVOID);
///</summary>
public delegate Boolean LPDSENUMCALLBACK(IntPtr guid, String sDesc, String sDevName, ref Int32 dwFlag);
///<summary>
///原形:HRESULT WINAPI DirectSoundCaptureEnumerateA(LPDSENUMCALLBACKA pDSEnumCallback, LPVOID pContext);
///</summary>
[DllImport("Dsound.dll")]
public static extern Int32 DirectSoundCaptureEnumerate(LPDSENUMCALLBACK pDSEnumCallBack, ref Int32 dwFlag);
///原形:BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);
///</summary>
[DllImport("kernel32.dll")]
public extern static Int32 ReadFile(IntPtr hFile, Byte[] buffer,Int32 nNumberOfBytesToRead, ref Int32 lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped);
///原型是: FARPROC GetProcAddress(HMODULE hModule,LPCSTR lpProcName);
///</summary>
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
private extern static IntPtr GetProcAddress(IntPtr hModule, String sFuncName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
private extern static IntPtr GetProcAddressByIndex(IntPtr hModule, Int32 dwIndex);
c#和c++互操作(平台调用相关)的更多相关文章
- C# CLRInsideOut 托管代码与非托管代码互操作,产生相关调用代码的好工具 C++ 头文件转C# 的好工具(转
http://www.cnblogs.com/jxsoft/archive/2011/08/04/2127250.html
- CA1060
Move P/Invokes to NativeMethods class 规则描述: 平台调用服务访问非托管代码. 平台调用方法(使用了System.Runtime.InteropServices. ...
- 【转帖】C# DllImport 系统调用使用详解 托管代码的介绍 EntryPoint的使用
1 DLLImport的使用 using System; using System.Runtime.InteropServices; //命名空间 class Example { //用Dl ...
- 嵌入式单片机STM32应用技术(课本)
目录SAIU R20 1 6 第1页第1 章. 初识STM32..................................................................... ...
- DICOM标准相关资料
由于需要阅读影像,对DICOM需要先熟悉起来.关于DICOM,找了一些资料,可以学习.如下: DICOM标准:http://dicom.nema.org/standard.html 中文 DICOM ...
- Android平台调用WebService详解
上篇文章已经对Web Service及其相关知识进行了介绍(Android开发之WebService介绍 ),相信有的朋友已经忍耐不住想试试在Android应用中调用Web Service.本文将通过 ...
- JAVA面试题相关基础知识
1.面向对象的特征有哪些方面 ①抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节 ...
- VS C# 嵌入互操作类型
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass(); SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLSer ...
- 2014Esri全球用户大会之标准和互操作
1. ArcGIS是一个开放协作的平台吗?Esri是怎样看待"开放"的? 是的,ArcGIS是一个开放协作的平台.Esri公司致力于支持我们的用户更加easy的在异构环境下工作 ...
随机推荐
- Spring MVC 之传递模型数据到视图中
类似于 JSP-Servlet 中的 req.setAttribute . req.getSession().setAttribute ... --> 最后在 JSP 用 EL 表达式取得这些数 ...
- 20145222黄亚奇《网络对抗》 逆向及BOF进阶实践学习总结
20145222<网络对抗> 逆向及BOF进阶实践学习总结 实践目的 1.注入shellcode 2.实现Return-to-libc攻击 知识点学习总结 Shellcode实际是一段代码 ...
- JSP Tomcat8.0运行连接池时发生异常【AbstractMethodError oracle.jdbc.driver.T4CConnection.isValid(I)Z】
原创 2015年12月28日 11:38:01 2004 一.Tomcat8.0运行连接池时发生异常: AbstractMethodError oracle.jdbc.driver.T4CConnec ...
- Floyd算法 - 最短路径
2017-07-27 22:21:04 writer:pprp 该算法的本质是动态规划,形式简单,复杂度高为O(n^3): d[i][j] = max(d[i][k]+d[k][j],d[i][j]) ...
- QT treewidget 右键菜单
VS2012+QT5.2 ,没有ui,纯代码实现右键 方法一:常规但略麻烦 1.头文件slot中声明 QTreeWidget *tree; void showrightMenu(QPoint);//显 ...
- JSON01_资料
1. 资料网址: http://blog.csdn.net/vincent_czz/article/details/7333977 http://blog.csdn.net/huangwuyi/art ...
- Treflection03_getFields_getField
1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Field; public ...
- note——《Tableau商业分析一点通》
为了更好地发掘各领域的数据价值,且能对数据进行精确分析及可视化,掌握资料的脉动,做出正确的决策 人们需要一种工具:能够快速灵活地连接和整合数据,提供简单的方式实现从不同的角度去观察研究数据,计算和展示 ...
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is
在复制xml文件进行修改的时候,我经常将不小心对原文件进行修改,而导致创建bean出错.报错如下所示: Exception sending context initialized event to l ...
- mysql数据库(二):查询(SELECT)
一. 数据库查询—查询(SELECT) 单表查询 多表联合查询 二. 查询—单表查询 查询特定字段: select <字段1,字段2,...> from <表名>; 示例:查询 ...