Solved

Using DLL calls

Posted on 2000-03-23
6
158 Views
Last Modified: 2010-04-04
I had created a dll with the MFC Wizard (Project of the type Win32 Dynamic-Link Library). This dll had just one function call in it as shown below.

extern "C"
__declspec (dllexport) __int32  pipt_teste( __int32 *pt )
{
      *pt = 50;
      return 0;
}


And then I tried to use it in my delphi program.

In the mainframe I created a menu option called 'Test', and then when this option was called it create a form dynamically with a button in it. When the button was activated, it call the pipt_teste. The code is shown below:


function pipt_teste( pTeste : Pointer ) : Integer; stdcall; external 'prjDLL.dll'

....

procedure TFrm.BtOkClick(Sender: TObject);
var
   teste : Integer;
begin

   if pipt_teste( @teste ) then
      modalResult := mrOk
   else
      modalResult := mrAbort;

end;


What happening was, after calling the pipt_teste function, something was lost, the modalResult was not available more, the same happened to all attributes within the class.

What is wrong with my code?

Thanks for the help.
0
Comment
Question by:mauling
6 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2650600
You are using the wrong calling convention. Try

function pipt_teste( pTeste : Pointer ) : Integer; cdecl; external 'prjDLL.dll'

0
 
LVL 15

Expert Comment

by:simonet
ID: 2651396
Try this:

function pipt_teste(var pTeste : longint ) : Integer; stdcall; external 'prjDLL.dll'

It cannot be CDECL, because it's declared as STDCALL in the DLL.

Yours,

Alex
0
 

Accepted Solution

by:
zealsoft earned 100 total points
ID: 2651517
The declrarion of MFC DLL is not correct. If you use __declspec (dllexport) to declare a function, VC will use decorated names, so you can't call it correctly in Delphi.You need to change as follows,

1. Change your C function:
extern "C"
__stdcall __int32  pipt_teste( __int32 *pt )
{
*pt = 50;
return 0;
}

2. Createa prjDLL.def file and add it to your VC project.
In the .def file:
      EXPORTS
         pipt_teste        

3. Change your Delphi declation
function pipt_teste(var pTeste : integer ) : Integer; stdcall; external 'prjDLL.dll'

4. When you call the function, you can use
 if pipt_teste( teste ) then
0
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.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2651643
Listening
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2651979
Where does anyone see STDCALL as the calling convention in the original question's DLL code? The answer is, was and will always be the calling convention.change it in the DLL or change it in the Delphi app, but change it.

0
 
LVL 1

Expert Comment

by:yk030299
ID: 2659384
listen
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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

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

19 Experts available now in Live!

Get 1:1 Help Now