?
Solved

Calling C-DLL from 2.0

Posted on 1997-04-17
3
Medium Priority
?
269 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
[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
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 100 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

718 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