c++ written dll

Posted on 2000-02-15
Last Modified: 2010-04-04
i have a 3rd party c++ written dll. I have the source code and header files for 16 bit of the dll, but i don't have any documentation or header file for the 32 bit one. For a function, in 16 bit header it says:

int FAR PASCAL CallC2 (LPSTR par1,LPSTR par2, LPSTR par3, LPSTR cmd, LPSTR par4) ;

i supposed 32 bit version will have the same type, so i declared it in delphi application as:

 function CallC2 (par1,par2,par3:pchar;par4:pointer):integer;
        cdecl;external 'g:\dll3011\file01.dll' index 5;

The dll is for some socket connection to a unix OS for db connection. You typically allocate some memory first, send some information and the address for the allocated memory(in par4), and can see the results in the par4 parameter address.
my program sends this information, gets the function result successfully however when i try to end up with end parameter in the code, it gives access violation error. Is it something to do with shared memory, or parameters, or ??? Can you please help??

Question by:febalci
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
LVL 27

Expert Comment

ID: 2523503
The four parameters in the 16-bit version are of the same type; but your 32-bit one has the last parameter different.
   One technique is to allocate (or just reference) memory with a fixed pattern (hex 0a0a0a0a0 or whatever), pass this to the function and just printout the memory areas upon return. If the fourth parameter is not what it seems you'll either see what it replies with or you'll get an acess violation with your pattern (address 0x0a0a0a0a).

Expert Comment

ID: 2523994
it is definitely a problem with wrong declaration...

first of all show us real declaration in *.h and your code in Delphi

secondary, try to replace 'par4:pointer' with 'var par4', this probably will help
LVL 12

Expert Comment

ID: 2524626
You have missed out a paramter, change it to this:

function CallC2 (par1,par2,par3,cmd:pchar;par4:pointer):integer;
        cdecl;external 'g:\dll3011\file01.dll' index 5;

You should probably change the type of par4 to a PChar to be consistent with the others.

Also, make sure the calling convention (cdecl) matches the DLL calling convention.


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 2525929
Well rwilson, i tried pchar for par4, it didn't worked. i also tried stdcall and safecall too. They all failed.
Alish, i tried that var thing too. Upon return, the address changes, but neither the old address, nor the new one doesn't contain the data.
in fact the real declarations are as follows:

int FAR PASCAL CallC2 (LPSTR req_id,LPSTR dest1, LPSTR dest2, LPSTR cmd, LPSTR object,
                LPSTR seq, LPSTR tws, LPSTR twe, LPSTR selection, LPSTR fields,
                LPSTR data, LPSTR data_dest) ;

and in my application:

    function CalC2 (_req_id,_dest1,_dest2,_comand,_table,_order,_tws,_twe,_selektion,_fields:pchar;var _data:pointer;_destbuf:pchar):integer;
        cdecl;external 'g:\dll3011\file01.dll' index 5;

When i check out the network, i see that the socket connection is made, the SQL request is sent, and the reply has been get, but however the reply is not on the required address and then i get the access violation.

in 16 bit version source code, the point which i guess as making an access violation is that it gives data[0]=/0 line. data is declared as LPSTR in the dll, not an array and i really didn't understand what data[0] means. (Btw i'm not good in c++ as you can see).

LVL 12

Expert Comment

ID: 2525981
Your last two paramters should not be var. That will be causing a problem as the wrong pointer values are being passed to the DLL.

data[0] is refering to the first byte pointed to by data (it's just the way pointers to things and arrays are essentially the same things in C/C++).

data[0] = /0; means assign the char value 0 to item index 0.



Expert Comment

ID: 2526440
sorry, I made a mistake above...
try to declare in pascal as
....;var somedata;...
without any type, so when you call this function use
...., mybuffer_fordata,...
pascal will send an address of your buffer, in C DLL it is expecting a pointer, so they have to match now...
(actually LPSTR = ^Char; in C)

if it not working again ;-) please tell us exactly what params are IN and what are OUT in C function - I am not sure... common rule is OUT parameters should be declared with 'var' so their passed by reference

also try to declare in pascal with different convention (cdecl, stdcall, etc... ) because you are not sure if 16-bit header the same in 32-bit


Expert Comment

ID: 2526455
Don't cast the parameters as PChar but do the following:

  pPar1: PChar;
  GetMem(pPar1, Length(sString1));
  StrPCopy(pPar1, sString1);

  FreeMem(pPar1, Length(sString1));

I had a lot of problems with casting Strings as PChar. The only solution was to declare a PChar and copy the string with StrPCopy. Don't ask me why it don't worked...

Expert Comment

ID: 2526573
there is simpler solution to this, brubohe:

  MyBuf : Array[0..1023 {or something} ] of Char;
  s1 : String;
  call_pchar_needed_function( MyBuf );
  s1 := String( MyBuf );
  { or }
  ShowMessage( String( MyBuf ));

Expert Comment

ID: 2526969
 IMHO, trying to use undocumented DLL, you're playing in a very risky game (casino always wins ;-). Maybe the better (and simpler, and faster) way is to write simple thunk to well-defined 16-bit DLL ? It's really simple, you just need to obtain a free Microsoft's thunk compiler or use undocumented function QT_Thunk from kernel32.dll. I did it by the second way and all works very stable. There was a very impressive article in "The Delphi Magazine" (jan'98) by Brian Long with huge set of samples. You may find it on
LVL 27

Accepted Solution

BigRat earned 100 total points
ID: 2532481
All the parameters to the function are LPSTR which means "long pointer to c-string" in other words long pointer to an (packed) array of characters whose lower bound is zero. In every parameter posaition the function expects an array of characters. Therefore ALL of the Delphi parameters must be PChar and you must feed the function with real arrays :-
   p1 : array[0..n1] of Char;
   p2 : array[0..n2] of Char;
   p3 : array[0..n3] of Char;
   p4 : array[0..n4] of Char;

is a somewhat cumbersome way of doing it but perfectly compatible with the specification. Do not declare a variable as PChar and pass that - since there is NO DATA BEHIND IT and it will crash.

PS: Brunohe: Delphi DIRECTLY supports casting Delphi long strings to PChar so as to be compatible with C. I have had absolutely no problems in this area.

Author Comment

ID: 2534507
Well thunking caused a lot more trouble. And it didn't solved my problem. Thanx

Author Comment

ID: 2534511
Well thunking caused a lot more trouble. And it didn't solved my problem. Additionally i'm trying to make it work on NT. As far as i learned generic thunks don't allow to use 32 bit code with 16 bit dll.
But anyway, thanx.

Author Comment

ID: 2534519
strange bu it worked. Thanx to everyone who commented and answered.

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month4 days, 15 hours left to enroll

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question