[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Using DLL calls

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
mauling
Asked:
mauling
1 Solution
 
DrDelphiCommented:
You are using the wrong calling convention. Try

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

0
 
simonetCommented:
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
 
zealsoftCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rwilson032697Commented:
Listening
0
 
DrDelphiCommented:
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
 
yk030299Commented:
listen
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now