从导出的函数访问时,UNexported全局变量将被初始化 [英] An UNexported global variable is becomes initialized when accessed from an exported function

查看:138
本文介绍了从导出的函数访问时,UNexported全局变量将被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将首先展示我的代码,以便更容易解释:



我将首先显示我的代码解释更容易:



Hi,
I will show my code first to make explaining easier:
Hi,
I will show my code first to make explaining easier:

//EXE
__declspec(dllimport) exportedDLLfunc(int i):
void exeFunc()
{
	for(int i=0; i<10;i++)
		exportedDLLfunc(i);
}

//someDLL
__declspec(dllexport) void exportedDLLfunc(int i);
someStruct_t gSome[10];
void exportedDLLfunc(int i)
{
	//When exportedDLLfunc is called from the exeFunc() gSome has zero values, NOT NULL
	//When exportedDLLfunc is called from the anotherdllfunc gSome is not NULL, and the program works perfectly (original version)
	//When exportedDLLfunc is called from both exeFunc() and anotherdllfunc(), gSome has the same pointer address
	dllFunc(gSome[i]);
}

void anotherdllfunc(int i)
{
	for(int i=0; i<10;i++)
		exportedDLLfunc(i);
}

/*
MSVC project properties:
Link->Input->Additional Dependencies: someDLL.lib
*/





注意:

*我没有编译错误或类似的东西。

* Windows,Microsoft Visual Studio 2010

* exportedDLLfunc()可以在里面看到someDLL.dll DependencyWalker



我的问题,为什么从exeFunc()调用gSome时,这些值是否包含在内?

注意:

*我没有编译错误或类似的东西。

* Windows,Microsoft Visual Studio 2010

* exportedDLLfunc()可以在someDLL.dll里面看到DependencyWalker



我的问题,为什么从exeFunc()调用时gSome里面的值都不包含?



Notes:
* I get no compiling errors or anything like that.
* Windows, Microsoft Visual Studio 2010
* exportedDLLfunc() can be seen inside someDLL.dll DependencyWalker

My question, why are the values aren''t contained inside gSome when it is called from exeFunc()?
Notes:
* I get no compiling errors or anything like that.
* Windows, Microsoft Visual Studio 2010
* exportedDLLfunc() can be seen inside someDLL.dll DependencyWalker

My question, why are the values aren''t contained inside gSome when it is called from exeFunc()?

推荐答案

这可能是初始化序列中的计时问题。当你调用exeFunc时,gSome数组可能还没有被初始化。



当调用anotherdllfunc时,保证gSome将在那时被初始化。这就是为什么你会看到不同的值,但是地址相同。



我也要指出调用

This could be a timing problem in the initialization sequence. When you call exeFunc, the gSome array might not be initialized yet.

When calling anotherdllfunc, it is guaranteed that gSome will have been initialized by that time. That is why you see different values, but the same address.

Let me also point out that calling
dllFunc(gSome[i]);



将按值传递参数,这意味着gSome [i]的整个结构被复制到堆栈中。可能通过const引用将是一种更好的方法。


will transfer the argument by value, which means that the entire structure of gSome[i] is copied to the stack. Probably going by const reference would be a better way.


这篇关于从导出的函数访问时,UNexported全局变量将被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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