现在项目基本都是旁边C++的哥们做好dll扔给我,然后我调用。好久之前晚上down了一份c#调用c++dll的方法,出处早已经遗忘。闲来无事,放上来好了。原作者看到后可以留言,我会把您链接放上的,帮了我很多!!!

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text; namespace TEDS_App
{
public enum ModePass
{
ByValue = 0x0001,
ByRef = 0x0002
}
public class FaultFunc
{
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll")]
static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]
static extern bool FreeLibrary(IntPtr hModule);
private IntPtr hModule = IntPtr.Zero;
private IntPtr farProc = IntPtr.Zero;
public void LoadDll(string lpFileName)
{
hModule = LoadLibrary(lpFileName);
if (hModule == IntPtr.Zero)
{
throw (new Exception("没有找到:" + lpFileName + "."));
}
}
public void LoadDll(IntPtr HMODULE)
{
if (HMODULE == IntPtr.Zero)
{
throw (new Exception("所传入的函数库模块的句柄为空"));
}
hModule = HMODULE;
}
public void LoadFun(string lpProcName)
{
if (hModule == IntPtr.Zero)
{
throw (new Exception("函数库模块的句柄为空,确保已进行加载dll操作"));
}
farProc = GetProcAddress(hModule, lpProcName);
if (farProc == IntPtr.Zero)
{
throw (new Exception("没有找到:" + lpProcName + "这个函数的入口点"));
}
}
public void LoadFun(string lpFileName, string lpProcName)
{
hModule = LoadLibrary(lpFileName);
if (hModule == IntPtr.Zero)
{
throw (new Exception("没有找到:" + lpFileName + "."));
}
farProc = GetProcAddress(hModule, lpFileName);
if (farProc == IntPtr.Zero)
{
throw (new Exception("没有找到:" + lpProcName + "这个函数的入口点"));
}
}
public void UnLoadDll()
{
FreeLibrary(hModule);
hModule = IntPtr.Zero;
farProc = IntPtr.Zero;
}
public object Invoke(object[] ObjArray_Parameter, Type[] TypeArray_parameterType, ModePass[] ModePassArray_Parameter, Type Type_Return)
{
if (hModule == IntPtr.Zero)
throw (new Exception("函数库模块的句柄为空,请确保进行了LoadLll操作"));
if (farProc == IntPtr.Zero)
throw (new Exception("函数指针为空,请确保已进行LoadFun操作"));
if (ObjArray_Parameter.Length != ModePassArray_Parameter.Length)
throw (new Exception("参数个数及其传递方式的个数不匹配"));
AssemblyName MyAssemblyName = new AssemblyName();
MyAssemblyName.Name = "InvokeFun";
AssemblyBuilder MyAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(MyAssemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder MyModuleBuilder = MyAssemblyBuilder.DefineDynamicModule("InvokeDll");
MethodBuilder MyMethodBuilder = MyModuleBuilder.DefineGlobalMethod("FaultFun", MethodAttributes.Public | MethodAttributes.Static, Type_Return, TypeArray_parameterType);
ILGenerator IL = MyMethodBuilder.GetILGenerator();
int i;
for (i = ; i < ObjArray_Parameter.Length; i++)
{
switch (ModePassArray_Parameter[i])
{
case ModePass.ByValue:
IL.Emit(OpCodes.Ldarg, i);
break;
case ModePass.ByRef:
IL.Emit(OpCodes.Ldarga, i);
break;
default:
throw (new Exception("第" + (i + ).ToString() + "个参数没有给定正确的传递方式"));
}
}
if (IntPtr.Size == )
{
IL.Emit(OpCodes.Ldc_I4, farProc.ToInt32());
}
else if (IntPtr.Size == )
{
IL.Emit(OpCodes.Ldc_I8, farProc.ToInt64());
}
else
{
throw new PlatformNotSupportedException();
}
IL.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, Type_Return, TypeArray_parameterType);
IL.Emit(OpCodes.Ret);
MyModuleBuilder.CreateGlobalFunctions();
MethodInfo MyMethodInfo = MyModuleBuilder.GetMethod("FaultFun");
return MyMethodInfo.Invoke(null, ObjArray_Parameter);
}
public object Invoke(IntPtr IntPtr_Function, object[] ObjArray_Parameter, Type[] TypeArray_ParameterType, ModePass[] ModePassArray_Parameter, Type Type_Return)
{
if (hModule == IntPtr.Zero)
throw (new Exception("函数库模块的句柄为空,请确保已进行LoadDll操作"));
if (IntPtr_Function == IntPtr.Zero)
throw (new Exception("函数指针IntPtr_Function为空"));
farProc = IntPtr_Function;
return Invoke(ObjArray_Parameter, TypeArray_ParameterType, ModePassArray_Parameter, Type_Return);
}
} }

