Solved

Calling C-DLL Again

Posted on 1997-04-30
3
225 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 144
Correct Component for Shopping Cart. 2 112
find a node in VST 2 70
Firemonkey BASS_Init into a thread 17 30
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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