Solved

Using DLL calls

Posted on 2000-03-23
6
155 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

16 Experts available now in Live!

Get 1:1 Help Now