Solved

Calling C-DLL from 2.0

Posted on 1997-04-17
3
248 Views
Last Modified: 2010-08-05
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 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
3 Comments
 

Expert Comment

by:Tonetheman041797
ID: 1335426
I think that you should first change your calling convention in your declaration of the routines PcdComOpen and PcdRdVersion. If you are trying to use a Win32 DLL the calling convention should stdcall so your routines should look like :
function PcdComOpen(LPCONNEC:PCDCONN):Integer;
                     stdcall; external 'SCOM32';
                    function PcdRdVersion(LPCONNEC:PCDCONN;Version:PChar):Integer;
                     stdcall; external 'SCOM32';

The problems that you describe could be caused by a wrong calling convention. You must test it to make sure that I have given you the correct answer.
0
 
LVL 2

Accepted Solution

by:
alona041797 earned 50 total points
ID: 1335427
You defined the functions as follows:

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

You have used the "pascal" keyword which sets the calling convention to pascal-style. The DLL was written in C so you need to use "C-style" calling convention i.e.  "stdcall".

Fixed declarations:

function PcdComOpen(LPCONNEC:PCDCONN):Integer;
                   far; stdcall; external 'SCOM32';
                  function
                  PcdRdVersion(LPCONNEC:PCDCONN;Version:PChar):Integer;
                   far; stdcall; external 'SCOM32';

0
 

Author Comment

by:rene041697
ID: 1335428
I had tried 'stdcall' already because C++ says that 'WINAPI is equivalent with far_pascal, pascal. But anyway i tried it again, and still got the same result: I get an error message -->

 'Raised exception class EaccessViolation with message 'Access     violation at adress 0000009. Read of address 709F9147'

I tried this also with the other calling conventions like 'register','cdecl'. But 'pascal' was the only one were i didn't got this error message.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 139
HTML text in the body of an email (delphi code) 12 94
Magic Software info 18 124
Delphi 2 46
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…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

12 Experts available now in Live!

Get 1:1 Help Now