公共变量问题 [英] Problem With Public Variable

查看:91
本文介绍了公共变量问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个平台(Metatrader)和另一个exe文件使用的一个dll.
我在标头代码中定义了一个公共变量(PP),并在
中设置了值 MT4_EXPFUNC double __stdcall Reza(double ppp)函数.

尝试从我的exe调用此dll时,它仅返回零(0)值,但是如果从其他平台调用(Metatrader)则返回正确的值.

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.

#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,

推荐答案

我认为您应该对price()函数使用相同的调用约定,即像这样声明它:
I think you should use the same calling convention for your price() function, i.e. declare it like:
MT4_EXPFUNC double __stdcall price()


这样做,您有两个不同的pp实例.其中一个实例在DLL中,另一个在EXE中.您应将dllexport用于DLL标头,并将dllimport用于EXE标头.像这样的东西:
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:
...
#ifdef M_DLL
__declspec(dllexport) double pp;
#else
__declspec(dllimport) double pp;
#endif
...



http://msdn.microsoft.com/en-us/library/9h658af8.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/9h658af8.aspx[^]


这篇关于公共变量问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