Communication Delphi with dll (parameters) C ++

Hi, I am having some problems integrating a DLL inside my Delphi 2007 application.

I suspect I'm doing something wrong with call parameters. At the moment, I have 2 questions, but I think they are related to eachother.

1) First call using a DLL: from a .h file:

extern "C" { __declspec(dllexport) HRESULT Startup(char* version); } 

This call should initialize the DLL and provide me with the version of the DLL. HRESULT must be 0, and the version pointer must contain the version.

My Delphi Code:

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

And the actual call:

 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; 

But as a result of 0, the call was successful.

Then my second problem: I need to call a function in a DLL that will open a COM port.

.h:

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

And my Delphi announce:

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

and I call it:

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

And here I get a rejection of the DLL in the res variable. I also have the source of the DLL, and the failure I get is related to the part where the DLL checks if the parameters are valid, if they are valid, it will continue, otherwise return the error I am getting now.

What he checks:

 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; 

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

I tried many combinations for types, in the end, I stuck to the conversion found at: http://www.drbob42.com/delphi/headconv.htm I also tried different ways to read the char pointer, but they all failed .. ...

So, at this point, I know that I am successfully communicating with the DLL, since I get different HRESULT for two calls, but I suspect that my parameters are not working as it should.

I am using Delphi 2007, and the C ++ DLL is with VS2010.

+5
source share
1 answer

The Startup declaration is rather suspicious:

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

It means:

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

Thus, there should not be var .

I got from your comments that the cdecl calling cdecl works for some of your codes. In this case, remove stdcall , since it overrides the previous cdecl .

The Open() declaration seems pretty good (I would use DWORD as a type, not Longint , especially since DWORD is Longword these days, but in Win32 they are the same size, so this will not matter much to you). And you seem to pass the correct parameters too.

You did not specify what the HRESULT value means, which you will return. But I assume that the COM5 port simply cannot be opened with these settings.

What can you do?

You must remove var from Startup() .

So you can try:

  • use cdecl instead of stdcall ( stdcall in your ad overrides cdecl )
  • to open various COM ports with various parameters
  • to decode the returned HRESULT .

Better diagnostics are not possible from a distance, without the same hardware and software, sorry.

You can read the article on conversion . It also contains several paragraphs that explain how to debug code to find out the correct calling convention. This will probably help you with a lot of problems with converting headers.

+1
source

All Articles