Solved

Calling C-DLL from 2.0

Posted on 1997-04-17
3
263 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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
Firemonkey allowing RTL on android 6 76
firemonkey Android Listview Sort items 7 102
Browsing a TTreeView in Delphi 5 47
migrate this code to work on android 1 45
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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