C#中调用unmanaged DLL
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Diagnostics; // for Process
using
System.Runtime.InteropServices; // for DllImport
namespace
ZSICReaderDemo
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
[DllImport("ZSICReader.DLL")]
public
static
extern
bool
LinkReader();
[DllImport("ZSICReader.DLL")]
public
static
extern
bool
StopReader();
[StructLayout(LayoutKind.Sequential)] // 调用C++DLL必须使用 LayoutKind.Sequential
// 接口体需要讲参数类型进行转换
public
struct
CONSUMERINFO
{
public
uint
ulNum; // 用户编号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strCardNum; // 卡序号
public
uint
ulCardNo; // 卡内码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strName; // 姓名
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strSex; // 性别
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strDepartment; // 部门名称
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strSort; // 人员类别
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strAddr; // 地址
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strCertificate; // 证件号
public
double
dbCardfee; // 卡押金
public
double
dbLeftMoney; // 余额
public
double
dbSubsidyMoney; //补助金额
public
uint
lDepartment; // 部门编号
public
uint
lSort; // 人员类别编号
public
bool
bGS; // 挂失状态
public
bool
bDestroy; // 注销状态
public
uint
ulPsw; // 大额消费密码
public
uint
lTimes; // 卡使用次数
public
double
dbTJSKLeftMoney; // 脱机水控余额
public
uint
lTJSKCardTimes; // 脱机水控卡次数
public
uint
lJiFen; // 积分值
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public
string
strContact; // 联系方式
}
[DllImport("ZSICReader.DLL", CharSet = CharSet.Ansi, SetLastError = false)]
public
static
extern
bool
ReadCardInfo(ref
CONSUMERINFO
ConsumerInfo);
// 由于DLL中使用了打开了系统串口,所以必须手动调用FreeLibrary释放资源,否则主程序退出DLL时DLL_PROCESS_DETACH会出错
[DllImport("Kernel32")]
public
static
extern
int
FreeLibrary(IntPtr
hModule);
private
void
button1_Click(object
sender, EventArgs
e)
{
try
{
if (LinkReader())
{
MessageBox.Show("连接读卡器成功!");
}
else
{
MessageBox.Show("连接读卡器失败!");
}
}
catch (System.Exception
ex)
{
}
}
private
void
button2_Click(object
sender, EventArgs
e)
{
try
{
if (StopReader())
{
MessageBox.Show("停止读卡器成功!");
}
else
{
MessageBox.Show("停止读卡器失败!");
}
}
catch (System.Exception
ex)
{
}
}
private
void
button3_Click(object
sender, EventArgs
e)
{
try
{
CONSUMERINFO
ConsumerInfo = new
CONSUMERINFO();
if (ReadCardInfo(ref
ConsumerInfo))
{
MessageBox.Show("读取卡信息成功");
MessageBox.Show("卡序号:" + ConsumerInfo.ulCardNo.ToString());
MessageBox.Show("用户编号:" + ConsumerInfo.ulNum.ToString());
MessageBox.Show("姓名:" + ConsumerInfo.strName);
MessageBox.Show("性别:" + ConsumerInfo.strSex);
MessageBox.Show("卡余额:" + ConsumerInfo.dbLeftMoney.ToString());
}
else
{
MessageBox.Show("读取卡信息失败");
}
}
catch (System.Exception
ex)
{
}
}
private
void
Form1_FormClosing(object
sender, FormClosingEventArgs
e)
{
try
{ // 在关闭窗口时释放引用的链接库
foreach (ProcessModule
mod
in
Process.GetCurrentProcess().Modules)
{
if (mod.ModuleName == "ZSICReader.DLL")
{
FreeLibrary(mod.BaseAddress);
}
}
}
catch (System.Exception
ex)
{
}
}
}
}
注意:在C#项目中调试unmanaged dll时需要在项目设置里启用允许调试非托管代码。
关于如何手工卸载DLL详见这里Unload a DLL loaded using DllImport。摘要如下:

