Wrong value passed to C DLL with Delphi 2006 function call
(too old to reply)
2006-09-16 00:19:22 UTC

I have a digital receiver that is controlled by calls to a windows
system API DeviceIOControl() function. I can easily make the receiver
do what I want from a C/C++ program, so I know the receiver is
functioning properly, and I know I have the proper calling syntax for
the DeviceIOControl() call. The difficulty arises when I try to do the
same things from my Delphi program, even after making all the proper
language translations (NULL to nil, UINT to Cardinal, that sort of

My latest run at the problem is this: I wrote a very simple DLL in
C++ wit h just a single function that takes one UINT argument. Inside
this function I do everything required to run the receiver. The DLL
function also opens and writes to a log file so I can tell what got
passed to it, and what it did. Then I wrote another C++ program to
call the DLL function with a UINT value that tells the DLL function
what to do. I can run this combination all day long, and it works
perfectly. The DLL function's internal values are what I expect, and
the receiver itself behaves as expected.

Then I declared the DLL function in the Delphi program, got it to
compile/link, and then call this function from inside my Delphi
function with a simple hard-wired integer value. The Delphi program
compiles, links, finds the DLL on startup, and executes the function
call without problems. However, the argument seen by the DLL function
(as recorded in the DLL's log file) isn't anything like the argument
supplied by the Delphi program.

Here's the DLL function declaration and definition:

#define ESL_PENTEK_API __declspec(dllexport)
#define ESL_PENTEK_API __declspec(dllimport)

extern "C" ESL_PENTEK_API int fnESL_Pentek( UINT bwid );

ESL_PENTEK_API int fnESL_Pentek( UINT bwid )
FILTER *filt;
FILE *outfile ;
HANDLE hDevice;
int res ;
char deviceName[50];
char chandevice[50];
struct tm *newtime;
time_t aclock;

time( &aclock ); // Get time in seconds
newtime = localtime( &aclock ); // Convert time to struct tm form
outfile = fopen( "filtcoef", "w" ) ;//open, create if necc
fprintf( outfile, "filtcoef file created: %s\n", asctime( newtime ) )
fprintf( outfile, "filt contents for filter bwid = %d\n", bwid ) ;

//other stuff....

And the Delphi declaration/usage:

//09/15/06 test to see if I can link to ESL_Pentek.dll
function fnESL_Pentek( bw : cardinal) : integer ; external
'ESL_Pentek' index 3 ;

//other stuff.....

I := fnESL_Pentek( 17 ) ;

The value as seen by the DLL is something like 1243244 (0x12F86C)
instead of the expected 17. Of course, when I do this same trick from
the C++ calling program, everything works like a charm!

what the heck am I doing wrong here?


Riki Wiki
2006-09-17 11:34:01 UTC
Post by Frank
what the heck am I doing wrong here?
Hoi Frank

You need to repost your question on the Borland news server to make
everybody see it and possibly answer your question. Further, this newsgroup
do not officially exist, use newsgroup b.p.d.nativeapi.win32.

How to post to Delphi newsgroups: