Solved

Using DLL calls

Posted on 2000-03-23
6
165 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
[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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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