Delphi如何调用c++;带有typedef void*参数的dll函数?

Delphi如何调用c++;带有typedef void*参数的dll函数?,delphi,dll,wrapper,typedef,void,Delphi,Dll,Wrapper,Typedef,Void,我有一个用C写的dll。 我的Delphi包装器正在调用C++ DLL中的函数。 这是C++代码: typedef enum EFTDeviceControlAction { EFT_DCA_CR_CARD_RETRACT = 0x01, EFT_DCA_CR_CARD_REPOSITION = 0x02, EFT_DCA_CR_SHUTTER_OPEN = 0x03, EFT_DCA_CR_SHUTTER_CLOSE = 0x04

我有一个用C写的dll。 我的Delphi包装器正在调用C++ DLL中的函数。

这是C++代码:

typedef enum EFTDeviceControlAction
{
        EFT_DCA_CR_CARD_RETRACT = 0x01,
        EFT_DCA_CR_CARD_REPOSITION = 0x02,
        EFT_DCA_CR_SHUTTER_OPEN = 0x03,
        EFT_DCA_CR_SHUTTER_CLOSE = 0x04,
        EFT_DCA_CR_CARD_EJECT = 0x05,
}

typedef enum EFT_PrintOptions {
        poPrintState = 0,
        poPrintFirst = 1,
        poPrintSubsequent = 2,
        poPrintFinal = 3,
        poPrintAbort = 9
} EFT_PrintOptions;


typedef void * EFT_HANDLE;

int EFT_CreateSession(EFT_HANDLE * h);
int EFT_DestroySession(EFT_HANDLE h);
int EFT_ReadProperty(EFT_HANDLE h, int table, int index, char * pValue, unsigned int maxLength);
int EFT_WriteProperty(EFT_HANDLE h, int table, int index, char * pValue);
...
这是delphi代码:

EFTDeviceControlAction = (
        EFT_DCA_CR_CARD_RETRACT = $01,
        EFT_DCA_CR_CARD_REPOSITION = $02,
        EFT_DCA_CR_SHUTTER_OPEN = $03,
        EFT_DCA_CR_SHUTTER_CLOSE = $04,
        EFT_DCA_CR_CARD_EJECT = $05,
);

EFT_PrintOptions = (
        poPrintState = 0,
        poPrintFirst = 1,
        poPrintSubsequent = 2,
        poPrintFinal = 3,
        poPrintAbort = 9
);

EFT_HANDLE = pointer;

function EFT_CreateSession(var h: EFT_HANDLE): Integer; stdcall; external 'api.dll';
function EFT_DestroySession(h: EFT_HANDLE): Integer; stdcall; external 'api.dll';
function EFT_ReadProperty(h: EFT_HANDLE; table: Integer; index: Integer; pValue: PChar; maxLength: Cardinal): Integer; stdcall; external 'api.dll';
function EFT_WriteProperty(h: EFT_HANDLE; table: Integer; index: Integer; pValue: PChar): Integer; stdcall; external 'api.dll';
我遇到的问题是行(C++)

这一行在Delphi中是如何定义的? 这是一个指针,程序吗???调用函数时,参数使用什么值

对于每个呼叫,我在模块中的地址
0040537B
处都会遇到访问冲突

typedef void * EFT_HANDLE;
声明类型的名称为
EFT\u HANDLE
,是
void*
的别名。而
void*
只是一个非类型指针

因此,在Delphi中,您可以这样定义它:

type
  EFT_HANDLE = Pointer;
var
  prop: AnsiString;
....
SetLength(prop, 128); // for example, not sure what value is needed here
retval := EFT_ReadProperty(handle, index, PAnsiChar(prop), Length(prop)+1);
// the +1 is for the null-terminator, but the library will specify exactly 
// how that is handled and it could equally be that the +1 is omitted
这正是你已经做过的

你其余的翻译看起来基本上相当合理。我有以下意见:

  • 您确定呼叫约定是
    stdcall
    ?你所显示的C++代码没有指定一个调用约定,并且它总是意味着“代码> CDDEL< /代码> .<
  • 使用
    PAnsiChar
    而不是
    PChar
    ,这样您的代码在Unicode Delphi和旧的非Unicode Delphi上都是正确的
  • 访问冲突的明显位置是以null结尾的字符串。查看调用
    EFT\u ReadProperty
    的代码会很有帮助。它需要如下所示:

    type
      EFT_HANDLE = Pointer;
    
    var
      prop: AnsiString;
    ....
    SetLength(prop, 128); // for example, not sure what value is needed here
    retval := EFT_ReadProperty(handle, index, PAnsiChar(prop), Length(prop)+1);
    // the +1 is for the null-terminator, but the library will specify exactly 
    // how that is handled and it could equally be that the +1 is omitted
    

    谢谢你的帮助。我不确定是否有stdcall。我如何检查我不喜欢使用哪种电话??您读取C++头文件。如果声明与问题完全一致,那么它是
    cdecl
    。它是cdecl调用,我使用PAnsiChar,现在正在工作。谢谢大家的帮助