Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I will show my code first to make explaining easier:
Hi,
I will show my code first to make explaining easier:

C
//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
*/


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()?
Posted
Updated 3-Jan-13 0:47am
v2

1 solution

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
C++
dllFunc(gSome[i]);

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.
 
Share this answer
 
Comments
coffeenet 3-Jan-13 9:14am    
Thanks for the reply!
--Regarding initialization
When I leave both anotherdllfunc() and exeFunc() running, and break the program inside anotherdllfunc(), I see gSome with values, but when I break inside exeFunc(), I don't see any values inside gSome. Does this still mean that gSome is uninitialized?

--gSome as an additional parameter
<pre>
dllFunc(gSome[i]);
</pre>
gSome is a global variable that is located inside the dll and not the exe. Do you mean that I should export gSome as well, and then add it as a parameter to the exported function, exportedDLLfunc(int i)?
nv3 3-Jan-13 9:44am    
What I mean is:

(a) make sure that the initialization of gSome is finished before calling exportedDllFunc. You didn't show the code that initialized gSome, so I have no way of checking where and when that is done.

(b) You perhaps defined dllFunc as:

void dllFunc (someStruct_t s);

And you should better define it as:

void dllFunc (const& someStruct_t s);

or (if dllFunc modifies the structure) as

void dllFunc (someStruct_t* pS);

Declaring the gSome array local to your DLL is perfectly ok. Just make sure that the index values you are operating with do not exceed the array.

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