C# dll 定义及参数传递

zhongguang1019 2010-11-25 11:18:08
新手 问题 请教:

vc++编译的dll的函数原型:
BOOL SGIF_ReadEEProm(unsigned short Addr, unsigned char *pBuf, int Bytes);

C#中的引用 方式 :

[DllImport("SGCamIF_Release.dll",EntryPoint = "SGIF_ReadEEProm")]

static extern bool SGIF_ReadEEProm (ushort Addr, ref byte pBuf, int Bytes);


C#中调用方式


if (!SGIF_ReadEEProm(0, ref eepromData[0], eepromData.Length))




编译能通过,但是返回结果一直是假。不知道什么原因。
...全文
302 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
FANG_jinyi 2012-03-27
  • 打赏
  • 举报
回复

http://stackoverflow.com/questions/5602645/why-do-i-get-pinvokestackimbalance-was-detected-for-this-simple-example
gomoku 2010-11-25
  • 打赏
  • 举报
回复
直接用
static extern bool SGIF_ReadEEProm (ushort Addr, StringBuilder pBuf, int Bytes);

static extern bool SGIF_ReadEEProm (ushort Addr, byte[] pBuf, int Bytes);
简单一些。

返回false原因可能多种,比如Bytes不够大,比如没有初始化等等。


zhongguang1019 2010-11-25
  • 打赏
  • 举报
回复
自己顶下,期待问题解决
liuwenjun830 2010-11-25
  • 打赏
  • 举报
回复
static extern bool SGIF_ReadEEProm (ushort Addr, ref string pBuf, int Bytes);
zhongguang1019 2010-11-25
  • 打赏
  • 举报
回复
各种方法我都试过了,编译没有报错,可是返回假,估计是dll看来有问题了,多谢各位大哥。
whycom 2010-11-25
  • 打赏
  • 举报
回复
BOOL SGIF_ReadEEProm(unsigned short Addr, unsigned char *pBuf, int Bytes);

C#中的引用 方式 :

[DllImport("SGCamIF_Release.dll",EntryPoint = "SGIF_ReadEEProm")]

static extern bool SGIF_ReadEEProm (ushort Addr, Intptr pBuf, int Bytes);
byte[] b = new byte[1024];
intptr p = Marshal.UnsafeAddrOfPinnedArrayElement(b,0);
SGIF_ReadEEProm (addr, p, 1024);
garfieldzf 2010-11-25
  • 打赏
  • 举报
回复
如果返回值还是返回false,问下厂家导致返回接结果的可能都有那几种情况。
jieon 2010-11-25
  • 打赏
  • 举报
回复
SGIF_ReadEEProm直接带入方法原型看下结果。
garfieldzf 2010-11-25
  • 打赏
  • 举报
回复

调用

byte[] bytes=new byte[1024];

bool result=false;

result=ReadEEProm(0,ref bytes,bytes.Length)
garfieldzf 2010-11-25
  • 打赏
  • 举报
回复
调用的时候实例化byte[]
garfieldzf 2010-11-25
  • 打赏
  • 举报
回复

public static extern bool SGIF_ReadEEProm (ushort Addr, ref byte[] pBuf, int Bytes);
zhongguang1019 2010-11-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gomoku 的回复:]

直接用
static extern bool SGIF_ReadEEProm (ushort Addr, StringBuilder pBuf, int Bytes);

static extern bool SGIF_ReadEEProm (ushort Addr, byte[] pBuf, int Bytes);
简单一些。

返回false原因可能多种,比如Bytes不够……
[/Quote]
数组是够大的,也初始化了,按照byte[] pBuf这样声明的话,那我调用的时候如何传递参数啊?
mjp1234airen4385 2010-11-25
  • 打赏
  • 举报
回复
2楼 + 1

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