一直以来,对于C++程序员报以崇高的敬意。。。一直觉得他们屌屌的,哈哈。

调用方式如下:

 PlusFunction.LoadDll(@"C:\win32dll.dll");//PlusFunction为调用类的实例
PlusFunction.LoadFun("MyFun");
byte[] a = File.ReadAllBytes(@"E:\19-bw\19-73.jpg");
object[] Parameters = new object[] {a}; // 实参为a
Type[] ParameterTypes = new Type[] { typeof(byte[])}; // 实参类型为byte[]
ModePass[] themode = new ModePass[] {ModePass.ByValue}; // 传送方式为值传
Type Type_Return = typeof(int); // 返回类型为int
ret = (int)PlusFunction.Invoke(Parameters, ParameterTypes, themode, Type_Return);

其实,c++与c#主要的就是数据类型的对应了。简单点的还好说,稍微复杂的各种麻烦。。。关键是不好调试。

下面举些我用到的例子,以后遇到其他的再补充。日积月累- -

 c++                                    c#
char* char[](string.tochararray)
byte* byte[]
int int
int* int[]
结构体
c++
typedef struct SRectChange_TAG
{
//NV_RECT rect;
int x;//左上角x轴坐标
int y;//左上角y轴坐标
int width;//宽
int height;//高
int degree;//报错级别;1最低,目前暂时设定3级
}
SRectChange;
c#
[StructLayout(LayoutKind.Sequential)]
public struct SRectChange
{
public int x;
public int y;
public int width;
public int height;
public int degree;
}
结构体传递
[DllImport("win32dll.dll", EntryPoint = "MyFun", CallingConvention = CallingConvention.Cdecl)]
public static extern int MyFun(ref SRectChange rect, char[] str, char[] str2);
c++结构体
typedef struct
{
int osVersion;
int majorVersion;
int minorVersion;
int buildNum;
int platFormId;
char szVersion[];
}OSINFO;
c#
// OSINFO定义
[StructLayout(LayoutKind.Sequential)]
public struct OSINFO
{
public int osVersion;
public int majorVersion;
public int minorVersion;
public int buildNum;
public int platFormId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string szVersion;
} 结构体数组传递
c#代码
[DllImport("win32dll.dll", EntryPoint = "MyFun", CallingConvention = CallingConvention.Cdecl)]
public static extern int MyFun(IntPtr p, char[] str, char[] str2);
数组传指针
char[] newpic = ("").ToCharArray();
char[] oldpic = ("").ToCharArray();
SRectChange[] rects = new SRectChange[];
for (int i = ; i < rects.Length; i++)
{
rects[i] = new SRectChange();
}
IntPtr[] ptArr = new IntPtr[];
ptArr[] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SRectChange)) * ); //分配包含两个元素的数组
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SRectChange)));
Marshal.Copy(ptArr, , pt, ); //拷贝指针数组
MyFun(pt, newpic, oldpic);
for (int i = ; i < ; i++)
{
rects[i] = (SRectChange)Marshal.PtrToStructure((IntPtr)(pt.ToInt32() + i * Marshal.SizeOf(typeof(SRectChange))), typeof(SRectChange));
Console.WriteLine("x:{0} y:{1}", rects[i].x, rects[i].y);
}

还说那句话:种一棵树最好的时间是十年前,其次是现在。

