Solved

Calling C-DLL Again

Posted on 1997-04-30
3
196 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
slp earned 50 total points
Comment Utility
>-------------- 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now