var
  err:pchar;
begin
     GetMem(err,128);
     try
     GetErrorMsg(ErrCode,err);{此函数是vc编写的dll中的,参数类型为int,unsignedhar*,在delphi定义为integer,pchar,要求调用后err带回字符串。可是调用的时候总是说project mm.exe raised exception class EAccessViolation with message 'Access violation at address 00402342 in modul me.exe.Read of address 3030302c',Process stopped.Use Step or Run to continue.各位给看看到底是什么原因?谢谢了}
     finally
     FreeMem(err,128);
     end;
end;

解决方案 »

  1.   

    你声明的不对,第一VC可能会给函数名加一段莫名其妙的东西,这个需要加入,另外,就是vc默认得调用方式是cdecl,而Delphi却是register。
      

  2.   

    续chechy(为程序而奋斗):
    如果你有VC所写DLL的源代码,则需要重新声明GetErrorMsg函数,象这样:
    extern "C" declspec(dllexport) void __stdcall GetErrorMsg(int, unsigned char *); // 好像是这样的,忘了!:(而你为了在Delphi中使用它,你需要声明:
    Static call style:
    procedure GetErrorMsg(const AErrorCode: Integer; var AErrMsg: PChar); stdcall;
    //---------------------------------------------------------------
    Dynamic call style:
    TGetErrorMsg = procedure GetErrorMsg(const AErrorCode: Integer; var AErrMsg: PChar); stdcall
      

  3.   

    如果你没有VC写的DLL的源代码,那么很不幸,Delphi无法使用这个DLL函数!
      

  4.   

    我有vc中dll的源文件。
    按照大家的方法,我先试试看,对了如何给这个贴子在加些分数,以分给帮助我的朋友,我觉得20分太少了。 :)
      

  5.   

    我在vc中的声明为
    extern "C" void GetErrorMsg(int ErrCode, unsigned char *Error);
      

  6.   

    To sunjguang(风卷残云):
    在管理功能中加分。
      

  7.   

    那么最起码在Delphi中应该这么声明:
    procedure GetErrorMsg(AErrorCode: Integer; AErrMsg: PChar); cdecl;
      

  8.   

    我在delphi中的声明为:GetErrorMsgErrCode:integer;varError:pchar):real;cdecl;external 'GDHZDSSK.dll' name 'GetErrorMsg' ;
      

  9.   

    TommyTong(童童--青锋软件工作室) :能否讲讲declspec(dllexport) 是什么意思?我按照你的方法作了,就是declspec(dllexport) 不能通过。
      

  10.   

    我用的是w2k p是不是有冲突呀
      

  11.   

    我最近做的东西,也是VC写的DLL,但在DELPHI中调用,在VC中的声明类似于
    extern "C" declspec(dllexport) void GetErrorMsg(int, unsigned char *); 
    在DELPHI中的调用的形式类似于
    GetErrorMsgErrCode:integer;varError:pchar):pointer;stdcall;external 'GDHZDSSK.dll' 而且我在DLL中的函数中用了malloc分配地址空间,也定义了释放内存的函数,在DELPHI中使用时,分配地址空间没有问题,但一释放就报错了。
    另外在使用DLL中函数生成的链表时,在DELPHI中向其增加一个节点时,在节点生成的那个过程中(使用NEW或GETMEM的过程中),这个链表是正常的,但在其它地方对这个链表操作时,新加的节点的内容发生了变化(我有全局变量记录该链表,而且所加节点前的链表是对的)。我想谁能解释一个在DLL调用时,地址分配是怎么样的,在DLL中分配的空间,在外部可否以一般的方式操作
      

  12.   

    我遇到的问题基本和楼上的相同,也是释放的问题。
    forza(象风一样):declspec(dllexport) 是什么意思?
      

  13.   

    添加 __declspec(dllexport) 是为了提供不使用 .DEF 文件从 .EXE 或 .DLL 导出函数的简单方法。更正上一次的说法,跨语言,似乎一定要使用.DEF文件的。在VC中声明:
    Header文件中:
    extern "C" void __stdcall GetErrorMsg(int, unsigned char *);CPP文件中:
    void __stdcall GetErrorMsg(int ErrorCode, unsigned char *ErrorMsg)
    {
    // Your code
    }.DEF文件中:
    EXPORTS
      GetErrorMsg;Delphi中静态调用的声明:
    procedure GetErrorMsg(const ErrorCode: Integer; var ErrorMsg: PChar); stdcall;
      external "SharedDLL.DLL" name "GetErrorMsg";
    (其中SharedDLL.DLL是你的VC写的DLL文件名)