首先,我们需要在C++程序中导出DLL文件。我使用的是Visual Studio开发,把项目"属性"中的“配置类型”改为"动态库dll",然后添加如下导出代码:

extern "C" __declspec(dllexport) void AS3911FindTag(Tag tags[], int &tagNum, int slot);//find tags
extern "C" __declspec(dllexport) bool GetTagInformation(Tag& tag);//get tag information
extern "C" __declspec(dllexport) int usbDeviceAttached(int waitTime);//initialize usb connect

然后运行程序,可以找到生成的DLL文件。在C#项目中使用,把DLL文件和生成的exe文件放在一起即可,然后C#中添加头部引用:

using System.Runtime.InteropServices;

类中声明:

[DllImport("AS3911Test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void AS3911FindTag(IntPtr tags, ref int tagNum, int slot); [DllImport("AS3911Test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetTagInformation(ref Tag tag);
[DllImport("AS3911Test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int usbDeviceAttached(int waitTime);

声明后,可以直接使用这些方法了。不过要注意一点,C#和C++的交互,最麻烦的是数据类型的转换。我这里在参数中使用了Tag这个结构体,在C++中如下:

struct Tag
{
char id[];
char dsfid[];
char afi[];
unsigned int blockNum;
unsigned int bytesPerBlock;
char info[];
};

在C#中对应的声明如下:

public struct Tag
{
/// char[]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = )]
public string id; /// char[]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = )]
public string dsfid; /// char[]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = )]
public string afi; /// unsigned int
public uint blockNum; /// unsigned int
public uint bytesPerBlock; /// char[]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = )]
public string info; }

注意C#中参数部分的ref,相当于C++中指针。涉及到数据类型对应转换时,可以借助CLRInsideOut这个工具。下面以AS3911FindTag(IntPtr tags, ref int tagNum, int slot)这个函数为例,看看如何使用。

static void FindTag()
{
ComInit();
usbDeviceAttached();
ISO15693Init(); int num = ;
int size = Marshal.SizeOf(typeof(Tag)) * Max_Tag_Num;
IntPtr pBuff = Marshal.AllocHGlobal(size);
Tag[] mytag = new Tag[Max_Tag_Num];
AS3911FindTag(pBuff, ref num, ); for (int i = ; i < num; i++)
{
IntPtr p = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Tag)) * i);
mytag[i] = (Tag)Marshal.PtrToStructure(p, typeof(Tag));
Console.WriteLine(mytag[i].id);
} Marshal.FreeHGlobal(pBuff); ISO15693DeInit();
usbDeviceDeAttached();
ComDeInit();
}

直接看到AS3911FindTag(pBuff, ref num, 16)这段代码,这里的pBuff是C#中的引用,然后用Marshal.AllocHGlobal(size)分配了一块内存,传入函数的是一块内存的引用。第二个参数比较简单,表示通过该函数给num赋值。AS3911FindTag这个函数运行完后,pBuff所指向的内存块会被赋值,我们下面就试着取出数据。我们使用了结构体Tag并声明了大小,所以可以从这里着手解决问题。看到for循环中第二行代码,这里的p表示从何处读取数据,最后做类型转换可。Marshal.PtrToStructure这个函数可以将数据从非托管内存块封送到新分配的指定类型的托管对象,这里从DLL中的非托管内存封送到当前C#托管内存。最后,用Marshal.FreeHGlobal(pBuff)释放内存。

C#中使用DLL文件的更多相关文章

  1. 如何保护.net中的dll文件(防破解、反编译)

    如何保护.net中的dll文件(防破解.反编译) 2010-07-19 15:08 [小 大] 来源: 赛迪网 评论: 0 分享至:      百度权重查询 词库网 网站监控 服务器监控 SEO监控  ...

  2. [转载]解析WINDOWS中的DLL文件---经典DLL解读

    [转载]解析WINDOWS中的DLL文件---经典DLL解读 在Windows世界中,有无数块活动的大陆,它们都有一个共同的名字——动态链接库.现在就走进这些神奇的活动大陆,找出它们隐藏已久的秘密吧! ...

  3. java中调用dll文件的两种方法

    一中是用JNA方法,另外是用JNative方法,两种都是转载来的, JNA地址:http://blog.csdn.net/shendl/article/details/3589676   JNativ ...

  4. 在VC中创建DLL文件的方法步骤

    一.Win32动态链接库 1.制作的步骤: (1)新建WIN32 Dynamic-link Library工程,工程名为MyDll,选择A simple DLL project类型. (2)MyDll ...

  5. 关于vs2012解决方案中项目DLL文件引用问题

    今天用vs2012建了项目框架,老是出现说解决方案中的项目dll文件不存在,但是我按照路径去找是可以找到这个文件的,也就是说这个文件存在的.我按照引用顺序单个编译每个项目都是成功的,就是当我编译整个解 ...

  6. C#中修改Dll文件 (反编译后重新编译)

    Dll文件生成后,如没有源代码,又要修改其中内容 可以用微软自带的ildasm和ilasm程序 先用ildasm将dll文件反编译成il文件 ildasm Test.dll /out=Test.il  ...

  7. 如何在 C#中添加 dll 文件

    按住鼠标左键,按住dll文件 ,然后将其拖动到工具箱里面 ,就出现了如图所示的控件

  8. vs2010中添加dll文件

    1.更改设置 1.1   project->properties->configuration properties->C/C++->General->Addtional ...

  9. 关于matlab2014a中生成dll文件,打包成com组件出现的问题和解决方法

    问题1:matlab2014a破解不完整,容易导致package打包失败 解决方法:1.下载破解文档:链接: http://pan.baidu.com/s/1eRJ4E2I 密码: 44th 2.下载 ...

随机推荐

  1. linux命令中 rpm –qa|grep softname的含义

    rpm –qa是列出所有rpm包后面接管道 |grep softname就是查含有softname的包名

  2. PHP基础语法2

    数组 PHP有两种数组:索引数组.关联数组. 函数 自定义函数 自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰 返回值 使用return关键字可以使函数返回值,可以返回包括数 ...

  3. Android:使用ViewPager实现左右滑动切换图片 (简单版)

    ViewPager,它是google SDk中自带的一个附加包的一个类, 可以使视图滑动. 步骤: 1.引入android-support-v4.jar包,在主布局里加入 <android.su ...

  4. *IntelliJ IDEA配置Hibernate

    为IntelliJ IDEA安装Hibernate插件

  5. LCMS

    LCMS(LearningContent Management System) 即学习内容管理系统

  6. UNIX内核的文件数据结构 -- v 节点与 i 节点

    龙泉居士:http://hi.baidu.com/zeyu203/item/cc89cfc0f36bfecc994aa07c 内核使用三种数据结构表示打开的文件(如图),他们之间的关系决定了在文件共享 ...

  7. php Late Static Bindings延迟静态绑定

    官网说道: As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to ref ...

  8. 转:Bitbucket使用方法

    一.软件及SSH keys: 由于我的Bitbucket账号的邮箱及用户名与Github相同,所以SSH Public Keys可以用Github的,登录Bitbucket,悬浮在用户名boliqua ...

  9. !! Scrum之 流程和术语

    !!Scrum之 流程和术语 http://www.cnblogs.com/zhoujg/archive/2009/07/15/1523680.html 以下将对一些术语进行简单介绍,以便大家现在开始 ...

  10. Android数据存储(三)——SQLite

    如果需要一个更加健壮的数据存储机制,则需要使用一个关系型数据库,在Android上,则为SQLlite. SQLite的特点:轻量级.嵌入式的.关系型数据库.可移植性好,易使用,小,高效且可靠,与使用 ...