C# C++;Delphi7中使用的DLL函数 我想在我的Delphi 7项目中使用C++ DLL,在DLL的Delphi项目中,我成功调用了一个函数,但在另一个参数是StrugBuudor的函数中得到了错误。 C++函数细节如下(我从DLL的头文件中得到)
答案类型为枚举类型:-C# C++;Delphi7中使用的DLL函数 我想在我的Delphi 7项目中使用C++ DLL,在DLL的Delphi项目中,我成功调用了一个函数,但在另一个参数是StrugBuudor的函数中得到了错误。 C++函数细节如下(我从DLL的头文件中得到),c#,c++,delphi,dll,type-conversion,C#,C++,Delphi,Dll,Type Conversion,答案类型为枚举类型:- typedef enum { WN_ANSWER_ACK = '>', // An OK frame was received WN_ANSWER_NACK = '!', // An ERR frame was received WN_ANSWER_TOUT = 0, // Command timed out before an answer could be received }WN_ANSWER
typedef enum {
WN_ANSWER_ACK = '>', // An OK frame was received
WN_ANSWER_NACK = '!', // An ERR frame was received
WN_ANSWER_TOUT = 0, // Command timed out before an answer could be received
}WN_ANSWER_TYPE;
我已经成功地在c代码中调用了相同的函数,它正在工作。但我不知道如何在delphi7上调用它,因为答案参数是stringbuilder类型,而在delphi7中,它们不是任何stringbuilder类型的东西。我尝试使用Pansichar、string、pchar,但在应答类型变量中没有得到值:-
C#宣言:工作
调用C#函数:工作
当我从我的c#应用程序调用上面的代码时,我成功地接收到answertype和sbanswer变量值
Delphi7声明:不工作
Delphi7调用函数:不工作
当我从delphi7应用程序调用MYFUNCTION时。它成功执行并更新answertype中的值,该值是整型参数,但在answertype变量中我变为空
谁能帮我一下我做错了什么吗?键入
没有访问DLL的权限,很难说出问题所在。但是试着从这个开始
function WN_ExeCommandA (handle:Longint; command: PAnsichar;var answer:PAnsichar; maxlen:integer; Timeout:integer; VAR answretype:INTEGER) : longint; stdcall; external 'wn_comm.dll';
<>如果我能看到WNI应答器类型的C++定义,我可以帮助你更多。 您的Delphi函数声明不正确。您不能使用本机Delphi类型的
string
。答案参数不是var参数
宣言应当是:
function MYFUNCTION(
handle: Integer;
command: PAnsichar;
answer: PAnsichar;
maxlen: Cardinal;
timeout_ms: Cardinal;
out answer_type: Integer
): Cardinal; stdcall; external 'wn_comm.dll';
问题中缺少的细节是分配答案的部分。你显然是在C代码中这样做的。看起来是这样的:
StringBuilder sbAnswer = new StringBuilder(256);
我已经猜到了容量的使用价值。在Delphi调用代码中,您需要类似的东西
var
handle: Integer;
command: AnsiString;
answer: AnsiString;
timeout_ms: Integer;
answer_type: Integer;
err: Integer;
....
handle := ...;
command := ...;
SetLength(answer, 256);
timeout_ms := ...;
err := MYFUNCTION(
handle,
PAnsiChar(command),
PAnsiChar(answer),
Length(answer),
timeout_ms,
answer_type
);
它只是一个enum typedef enum{WN_ANSWER_ACK='>',//收到了一个OK帧WN_ANSWER_NACK='!',//收到了一个ERR帧WN_ANSWER_TOUT=0,//在收到应答之前命令超时}WN u ANSWER_TYPE@大卫·费弗南那么什么是对的?你能解释一下我应该使用什么类型的数据类型吗?@Pri我在我的answer@PriyankaMehta:变量var
错误。答案很简单:潘斯卡尔
PAnsiChar
已经是指针(char*
)。使用var
会增加一级间接寻址,这是错误的。我已经贴过了,但是再一次:。选民愿意解释一下,还是这只是盲目的报复?FWIW,和
MYFUNCTION(self.comm_Handle,command,answer,2,Timeout,answerType)
function WN_ExeCommandA (handle:Longint; command: PAnsichar;var answer:PAnsichar; maxlen:integer; Timeout:integer; VAR answretype:INTEGER) : longint; stdcall; external 'wn_comm.dll';
function MYFUNCTION(
handle: Integer;
command: PAnsichar;
answer: PAnsichar;
maxlen: Cardinal;
timeout_ms: Cardinal;
out answer_type: Integer
): Cardinal; stdcall; external 'wn_comm.dll';
StringBuilder sbAnswer = new StringBuilder(256);
var
handle: Integer;
command: AnsiString;
answer: AnsiString;
timeout_ms: Integer;
answer_type: Integer;
err: Integer;
....
handle := ...;
command := ...;
SetLength(answer, 256);
timeout_ms := ...;
err := MYFUNCTION(
handle,
PAnsiChar(command),
PAnsiChar(answer),
Length(answer),
timeout_ms,
answer_type
);