Solved

Calling C-DLL Again

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

691 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