与C ++ dll的Delphi通信(参数) [英] Delphi communication with C++ dll (parameters)

查看:62
本文介绍了与C ++ dll的Delphi通信(参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Delphi 2007应用程序中集成DLL时遇到了很多问题.

Hi I'm having quite some issues with integrating a DLL inside my Delphi 2007 application.

我怀疑呼叫参数做错了.目前,我有2个问题,但我认为它们彼此相关.

i suspect that I'm doing something wrong with the parameters of the calls. At this moment i have 2 issues, but i think they are related to eachother.

1)第一次使用DLL进行调用:从.h文件中:

1) First call with the DLL: from the .h file:

extern "C" {

__declspec(dllexport) HRESULT Startup(char* version);

}

此调用应初始化DLL,并为我提供DLL的版本.HRESULT应该为0,并且版本指针应包含版本.

This call should initialize the DLL and give me the version back of the DLL. HRESULT should be 0, and the version pointer should contain the version.

我的Delphi代码:

My Delphi code:

function Startup(var version: Pchar): HRESULT; cdecl; stdcall; external 'myDLL.dll';

实际通话:

var
  res : HRESULT;
  Name1 : PChar;
  test : AnsiString;
  buf2: array [0..20] of AnsiChar;
begin
  FillChar(buf2,20,0);
  Name1:= @buf2[0];
  res := RdmStartup(Name1);
//Here res = 0, but the Name1 stays empty, and the buf2 still contains 0.
end;

但是由于结果为0,因此调用成功.

But as the result is 0 the call was a success.

然后是我的第二个问题:我需要在DLL中调用一个将打开COM端口的函数.

Then my second issue: i need to call a function in the DLL that will open a COM port.

.h:

extern "C" {
__declspec(dllexport) HRESULT Open(HWND hWnd, int Port, DWORD BaudRate, DWORD Interval); 
}

我的Delphi声明:

And my Delphi declare:

function Open(hWnd: HWND;Port : integer;BaudRate:LongInt;Interval:LongInt): HRESULT; cdecl; stdcall; external 'myDLL.dll';

我这样称呼:

res:= Open(self.Handle,5,115200,500);

在这里,我从res变量中的DLL返回失败.我也有DLL的来源,而我得到的失败来自DLL正在检查参数是否有效的部分,如果它们有效,它将继续,否则返回我当前得到的错误.

And here i'm getting a failure back from the DLL in the res variable. i also have the source of the DLL, and the failure that i'm getting is from the part where the DLL is checking if the parameters are valid, if they are valid it will continue, else return the error i'm currently getting.

它正在检查的东西:

if(hWnd == NULL)
{
    return false;
}
if(BaudRate != 2400 && BaudRate != 9600 && BaudRate != 38400 && BaudRate != 115200)
{
    return false;
} 
if(IntervalTimer < 300)
{
    return false;
}
std::string strPortName = lexical_cast<std::string>( format("COM%d") % Port);
std::string strPortName(lpPortName.c_str());
std::string::size_type loci = strPortName.find("COM");
if( loci == std::string::npos )
{
    return false;
}
return true;

以上这些之一在我的调用中返回false,因为如果此函数的结果为false,则DLL会给出我当前在结果中得到的错误.有人知道我在做什么错吗?

And one of these above is returning false on my call, because if the result of this function is false, the DLL gives the error i'm currently getting in the results. Does anyone have an idea of what i am doing wrong?

我最终尝试了多种类型的组合,但我坚持要在以下位置找到的转换形式: http://www.drbob42.com/delphi/headconv.htm 我也尝试了不同的读取char指针的方法,但是所有方法都失败了.....

i've tried numerous of combinations for the types in the end i sticked to the conversion i found at: http://www.drbob42.com/delphi/headconv.htm i've also tried different ways of reading the char pointer, but all of them failed.....

所以在这个阶段,我知道我正在成功地与DLL通信,因为我为2个调用返回了不同的HRESULT,但是我怀疑我的参数不能正常工作.

So at this stage i know i am succesfully communicating with the DLL as i'm getting different HRESULTs back for the 2 calls, but i suspect my parameters are not working like the should.

我正在使用Delphi 2007,并且C ++ DLL是使用VS2010构建的.

I'm using Delphi 2007 and the C++ DLL was build with VS2010.

推荐答案

启动的声明非常可疑:

__declspec(dllexport) HRESULT Startup(char* version);

这翻译成:

function Startup(version: PAnsiChar): HResult; stdcall; external 'myDLL.dll';

所以那里应该没有 var .

我从您的评论中得知, cdecl 调用约定适用于您的某些代码.在这种情况下,删除 stdcall ,因为它会否决前面的 cdecl .

I got from your comments that the cdecl calling convention works for some of your code. In that case remove stdcall, since it overrules the preceding cdecl.

Open()的声明似乎还不错(我将使用 DWORD 作为类型,而不是 Longint ,特别是因为如今,> DWORD Longword ,但是在Win32中,它们的大小相同,因此对您没有太大的影响.而且您似乎也在传递正确的参数.

The declaration of Open() seems to be pretty OK (I would use DWORD as type, not Longint, especially since DWORD is Longword these days -- but in Win32 they are the same size, so that won't make any big difference for you). And you seem to be passing the right parameters too.

您没有写回 HRESULT 值是什么.但是我认为端口 COM5 根本无法使用这些设置打开.

You did not write what the HRESULT value is that you get back. But I assume that the port COM5 simply cannot be opened with these settings.

您应该从 Startup()中删除 var .

因此您可以尝试:

  • 使用 cdecl 代替 stdcall (声明中的 stdcall 取代 cdecl )
  • 打开具有不同参数的不同COM端口
  • 解码返回的 HRESULT .
  • to use cdecl instead of stdcall (the stdcall in your declaration overrules the cdecl)
  • to open different COM ports with different parameters
  • to decode the HRESULT that is returned.

如果没有相同的硬件和软件,很可能无法从远处更好地诊断.

A better diagnosis is not possible, from a distance, without the same hardware and software, sorry.

您可以阅读我的有关转化的文章.这也有几段说明如何调试代码以找出正确的调用约定.它可能也可以帮助您解决转换标头的更多问题.

You could read my article on conversion. This has also a few paragraphs that explain how to debug the code to find out the proper calling convention. It can probably help you with more of your problems converting headers, too.

这篇关于与C ++ dll的Delphi通信(参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