dll中返回值错误,急?

clwk 2009-07-22 11:24:13

创建了一个MFC规则DLL,里面的一个函数如下:
头文件定义:
int extern "C" __declspec(dllexport) Readnum( int &num)

code 如下:

int Readnum( int &num)
{

int IDstr = 825374512;//

if ((IDstr > 6291455) || (IDstr < 2097152))
{
IDstr = 0;
num =0;

return -2;
}
else
{
num = IDstr;
return IDstr;
}


}


代码大意如上,但是我运行到最后的时候,就出错了,
报错为:
First-chance exception in sample.exe: 0xC0000005: Access Violation.

实在不知哪里有错,请教一下,多谢各位。



...全文
294 42 打赏 收藏 转发到动态 举报
写回复
用AI写文章
42 条回复
切换为时间正序
请发表友善的回复…
发表回复
clwk2000 2009-08-03
  • 打赏
  • 举报
回复
结贴。

问题搞清楚了,是Dll中的一个数组定义的buffer小了,引起的,多谢大家帮忙。
clwk 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 38 楼 qrlvls 的回复:]
参数没问题,用引用也没问题,问题可能出在你的调用约定不一致

[/Quote]

都是__cdecl *

还是没找到问题出在哪。
clwk 2009-07-31
  • 打赏
  • 举报
回复
[Quote=引用 34 楼 xylicon 的回复:]
看看你exe 和 dll 中用的Code Generation 中的 Use Run-time library 是不是一样。如果不一样的,改为一样的试试看。
[/Quote]

都是Multithreaded的。

但是函数执行完,传递出来的num经常是一个很大的负数,
在debug下面,我的dlg经常不能updatedata(), 报错为assert iswindow 错误。
clwk 2009-07-31
  • 打赏
  • 举报
回复
现在可能问题就是从DLL 函数返回整型数时,有些地方不对,或者是调用过程有问题,但是确实没啥不同之处。
qrlvls 2009-07-30
  • 打赏
  • 举报
回复
参数没问题,用引用也没问题,问题可能出在你的调用约定不一致
clwk 2009-07-27
  • 打赏
  • 举报
回复
比较一下
做鸡真好吃 2009-07-24
  • 打赏
  • 举报
回复
mark~
xge 2009-07-24
  • 打赏
  • 举报
回复
how is DCDLL_API defined. because when calling the function, it is defined as
int extern "C" __declspec(dllexport) Readnum( int &num)
there is no specification of calling convention, that means the calling convertion would be the default(depending on /Gd /Gr or /Gz flag in the project setting).

when defining the function in your dll project, the calling convention is specified as DCDLL_API. It has to match the exe project setting. try this in you exe prjoect "int extern "C" DCDLL_API __declspec(dllexport) Readnum( int &num)"
clwk 2009-07-23
  • 打赏
  • 举报
回复
没有数组, 这个问题太奇怪了,会导致一连串的错误出来。
catstar0516 2009-07-23
  • 打赏
  • 举报
回复
这个错误表示内存的获取越界了啊~~~你有没有定义数组? 另外,把int 改为long试一下呢?
xylicon 2009-07-23
  • 打赏
  • 举报
回复
看看你exe 和 dll 中用的Code Generation 中的 Use Run-time library 是不是一样。如果不一样的,改为一样的试试看。
clwk 2009-07-23
  • 打赏
  • 举报
回复
头文件是这样的:

#ifdef DCDLL_EXPORTS
#define DCDLL_API extern "C" __declspec(dllexport)
#else
#define DCDLL_API extern "C" __declspec(dllimport)
#endif

DCDLL_API int Readnum( int &num) ;


很简单,就着一个函数
yhhxq 2009-07-23
  • 打赏
  • 举报
回复
lz不妨将extern "c"去掉试一下, 或者将导出的函数改为下面的形式试一下
int extern "C" __declspec(dllexport)  Readnum( int *num)
xge 2009-07-23
  • 打赏
  • 举报
回复
moreover, look at the compile options of you exe project and dll project specificly for Gd Gr and Gz switch. those are for the default calling convertion of the project. If you do not specify a specific calling version for function Readnum and the default call convertion of the two project does not match, then you will see this problem. You are lucky(unlucky) that you exe project links at all.
xge 2009-07-23
  • 打赏
  • 举报
回复
If this is the complete source code, the root cause might be calling convention mismatch. Could you paste the .h file of the read.dll. If you have a def file, could you paste the content of the def file?
clwk 2009-07-23
  • 打赏
  • 举报
回复
代码:创建的是MFC规则DLL

read.dll
头文件定义:
int extern "C" __declspec(dllexport) Readnum( int &num)

cpp 如下:

int Readnum( int &num)
{

int IDstr = 825374512;//

if ((IDstr > 6291455) || (IDstr < 2097152))
{
IDstr = 0;
num =0;

return -2;
}
else
{
num = IDstr;
return IDstr;
}

}

在sample.exe中调用,静态调用。

void CsampleDlg::Onread()
{
int IDstr =0;

int ret = Readnum(IDstr);
if (ret<0)
{
IDstr =0;
m_notifyInfo = _T("Wrong ID"); //有时候是在这个地方报错,报错内容如上面所说;和dll之中报错内容一样

UpdateData(FALSE);
return ;

}
else
{
m_Sread.Format("%d", IDstr);
UpdateData(FALSE);
return ;
}

}

其中,m_notifyInfo 的定义:
DDX_Text(pDX, IDC_NOTIFY, m_notifyInfo);
静态控件,CString类型;
m_Sread 是编辑控件:
CString m_Sread;


这应该基本很清楚了吧,可能与地址传递有关,造成了冲突,我只是不知道该如何改正。

多谢各位帮忙!
xge 2009-07-23
  • 打赏
  • 举报
回复
另外yayafan指出的调用约定不匹配也有可能.这些问题都搞清楚你也该晋级了
xge 2009-07-23
  • 打赏
  • 举报
回复
1. look for stack overflow. your return address got written over. 光贴出程序大意是不行的。如果不方便贴整个程序就得自己debug了。Set a condition break point for a write operation at the address of you method return address. whoever writes to that that location will cause the thread to break.
2. look for any local object that need to destruct during function exit. maybe there are errors in the destruction code.
ChamPagneZ 2009-07-23
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 clwk 的回复:]
VC 下int 和 long 的长度是一样的。
[/Quote]
不是你贴出的那段代码的问题.
问题在别的地方,你想让大家帮你猜么?
clwk 2009-07-23
  • 打赏
  • 举报
回复
VC 下int 和 long 的长度是一样的。
加载更多回复(22)

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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