Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Windows DLL export
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
*/
 
 
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 3-Jan-13 1:45am
Edited 3-Jan-13 1:47am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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]);
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.
  Permalink  
Comments
coffeenet at 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 at 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)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100