Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling C-DLL Again

Posted on 1997-04-30
3
Medium Priority
?
261 Views
Last Modified: 2010-04-06
Help !!

I trie to call a function from a DLL called scom32. The DLL is written in MSVC++ 4.00 and i only have the headerfile.
I have converted the headerfile to delphi and try to call the funtion. But something goes wrong with i think passing the function arguments. When the function i call only has one argument then the call works, but when i have two or more arguments the call actualy changes arguments wich normally would't change.

Here is what i did:

-------------- C-header:

typedef struct tagPCDCONN
{
  int Port;
  int Protocol;
  int Baud;
  BYTE Sbusmode;
  BOOL bEPROM;
  BYTE TxMsg[270];
} PCDCONN;

typedef PCDCONN FAR *LPCONN;

int WINAPI PcdComOpen(LPCONN);
int WINAPI PcdRdVersion(LPCONN,LPSTR);

-------------- This is my DELPHI equivalent:

type
 PCDCONN = Record
   Port    :Integer;
   Protocol:Integer;
   Baud    :Integer;
   Sbusmode:Byte;
   bEPROM  :Longbool;
   TxMsg   :Array[0..269] of Byte;
 end;

var

 LPCONN : ^PCDCONN;
 Version: Array [0..127] of AnsiChar;
 Status : Integer;

function PcdComOpen(LPCONNEC:PCDCONN):Integer;
         far; pascal; external 'SCOM32';
function PcdRdVersion(LPCONNEC:PCDCONN;Version:PChar):
         Integer; far; pascal; external 'SCOM32';
                     
-------------- And so i call the functions --------
 begin
  getmem(LPCONN,SizeOf(PCDCONN);
  PCDCONN^.Port:=0;
   etc.etc.
  status:=PcdComOpen(PCDCONN^); {1}
  status:=PcdRdVersion(PCDCONN^,Version); {2}
  freemem(LPCONN,SizeOf(PCDCONN);
 end;

When i check what is returned in the status then i can only say it works correct (in statement {1} as well in {2}). But in statement {2} the result in 'version' is wrong and 'PDCONN' value's are destroyed. Only the first two fields are not correct anymore, so i suspect it has something to do with memory allocation or with passing the function arguments.
I tried all the other calling conventions (stdcall,cdecl etc.) but the give a access violation error. Pascal is the
only calling method wich gives the result described above.
I have searched for two days but can't seem to find out what the problem is.

Is there anyone who can help me !!!

Kind Regards,

Rene Reuscher, ANTONIUS Vessel heads
0
Comment
Question by:rene041697
[X]
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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
slp earned 100 total points
ID: 1335811
>-------------- This is my DELPHI equivalent:
>type
> PCDCONN = Record
>   Port :Integer;
>   Protocol:Integer;
>   Baud :Integer;
>   Sbusmode:Byte;
>   bEPROM :Longbool;
>   TxMsg :Array[0..269] of Byte;
> end;

This basically looks okay, though I don't think a C "bool" translates to a Delphi longbool, wordbool maybe?

>var
> LPCONN : ^PCDCONN;

I wouldn't bother with this, see below

>Version: Array [0..127] of AnsiChar;
>Status : Integer;

>function PcdComOpen(LPCONNEC:PCDCONN):Integer;
>    far; pascal; external 'SCOM32';

I'd change this to:
function PcdComOpen( var PCD: PCDCONN );
  far; StdCall; external 'SCOM32' ;

>function PcdRdVersion(LPCONNEC:PCDCONN;Version:PChar):
>    Integer; far; pascal; external 'SCOM32';

Similarly:
function PcdRdVersion( var PCD: PCDCONN Version: PChar );
  far; StdCall; external 'SCOM32' ;


>-------------- And so i call the functions --------
> begin
>  getmem(LPCONN,SizeOf(PCDCONN);
>  PCDCONN^.Port:=0;

I'm assuming you've got your variable/type names mixed up a bit here, didn't you mean LPCONN^.Port ?

>   etc.etc.
>  status:=PcdComOpen(PCDCONN^); {1}
>  status:=PcdRdVersion(PCDCONN^,Version); {2}
>  freemem(LPCONN,SizeOf(PCDCONN);
> end;


Anyways, try:
var
  LPCONN : PCDCONN ;
begin
  LPCONN.Port := 0 ;
  ...
  Status := PcdCommOpen( LPCONN ) ;
  Status := PcdRdVersion( LPCONN, Version );
end ;

HTH

slp
0
 

Author Comment

by:rene041697
ID: 1335812
Great !! it works. The biggest problem i seem to have was the
missing 'var' statement in my function call because the function
changes my arguments. (Stupid,stupid of me). But then i find it strange that i didn't got an error message when i used the 'pascal' calling method.
Anyway, Thanks A lot!!!!
0
 
LVL 1

Expert Comment

by:slp
ID: 1335813
Actually not so strange, the "pascal;" calling directive is (I believe) largely functionally equivalent to the "StdCall;" directive.  As I understand it, StdCall is the D2 version that is supposed to remain compatible with any changes in the Windows DLL conventions whereas pascal matches the original spec for backward compatibility; since the convention hasn't changed significantly yet they are the same at the moment.

slp





0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

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