Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have one dll that used by one platform(Metatrader) and another exe file.
I define one Public variable(PP) in header code and set value in
MT4_EXPFUNC double __stdcall Reza(double ppp) function.

When try call this dll from my exe it returns only Zero(0) value but if call from other platform(Metatrader) returns correct value.
C++
#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

double price();
double pp;

#define MT4_EXPFUNC __declspec(dllexport)

BOOL WINAPI DllEntryPoint(HINSTANCE hDll,DWORD dwReason, LPVOID Reserved)
{
	switch(dwReason)
	{
	case DLL_PROCESS_ATTACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
  




MT4_EXPFUNC double __stdcall Reza(double ppp)
  {
   pp=ppp;
  
   return (pp);
  }


double price(){
double dd;
dd=pp;
return dd;
}


How I must define public variable to return correct value?
Regards,
Posted
Comments
[no name] 9-Oct-12 12:02pm    
Probably has nothing to do with your declaration. Are the calling conventions the same when calling your function from the other applications?
Rezame 9-Oct-12 12:04pm    
Yes, same.
Try call that double type too.
pasztorpisti 9-Oct-12 13:19pm    
What do you see in the debugger? Try to put some printffing (or outputdebugstring, or logging or whatever) as well into your code.
Rezame 9-Oct-12 14:20pm    
I used MessageBox:
function: MT4_EXPFUNC double __stdcall Reza(double ppp) shows correct value
function: double price() shows incorrect, zero(0) value
pasztorpisti 9-Oct-12 14:54pm    
Use a debugger instead.

I think you should use the same calling convention for your price() function, i.e. declare it like:
C++
MT4_EXPFUNC double __stdcall price()
 
Share this answer
 
Comments
Rezame 9-Oct-12 17:21pm    
I tried same calling methods too.
When call both functions from same platform(Metatrader) works correct, but call each function from different platform returns incorrect.
chaau 9-Oct-12 17:44pm    
Where do you initialise your global variable pp? What exact line of code you use?
Mohibur Rashid 9-Oct-12 20:12pm    
he is doing at initialization in reza function.
chaau 9-Oct-12 20:37pm    
Yes, but is he calling reza() function before price() within the same process? He might expect that calling the function within the exe will bring the value to "other platform(Metatrader)" (whatever it means)
Doing so, you have got two different instances of pp. One of them is in DLL and another in EXE. You should use dllexport for DLL header and dllimport for EXE header. Something like this:
C++
...
#ifdef M_DLL
__declspec(dllexport) double pp;
#else
__declspec(dllimport) double pp;
#endif
...


http://msdn.microsoft.com/en-us/library/9h658af8.aspx[^]
 
Share this answer
 
v2
Comments
Rezame 10-Oct-12 13:15pm    
When add above lines get this errors:

Creating library C:\Users\Reza\Desktop\Test_Projects\C#\Reza2\Debug\Reza2.lib and object C:\Users\Reza\Desktop\Test_Projects\C#\Reza2\Debug\Reza2.exp
1>dllmain.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) double pp" (__imp_?pp@@3NA)
1>C:\Users\Reza\Desktop\Test_Projects\C#\Reza2\Debug\Reza2.dll : fatal error LNK1120: 1 unresolved externals
eugene.shikhov 11-Oct-12 7:40am    
Try _DLL instead of M_DLL.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900