当前位置:网站首页>C#.net调用动态库dll注意事项
C#.net调用动态库dll注意事项
2022-06-09 03:35:00 【51CTO】
C#.net调用动态库dll注意事项
本文主要描述作者在工作中所遇到技术难点及问题,最后提出相关的解决方案
场景
公司有个业务需求涉及到ID读卡器,构建成在ID卡读卡器一刷ID卡,系统就自动显示其ID卡号以及该ID卡的基本信息和消费的记录等信息;
问题
在选购相关的ID读卡器产品后,下载其开发包其中包含了用C++语言开发的动态库DLL,在调用时出现以下问题:
1、无法加载 DLL“OUR_IDR.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
2、尝试读取或写入受保护的内存。这通常指示其他内存已损坏
解决方案
1、第一个问题“无法加载 DLL“OUR_IDR.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。”,解决很简单,具体方法是将 dll复制到应用程序中的bin目录即可。
2、
我在调用以下代码时:
[DllImport("OUR_IDR.dll")]
public static extern byte[] idr_read(byte[] pserial);
就出现错误“尝试读取或写入受保护的内存。这通常指示其他内存已损坏 ”。
解决方法是将byte[]改为IntPtr即可,如:
[DllImport("OUR_IDR.dll")]
public static extern IntPtr idr_read(IntPtr pserial);
在调用时:
IntPtr cardId = new IntPtr();
IntPtr result = Module.idr_read(cardId);
即可;
C#在调用C++动态库DLL时,注意数据类型转换
C# 与 C++ 数据类型对照
C++ C#
=====================================
WORD ushort
DWORD uint
UCHAR int/byte 大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte
UCHAR* string/IntPtr
unsigned char* [MarshalAs(UnmanagedType.LPArray)]byte[]/?(Intptr)
char* string
LPCTSTR string
LPTSTR [MarshalAs(UnmanagedType.LPTStr)] string
long int
ulong uint
Handle IntPtr
HWND IntPtr
void* IntPtr
int int
int* ref int
*int IntPtr
unsigned int uint
COLORREF uint
边栏推荐
- Ranking list of short-term financial products in 2022
- 85.(leaflet之家)leaflet军事标绘-直线箭头绘制
- What is the network transformer for? (Ethernet network LAN LAN communication isolation filter) production plant / product schematic diagram / common products / price influencing factors
- Practical combat of Youku terminal side bullet screen piercing technology: pixelai mobile terminal real-time portrait segmentation
- C#/VB. Net to set solid color / gradient / picture background in word
- Container internal mysql5.7 configuration mode sqlmode + timeout wait_ timeout
- 分布式 | dble 读写分离场景下为什么普通的读 sql 发送到了 master 实例上
- Do you know websocket?
- ERP总体介绍
- Embedded related open source projects, libraries and materials
猜你喜欢

GC日志分析

现在VB6.0已经和SQL连接了,但是使用查询功能时无法做到任意条件查询,网上的情况和我的也不太相符,请问该如何实现呢?

About JS console Log() is a problem caused by synchronous or asynchronous

Flutter donewidget example

Ccf-csp 201503-3 Festival

Customized development of blind box app system

一文搞懂Cookie+Session,Redis+Token,JWT三者的区别
![InfoQ geek media's 15th anniversary solicitation | one minute Automated Deployment Based on ECs [elastic ECS]](/img/04/346f916c0af0a66647222de64c242c.png)
InfoQ geek media's 15th anniversary solicitation | one minute Automated Deployment Based on ECs [elastic ECS]

Simple use of Wireshark

服务器注册使用
随机推荐
Do you know websocket?
oracle 连接PLSQL
故障分析 | DDL 导致的 Xtrabackup 备份失败
[reinforcement learning notes] V value and Q value in reinforcement learning
Embedded related open source projects, libraries and materials
Two merged sequences (CF 1144 g) -dp
Generation of random numbers in C language [detailed explanation]
Ccf-csp 202012-5 Star Trek 80 points
RTSP/Onvif协议视频平台EasyNVR对静态文件大小的优化
Neural network learning (IV) -- a simple summary of the knowledge of each layer of neural network
Oracle connecting to PLSQL
ERP starts from internal integration
故障分析 | 大量短时进程导致 cpu 负载过高案例一则
Luogu p3647 [apio2014] Lianzhu line solution
Introduction to AI computing power
No cached version available for offline mode
character string
關於JS console.log() 是同步 or 异步引發的問題
This book has won the 2022 Beijing college entrance examination composition!
TS implements promise related APIs