Solved

Calling C-DLL from 2.0

Posted on 1997-04-17
3
243 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now