Solved

Calling C-DLL from 2.0

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

617 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