VC写的dll的定义
extern "C"_declspec(dllexport) unsigned char* GenAuthenNO(unsigned char *MachineNO, unsigned char *AuthenNO)
Delphi调用时的申明
SQRTPROC = function(MachineNO: array of BYTE; var pBuff: PByte): pbyte;stdcall;
程序运行过程中,如果pBuff赋值了,就不能运行,如果pBuff没有赋值了,就可以运行,
有谁知道怎么回事啊?

解决方案 »

  1.   

    不要用var 声明参数。
    指针对指针。
    SQRTPROC = function(MachineNO: PChar;  pBuff: PByte): pbyte;stdcall;
      

  2.   

    function GenAuthenNO(MachineNO, AuthenNO: PByte): PByte; cdecl; external 'xxx.dll'
    -------------
    var
      return: PByte;
    begin
      return := GenAuthenNO(PByte(PChar('11111')), PByte(PChar('22222')));
      showmessage(pchar(return));
    end;
      

  3.   

    把var去掉后仍然不行,只要对 pBuff操作,还是有错
      

  4.   

    function(MachineNO: Pchar; pBuff:PChar): PChar;stdcall;
      

  5.   

    SQRTPROC = function(MachineNO, pBuff:PChar): PChar;cdecl;调用之前是否为MachineNO, pBuff申请了空间?
    看声明应该默认的是cdecl方式
      

  6.   

    我事先定义了:
    iAuthenNO:          array[0..7] of BYTE;
    MachineNO:          array[0..7] of BYTE;
    pTemp:              pByte;
    然后:
    pByte := @iAuthenNO;
    所以,已经是申请了空间
      

  7.   

    实际上,extern "C"_declspec(dllexport) unsigned char* GenAuthenNO(unsigned char *MachineNO, unsigned char *AuthenNO)中的AuthenNO存放的是一串数字,我想在delphi中通过
    pByte得到该串数字,可是就是不成功,不知道为什么
      

  8.   

    我发现一个很奇怪的问题,就是VC DLL中声明:
    extern "C"_declspec(dllexport) int GenAuthenNO(unsigned char *MachineNO, unsigned char *AuthenNO)在delphi 6中定义:
    AUTHENROC = function(MachineNO: array of BYTE; AuthenNO: array of BYTE): Integer;stdcall;则只有MachineNO可以访问和修改,AuthenNO只要一修改,就出错了!非常奇怪,我可以保证
    MachineNO和AuthenNO是在调用GenAuthenNO函数前进行相同的初始化操作。哪位高手知道啊
      

  9.   

    AUTHENROC = function(MachineNO: array of BYTE; AuthenNO: array of BYTE): Integer;stdcall不要用array of byte这种动态数组类型,这是Delphi特有的,与VC不兼容,使用PChar,PByte
    或者:
    type
      TArray = array [0..7]of byte;
      PArray = ^TArray;
    var
      MachineNO,AuthenNO:PArray;使用时 New(MachineNo);动态申请空间另确认一下是否是stdcall方式
      

  10.   

    GenAuthenNO(MachineNO, AuthenNO: PChar): PChar;//PByte也行var
      MachineNO, AuthenNO: array[0..7] of Char;//of Byte也行调用
      GenAuthenNO(@MachineNO[0], @AuthenNO[0])
      

  11.   

    1.使用前请对需要传出的参数预先分配固定空间,这个不能交给vc的DLL来处理,当然传入的时候是指针。
    2、当然使用stdcall了,怎么会使用cdecl呢?
      

  12.   

    不好意思,第二点需要修正!如果Delphi为stdcall则,vc使用__stdcall
    否则Delphi用cdecl