Solved

Calling C-DLL Again

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

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

Suggested Solutions

Title # Comments Views Activity
FMX and jaudiotracker playing memory stream 29 107
SUM 2 INTEGER ARRAYS INTO 1 10 115
shape, triangle, dbctrlgrid 3 43
TlistView is Really heavy on Android 3 42
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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