Calling a C++ DLL from Delphi 3

Hi...
  I'm trying to call a 3rd party sort routine which is in the form of a C++ DLL. The documentation included in v3 really stinks on this topic. My app is called MYAPP.EXE. Anyway, here's what I'm doing:

First the C++ DLL I'm trying to call is the file 'pdqsort.dll'. The function within that file is 'pdqsort'.Also included with this is a pdqsort.lib file...which I don't know if I need or not. Here is a snippet from the Pdqsort manual on how to call this DLL:


Application Program Interface
Pdqsort is invoked when a user-written program uses the following interface. "PdqSort" is the name of the entry
point, but "SMARTsort" can be used instead, since the interface is SMARTsort-compatible.
rc = PdqSort ( pdqparms, source_buffers, target_buffers, input_exit, output_exit, NULL, NULL)
rc
is the return code from Pdqsort. A value of zero indicates success. A non-zero
value indicates either an error or an unusual situation. The value generally
corresponds to the most recent error message number generated by Pdqsort.
See the chapter on "Messages" for further details.
pdqparms
is a pointer to a null-delimited string in memory. The string should contain valid
Pdqsort parameters that are described in the "Parameters" chapter.
char * pdqparms = "-x'format key char 8 char 72'
-k key -o output.file input.file"
source_buffers
is an optional pointer to a sequence of one or more PDQSORT buffer
structures, described below. If no source buffers are passed then a NULL
pointer must be specified. All buffer structures must reside in contiguous
memory, terminated by a word containing binary zeroes (a NULL). There need
not be any relationship between the number or size of any target and source
buffers.
PDQSORT_BUFFER sbs[3];
/* Pass 15 records in two different memory areas */
sbs[0].buffer = memory_ptr_1;
sbs[0].buffer_size = 80*8; sbs[0].nbytes_used = 0;
sbs[1].buffer = memory_ptr_2;
sbs[1].buffer_size = 80*7; sbs[1].nbytes_used = 0;
sbs[2].buffer = NULL; /* End of list indicator */
target_buffers
is an optional pointer to a sequence of one or more PDQSORT buffer
structures. If no target buffers are passed then a NULL pointer must be
specified. All buffer structures must reside in contiguous memory, terminated
by a word containing binary zeroes (a NULL). There need not be any
relationship between the number or size of any target and source buffers. If
no output file is used, then the target buffers must be large enough to hold the
sorted records. Pdqsort will fill the target buffers in the order listed. Records
may span target buffers.
input_exit
is an optional pointer to a user-written input exit procedure. If no input exit
is present, then a NULL value must be passed.
output_exit
is an optional pointer to a user-written output exit. If no exit is present, then
a NULL value must be passed.
The final two NULL pointers are reserved for future use.


..The folks at pdqsort told me that all these variables in the pdqsort function call are pointers to strings, and the only one I need to worry about is the first one. The rest I can just set as NULL pointers.

OK,now in my Delphi program I put this code in my main Unit in the "Implementation" section (is this correct?)

function PdqSort(pdqparms:ptstr; s_buf:ptstr; t_buf:ptstr; i_exit:ptstr; o_exit:ptstr; null1:ptstr; null2:ptstr): integer; far; external 'pdqsort.dll';

(I declare ptstr as ...ptstr = ^string;)

..Later on in my unit I call the DLL like so:

pdqparms^:='-z 80 -u -o '+ homedir + 'inpsort.txt '+ input_str;
result := PdqSort(pdqparms, s_buf, t_buf, i_exit, o_exit, null1, null2);

..I compile and build it ok, but when I go to run it, it comes up with an error window: "Error Starting Program: The MYAPP.EXE file is linked to missing export PDQSORT.DLL:PdqSort

..When I click OK on that error, there's another one right underneath it saying "Error: Unable to create process".

What am I doing wrong...DLL's are confusing enough, let alone trying to call a 3rd party's one, where you can't get into the source code in order to see how the heck they have it set up. Can you help?

Thanks...
   Shawn Halfpenny
   drumme59@sprint.ca
aztecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

interCommented:
Important notes:

Pointer to string does mean NULL terminated string in C++ in general so, your decleration above is incorrect. So they most likely be PChar;(it is known in delphi, you do not need to declare it). Another important think is DLLs exports functions in stdcall model. So the function be

Function PdqSort(pdqparms, s_buf, t_buf, i_exit , o_exit, null1, null2: PChar): integer; stdcall; external 'pdqsort.dll';

Here is an example how to declare a null terminated string:

var
  pdqparms : array[0..255] of char;
begin
  // delphi automatically understands that this is a
  // null terminated string and append #0 to the end
  pdqparms = '-x''format key char 8 char 72''-k key -o output.file input.file'
  if PdqSort(pdqparms, nil, nil, nil, nil, nil, nil)<> 0 then
     ShowMessage('Error in pdqsort');
end;


May this help
Igor
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aztecAuthor Commented:
Hello Igor...
  I have done all you suggested and it compiles and builds OK,  however I am STILL getting those same error windows when I try to run it. There must be something fundamentally wrong with what I'm doing or somehow it cannot find the pdqsort.dll file. I'm stumped.

Hope you have more suggestions!

Cheers!
   Shawn Halfpenny
   drumme59@sprint.ca
0
interCommented:
Hi Aztec,

1 - Are you SURE this DLL is a 32 bit DLL(rightclick and quickview on it in explorer and look at the header)?

2 - When program start is it stucked just in the begining of the .DPR file before creating any forms and calling application.Run?

IF

** Q1 - NO
  We should use somekind of thunking if this is the case

** Q1 - YES AND Q2- YES
  We still can not give the prototype(external decleration ) correctly. We should fix it.

** Q1 - YES AND Q2- NO
  Dont wory, we (DELPHI actualy) have succesfully, dynamically link to the DLL. We have a parameter format problem.

Please respond
Igor


0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

aztecAuthor Commented:
Igor...
   What is your e-mail address? I will e-mail you the DLL (only 201K), and you can try to call it from a Delphi app...ok? This will save us both a lot of time and questions.

Cheers
   Shawn
   drumme59@sprint.ca
0
interCommented:
Sure but, give an example to the pdgparams since I do not have the documentation?

inter@kosgeb.tekmer.gov.tr
Bye
Igor
0
aztecAuthor Commented:
Hello Igor...
   We finally figured out how to get thru to that C++ DLL. The maker of the DLL had to fix things on his end! Thanks for all your effort on this problem. Did I give you a grade yet on this? If not, give yourself an 'A' !

Cheers
   Shawn Halfpenny
0
interCommented:
Thanks friend,

I am happy for the problem is over.

Bye and regards,
Igor
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.