我写了个DLL,里面有一个导出函数,我在调试他的时候发现,他一执行return语句就出错,不懂为啥,我在想是不是函数的调用约定不对,请高手指教,代码如下://////////////////////////////////////////////////////////////////////////
// File name: modNoteinfo.cpp
#include <process.h> //this header file is define the _beginthread function
#include <afxwin.h>
#include "modNoteinfo.h" //in this file define the output interface
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////////
// Global variables
//
static HMODULE g_hModule = NULL ; // DLL module handle
static HMODULE g_hGMSDLL = NULL; // Note mode link library handle
static char* g_pszLastError = NULL; // Last Error Message
static int g_nRef = 0; // The library using times countBOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, void* lpReserved)
{
switch (dwReason )
{
case DLL_PROCESS_ATTACH:
if (NULL == g_hModule)
g_hModule = (HMODULE)hModule ;
g_nRef ++;
break;
case DLL_PROCESS_DETACH:
g_nRef --;
if (0 == g_nRef && NULL != g_pszLastError)
{
delete[]g_pszLastError;
g_pszLastError = NULL;
}
if (NULL != g_hGMSDLL)
{
FreeLibrary(g_hGMSDLL);
g_hGMSDLL = NULL;
}
break;
default:
break;
}
return TRUE ;
}STDINTMETHOD InitNoteInfo(int nCommport)
{
if (NULL == g_pszLastError)
g_pszLastError = new char[1024];
if (NULL == g_hGMSDLL)
g_hGMSDLL = LoadLibrary("..\\bin\\SMS.dll");
Sms_Connection pProcConnGSM;
pProcConnGSM = (Sms_Connection)GetProcAddress(g_hGMSDLL, "Sms_Connection");
CString strMobileType[265];
CString strCopyToCOM[265];
int nRet;
CString strCopyright;
strCopyright = "//上海迅赛信息技术有限公司,网址www.xunsai.com//";
nRet = pProcConnGSM(strCopyright, nCommport, 19200, strMobileType, strCopyToCOM);
TRACE("InitNoteInfo(int): nRet= {%d}", nRet);
if (0 == nRet)
{
if (NULL == g_pszLastError)
g_pszLastError = new char[1024];
strcpy(g_pszLastError, "Initializing the GMS Modem happen error!");
}
return nRet;
}......../////////////////////////////////////////////////////////////////////////////
//
// File: modNoteinfo.h
#ifndef __MODNOTEINFO_H__
//
// Macro defines 
//
// this macro defined for avoid this header file replay import
#define __MODNOTEINFO_H__
//
// STDINTMETHOD macro is define this library standard export method
#define STDINTMETHOD extern "C" int __stdcall
// all interface method of this library provide listSTDINTMETHOD InitNoteInfo(int);
#define STDINTMETHOD
.......
#endif
// File name: modNote.def
LIBRARY         modNoteinfo.dll
DESCRIPTION     'cwcyClient -- Note group broad module'EXPORTS
InitNoteInfo @2 PRIVATE
......

解决方案 »

  1.   

    函数返回出错,极有可能是调用约定
    pProcConnGSM是__stdcall的么。?
      

  2.   

    在VisualStudio项目属性->c/c++->高级中,默认的调用约定是__cdecl。
    你的DLL函数默认是__stdcall,因此在调用DLL的时候声明函数原型,一定要使用__stdcall。
      

  3.   

    那你确定..\\bin\\SMS.dll函数导出使用的__stdcall吗?
      

  4.   

    在引入工程中也要声明Sms_Connection为 __stdcall啊
      

  5.   

    不感保证,但是我觉得与那个应该无关吧,因为是在InitNoteInfo这个函数的返回时出错的,与那个有关吗?我把Sms_Connection声明为__stdcall的,问一下,在VC里面,_stdcall与__stdcall是一样的不
      

  6.   

    看了下,疑点有两个:
    一、声明为extern "C"的话,就是以C的语法规定吧
    我看有几个变量声明不在开始,而在中间部分二、函数声明为私有导出函数
      

  7.   

      g_hGMSDLL = LoadLibrary("..\\bin\\SMS.dll");
    换成绝对路径试试
      g_hGMSDLL = LoadLibrary("..\\bin\\SMS.dll"); pProcConnGSM = (Sms_Connection)GetProcAddress(g_hGMSDLL, "Sms_Connection");
    两处调用都没有判断是否成功如果加载失败,调用肯定有问题
      

  8.   

    问题已解决了,我在EXE文件调用那儿做了一下改变,然后错误就没有了,
    if(InitNoteInfo())
    {...}
    else
    {...}
    我改成了
    if(1 == InitNoteInfo())
    {...}
    else
    {...}
    然后错误就没了,但是我就不明白为什么了,能给个理由吗?
      

  9.   

    // STDINTMETHOD macro is define this library standard export method
    #define STDINTMETHOD        extern "C" int __stdcall
    // all interface method of this library provide listSTDINTMETHOD InitNoteInfo(int);
    #define STDINTMETHOD  // ??? 这是什么意思?上面定义的内容又被取消了?
      

  10.   

    谢谢你的提示,我这人写代码比较粗心,忘了这个了,而且我也没有保存为.C,保存为.CPP的,所以VC6也没有认为是错的,嘿嘿,结贴了
    哦,那个如果谁知道为什么会出现这种情况,就是加个 1 == 后就没错了,如果谁知的话,回一下我哈,谢谢了,邮箱是[email protected]