.Net调用非托管代码时候遇到的问题

liveinblue 2008-07-14 11:24:39
小弟手头有一活,底层都是vc开发的,现在用c#来调用
未曾研究过vc,初次调用出错,在线急等解决方案
vc方法签名如下

LPRENGINE_API LPRRESULT __stdcall InitLPREngineParamHelper( LPREngineParam* pParam, int maxPlate, int minPlateW, int maxPlateW,
int chnRegion, LPRRatioRect recogRegion, int speed, bool bCapture, bool bAdaptiveFP, bool bEnableLowConfid );

其中包含结构体

struct LPREngineParam
{
int size;
LPRParam lprParam;
LPRRatioRect recogRegion;

LPRRatioLine line1;
LPRRatioLine line2;

int maxSkip;
float minValid;
float minConfid;

int framePercent;
bool bAdaptiveFP;
int motionDetectionMode;
bool bCapture;
int minCaptureDiff;
LPRRatioRect preferCapRegion;
};


我在C#下是这样实现的
定了一结构体如下

public struct LPREngineParam
{
public int size;
public LPRParam lprPrarm;
public LPRRatioRect recogRegion;
public LPRRatioLine line1;
public LPRRatioLine line2;
public int maxSkip;
public float minValid;
public float minConfid;
public int framePercent;
public bool bAdapticeFP;
public int motionDetectionMode;
public bool bCapture;
public int minCaptureDiff;
public LPRRatioRect preferCapRegion;
}


创建方法申明为

[DllImport("LPREngine.dll", CharSet = CharSet.Auto)]
public static extern long InitLPREngineParamHelper(LPREngineParam pParam,
int maxplate, int minPlateW, int maxPlateW, int chnRegion, LPRRatioRect recogRegion,
int speed, bool bCapture, bool bAdaptiveFP, bool bEnableLowConfid);


出现异常为

对 PInvoke 函数“PCDLLTest!PCDLLTest.Form1::InitLPREngineParamHelper”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。

不好意思没分送大家了

...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liveinblue 2008-07-14
  • 打赏
  • 举报
回复
谢了,结贴!
liveinblue 2008-07-14
  • 打赏
  • 举报
回复
不好意思
忘记注明了

LPRParam //struct

LPRRatioRect //struct
gomoku 2008-07-14
  • 打赏
  • 举报
回复
1, In general, LP... is used to describe a pointer, so be careful not to mix them:

public struct LPREngineParam
{
public int size;
public LPRParam lprPrarm; // a pointer or a struct?
public LPRRatioRect recogRegion; // a pointer or a struct?
public LPRRatioLine line1;
public LPRRatioLine line2;
public int maxSkip;
public float minValid;
public float minConfid;
public int framePercent;
public bool bAdapticeFP;
public int motionDetectionMode;
public bool bCapture;
public int minCaptureDiff;
public LPRRatioRect preferCapRegion;
}


2, another easy change you may try:
[DllImport("LPREngine.dll", CharSet = CharSet.Auto)]
public static extern long InitLPREngineParamHelper(ref LPREngineParam pParam, ...)

According to C++ prototype, the first parameter must be a pointer, but LPREngineParam is a struct value.

110,017

社区成员

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

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

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