原始C的接口定义是这样的:
typedef
struct
_CONSUMERINFO
{
ULONG
ulNum;
char
strCardNum[100];
ULONG
ulCardNo;
char
strName[100];
char
strSex[100];
char
strDepartment[100];
char
strSort[100];
char
strAddr[100];
char
strCertificate[100];
double
dbCardfee;
double
dbLeftMoney;
double
dbSubsidyMoney;
long
lDepartment;
long
lSort;
bool
bGS;
bool
bDestroy;
ULONG
ulPsw;
long
lTimes;
double
dbTJSKLeftMoney;
long
lTJSKCardTimes;
long
lJiFen;
char
strContact[100];
} CONSUMERINFO;
BOOL
WINAPI
LinkReader();
BOOL
WINAPI
StopReader();
BOOL
WINAPI
ReadCardInfo(CONSUMERINFO *ConsumerInfo);
对比之前的C#代码,可以知道如何将C++结构转换为C#可以调用的结构了。
为了方便查看C#与C++结构的转换,引用http://www.cnblogs.com/lizi/archive/2012/02/22/2363181.html,
做了个表格:
|
C++类型 |
C#类型 |
|
基本类型 |
|
|
Handle |
IntPtr |
|
Hwnd |
IntPtr |
|
int* |
ref int |
|
int& |
ref int |
|
void* |
IntPtr |
|
unsigned char* |
ref byte |
|
BOOL |
Bool |
|
DWORD |
int 或 uint |
|
char** |
作为输入参数转为char[],通过Encoding类对这个string[]进行编码后得到的一个char[] 作为输出参数转为byte[],通过Encoding类对这个byte[]进行解码,得到字符串 C++ Dll接口: void CplusplusToCsharp(in char** AgentID, out char** AgentIP); C#中的声明: [DllImport("Example.dll")] public static extern void CplusplusToCsharp(char[] AgentID, byte[] AgentIP); C#中的调用: Encoding encode = Encoding.Default; byte[] tAgentID; byte[] tAgentIP; string[] AgentIP; tAgentID = new byte[100]; tAgentIP = new byte[100]; CplusplusToCsharp(encode.GetChars(tAgentID), tAgentIP); AgentIP[i] = encode.GetString(tAgentIP,i*Length,Length); |
|
枚举类型 |
|
|
BOOL MessageBeep(UINT uType // 声音类型); 其中的声音类型为枚举类型中的某一值。 |
用户需要自己定义一个枚举类型: public enum BeepType { SimpleBeep = -1, IconAsterisk = 0x00000040, IconExclamation = 0x00000030, IconHand = 0x00000010, IconQuestion = 0x00000020, Ok = 0x00000000, } C#中导入该函数: [DllImport("user32.dll")] public static extern bool MessageBeep(BeepType beepType); C#中调用该函数: MessageBeep(BeepType.IconQuestion); |
|
结构体 |
|
|
使用结构指针作为参数的函数: BOOL GetSystemPowerStatus( LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); Win32中该结构体的定义: typedef struct _SYSTEM_POWER_STATUS { BYTE ACLineStatus; BYTE BatteryFlag; BYTE BatteryLifePercent; BYTE Reserved1; DWORD BatteryLifeTime; DWORD BatteryFullLifeTime; } SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; |
使用结构指针作为参数的函数: BOOL GetSystemPowerStatus( LPSYSTEM_POWER_STATUS lpSystemPowerStatus ); Win32中该结构体的定义: typedef struct _SYSTEM_POWER_STATUS { BYTE ACLineStatus; BYTE BatteryFlag; BYTE BatteryLifePercent; BYTE Reserved1; DWORD BatteryLifeTime; DWORD BatteryFullLifeTime; } SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS; |
|
字符串 |
|
|
对于字符串的处理分为以下几种情况: 1、 2、 Win32: BOOL GetFile(LPCTSTR lpRootPathName); |
函数声明: [DllImport("kernel32.dll", CharSet = CharSet.Auto)] static extern bool GetFile ( [MarshalAs(UnmanagedType.LPTStr)] string rootPathName); 函数调用: string pathname; GetFile(pathname); 备注: DllImport中的CharSet是为了说明自动地调用该函数相关的Ansi版本或者Unicode版本 变长字符串处理: C#: 函数声明: [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetShortPathName( [MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength); 函数调用: StringBuilder shortPath = new StringBuilder(80); int result = GetShortPathName( @"d:\test.jpg", shortPath, shortPath.Capacity); string s = shortPath.ToString(); |
|
结构体 |
|
|
具有内嵌字符数组的结构: Win32: typedef struct _TIME_ZONE_INFORMATION { LONG Bias; WCHAR StandardName[ 32 ]; SYSTEMTIME StandardDate; LONG StandardBias; WCHAR DaylightName[ 32 ]; SYSTEMTIME DaylightDate; LONG DaylightBias; } TIME_ZONE_INFORMATION, *PTIME_ZONE_INFORMATION; |
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] struct TimeZoneInformation { public int bias; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string standardName; SystemTime standardDate; public int standardBias; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string daylightName; SystemTime daylightDate; public int daylightBias; } |
|
回调函数 |
|
|
BOOL EnumDesktops( HWINSTA hwinsta, // 窗口实例的句柄 DESKTOPENUMPROC lpEnumFunc, // 回调函数 LPARAM lParam // 用于回调函数的值 ); 回调函数DESKTOPENUMPROC的声明: BOOL CALLBACK EnumDesktopProc( LPTSTR lpszDesktop, // 桌面名称 LPARAM lParam // 用户定义的值 ); |
将回调函数的声明转化为委托: delegate bool EnumDesktopProc( [MarshalAs(UnmanagedType.LPTStr)] string desktopName, int lParam); 该函数在C#中的声明: [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern bool EnumDesktops( IntPtr windowStation, EnumDesktopProc callback, int lParam); |
C#中调用unmanaged DLL的更多相关文章
- 在.net中调用Delphi dll的Pchar转换
Pchar是非托管代码,要在.net中调用Delphi dll中的功能,请使用MarshalAs属性告知.net调用PInvoke去转换.net中标准的string类型.如果Delphi dll是De ...
- Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)
文章目录: 1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...
- 在SQL Server 2008中调用.net,dll
原文:在SQL Server 2008中调用.net,dll T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可以在SQ ...
- SOE 中调用第三方dll
一.简介 在利用soe实现server的扩展的时候,有些时候,需要调用第三方的dll库.官网中给出了明确的说明,soe中是可以添加第三方的dll文件,但是一直没有测试.按照官方的步骤应该是一个非常的简 ...
- Java Tomcat 中调用.net DLL的方法
近日一个java的项目,客户要求项目中必须使用其提供的加密机制,扔给了两个.net写的DLL.网络上搜了一圈也没找到啥东西,甚至看到人扬言此事绝无可能.郁闷当中考虑了一个思路.用C#做一个Com,调用 ...
- 如何在LabWIndows/CVI中调用LabVIEW DLL
首先请参考官方的文档 http://digital.ni.com/public.nsf/websearch/70995EC2CA1B523386256DD4004F3DE6?OpenDocument ...
- 【C# C++】C#中调用msvcr100.dll中的_beginthreadex函数
msvcr100.dll是VS2010的C运行时库DLL, _beginthreadex开启子线程的函数就在这个DLL里面实现 unsigned long _beginthreadex( voi ...
- C#中调用user32.dll库的keybd_Event函数,操作键盘
keybd_event()的函数原型是: void keybd_event( byte bVk, //虚拟键码 byte bScan, //该键的硬件扫描码 dword ...
- 关于在VB.NET中调用使用VC++编写的类库dll的一点笔记
前言 结对作业要求一出来,我就立刻想到了把“计算核心”封装成dll,然后使用vb.net编写UI调用dll的思路.然而在实现过程中却遇到了很多的问题. 我在这个过程中是负责使用vb.net编写UI并调 ...
随机推荐
- Select模型原理
Select模型原理 利用select函数,推断套接字上是否存在数据,或者是否能向一个套接字写入数据.目的是防止应用程序在套接字处于锁定模式时,调用recv(或send)从没有数据的套接字上接收数据, ...
- 关于学习netty的两个完整服务器客户端范例
https://github.com/wangyi793797714/IMServer https://github.com/wangyi793797714/IMClient https://gith ...
- iOS常用的存储方式介绍
在iOS App开发过程中经常需要操作一些需要持续性保留的数据,比如用户对于App的相关设置.需要在本地缓存的数据等等.本文针对OC中经常使用的一下存储方式做了个整理. 常用的存储工具/方式: NSU ...
- SAP BW 通过视图创建数据源(无单位)
因业务明细表中数量没有单位,所以BW创建数据源时,需做增强 数据表: ZDB_H(抬头) ZDB_I(明细) ECC 系统中: 1.创建视图ZVDBWQ,因明细表中数量没有单位,所以创建视图时不包括数 ...
- PHP生成条形码
前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...
- Linux shell 脚本攻略之统计文件的行数、单词数和字符数
摘自:<Linux shell 脚本攻略>
- Fedora安装
转载:http://www.51ou.com/browse/fedora/33174.html 安装fedora后的20个系统设置 安装VirtualBox增强工具 1.编辑sudoers文件,先备份 ...
- Android(java)学习笔记92:泛型高级之通配符
package cn.itcast_07; import java.util.ArrayList; import java.util.Collection; /* * 泛型高级(通配符) * ?:任意 ...
- LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...
- PHP中使用kindeditor
KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即所得编辑效果,兼容IE.Firefox.Chrome. Safari.Opera等主流浏览器.KindEditor ...