C#调用C++ Dll的更多相关文章

  1. c# 调用c++DLL方法及注意事项

    引用命名空间 using System.Runtime.InteropServices 调用方法: 一.静态加载 用DllImprot方式来加载c++DLL.如下格式: //对应c++方法 //voi ...

  2. 在.net中调用Delphi dll的Pchar转换

    Pchar是非托管代码,要在.net中调用Delphi dll中的功能,请使用MarshalAs属性告知.net调用PInvoke去转换.net中标准的string类型.如果Delphi dll是De ...

  3. Java调用第三方dll文件的使用方法 System.load()或System.loadLibrary()

    Java调用第三方dll文件的使用方法 public class OtherAdapter { static { //System.loadLibrary("Connector") ...

  4. C#调用C++ DLL类方法

    C++的优势在于高效灵活,C#的优势在于简单易用,两者结合起来对项目开发来说是件好事,而且C++不容易反编译,也保障了代码的安全性,如果一些核心算法使用C#编写,那么保密就是一个问题. C++生成的D ...

  5. paip.java 调用c++ dll so总结

    paip.java 调用c++ dll so总结 ///////JNA (这个ms sun 的) 我目前正做着一个相关的项目,说白了JNA就是JNI的替代品,以前用JNI需要编译一层中间库,现在JNA ...

  6. C#调用C++ DLL 文件

    说来惭愧,都注册一年多了,却没有发表过一篇正式的博文,中间很多学习的过程也没有记录下来.如今到了一个新的环境,也有了学习的机会,一定要把每天的收获记录一下. 要做的东西需要引用C++编写的DLL,刚开 ...

  7. LR调用动态链接库DLL

    什么是动态库? 动态库一般又叫动态链接库(DLL),是Dynamic Link Library 的缩写形式,DLL是一个包含可由多个程序同时使用的代码和数据的库. 动态链接提供了一种方法 ,使进程可以 ...

  8. 【JNI】OPUS压缩与解压的JNI调用(.DLL版本)

    OPUS压缩与解压的JNI调用(.DLL版本) 一.写在开头: 理论上讲,这是我在博客园的第一篇原创的博客,之前也一直想找个地方写点东西,把最近做的一些东西归纳总结下,但是一般工程做完了一高兴就把东西 ...

  9. c#调用c++ dll的几种类型(转)

    http://www.sosuo8.com/article-2012/dllleixingzhuanhuan.htm   在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传 ...

随机推荐

  1. 编写javascript、Jquery的String.format();

    在javascript.Jquery里面好像是没有String.format();这个函数的,所以我们在拼接字符串的时候就特别的辛苦,生怕又打错,而且又乱,所以就自己去写一个函数来代替. String ...

  2. 简单的python http接口自动化脚本

    今天给大家分享一个简单的Python脚本,使用python进行http的接口测试,脚本很简单,逻辑是:读取excel写好的测试用例,然后根据excel中的用例内容进行调用,判断预期结果中的返回值是否和 ...

  3. HTTP协议——学习资料小结

    嗯,这几天回头再次的学习Servlet的知识点,觉得HTTP协议的内容是相当重要的,现在虽然知道浏览器与应用程序的交互离不开它,但是怎么将信息从浏览器传输到服务器的这个知识点还是一个盲点.于是从网上找 ...

  4. 再看.net本质(二)

    3.[HTTP协议]  当浏览器寻找到Web服务器的地址之后,浏览器将帮助我们把对服务器的请求转换为一系列参数发送给Web服务器.服务器收到浏览器的请求对数之后,将会分析这些数据并进行处理,然后向浏览 ...

  5. PAT1013

    #include<cstdio>#include<cstring>#include<vector>using namespace std;const int max ...

  6. js的二元三元操作符

    二元 if ( a == b) { alert(a) } // (a == b) && alert(a) if ( a != b) { alert(a) } // (a == b) | ...

  7. QTP操作论坛回复编辑框----webelement

    Set bp=browser("micclass:=browser","index:=0").page("micclass:=page") ...

  8. 织梦DedeCMS列表摘要 description 长度控制方法

    [field:description /]标签如何限制字数? [field:description function='cn_substr(@me,80)'/] DedeCMS 里的所有标记都支持这样 ...

  9. 在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client

    在PHP应用中简化OAuth2.0身份验证集成:OAuth 2.0 Client   阅读目录 验证代码流程 Refreshing a Token Built-In Providers 这个包能够让你 ...

  10. poj1005 I Think I Need a Houseboat

    这题目只要读懂了意思就好做了,先求出来(0.0)到(x.y)的距离为r,然后求出来以r为半径的半圆的面积,然后再用这个面积除以50,再向上取整就可以啦. #include <stdio.h> ...