我希望在我的Delphi 7项目中使用C++ DLL。我已经成功地在我的Delphi 7项目中调用一个DLL函数,但在另一个参数为stringbuilder的函数中出现错误。
C++函数的详细信息如下(来自DLL的头文件)。
WN_COMM_ERR WINAPI MYFUNCTION(WN_COMM_HANDLE handle, const char *command, char *answer, unsigned long maxlen, unsigned long timeout_ms, WN_ANSWER_TYPE *type);
答案类型是枚举类型:
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,但是无法获取answer类型变量的值:-
C#声明:工作
[DllImport("wn_comm.dll", CharSet = CharSet.Unicode)]
public static extern UInt32 MYFUNCTION(IntPtr handle, string command, StringBuilder answer, UInt32 maxlen, UInt32 timeout_ms, ref Int32 answer_type);
调用C#函数:成功
WriteNowWrapper.MYFUNCTION(this.comm_handle, command, sbAnswer, (uint)sbAnswer.Capacity, Timeout, ref answerType)
当我从我的C#应用程序调用上述代码时,成功接收了answertype和sbanswer变量的值。
Delphi7声明:不起作用
function MYFUNCTION (handle:Longint; command:string;var answer:PAnsichar; maxlen:integer; Timeout:integer; VAR answretype:INTEGER) : longint; stdcall; external 'wn_comm.dll';
Delphi7 调用函数:无法正常工作
MYFUNCTION(self.comm_Handle,command,answer,2,Timeout,answerType)
当我从Delphi7应用程序调用MYFUNCTION时,它成功执行并更新了整数类型参数answertype中的值,但在answer变量中我得到了空白。有人能帮我看看我做错了什么吗?