Click here to Skip to main content
15,895,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am trying to implement a seemingly simply thing here. But, I just can't seem to get it to work.
What I need is: Access a variable from an EXE, where the variable is located in a DLL.

C++
//DLL:
#define __ZG_WIN32__
#ifdef __ZG_WIN32__
#define __ZG_DLL_EXPORT__ __declspec(dllexport)
#endif

#ifdef __ZG_LINUX__ //LINUX
#define __ZG_DLL_EXPORT__ extern
#endif

__ZG_DLL_EXPORT__ int ZGKillerStarter = -1;


//EXE:
#define __ZG_WIN32__
#ifdef __ZG_WIN32__
#define __ZG_DLL_IMPORT__ __declspec(dllimport)
#endif

#ifdef __ZG_LINUX__ //LINUX
#define __ZG_DLL_IMPORT__
#endif

__ZG_DLL_IMPORT__ int ZGKillerStarter;


Then, I linked the dll in the compiler as an additional dependency. I even checked the variable's name with Dependency Walker.

When I run the program, ZGKillerStarter's value is its initial value of -1, and never changes even when I change it on the DLL side! ><<br mode="hold" />
Furthermore, I even tried to "Get" the variable's value using an EXPORT/IMPORT function, but it kept on returning the initial value every time. Moreover, I did the same but using a function that returns the variable's address, still initial value ><;

Q1: What am I missing here?
Q2: What is the equivalent of this code in Linux?
Q3: How do I do the inverted action of the above!? (NOT at the same time, just exporting variables from EXE, importing to DLL)

NOTE: There are certain restrictions that I need to follow, which prohibits me from using function pointers.

reference: http://msdn.microsoft.com/en-us/library/8fskxacy%28v=vs.80%29.aspx[^]


C++
//EXE side
__ZG_DLL_IMPORT__ int ZGKillerStarter;//first try
__ZG_DLL_IMPORT__ int ZG_getZG( /*void */);//second try
__ZG_DLL_IMPORT__ int *ZG_getZG( /*void */);//third try

int XXXXX = 0; int IIII = 9;

//DLL side
__ZG_DLL_EXPORT__ int ZG_getZG( )//second try
{
	return ZGKillerStarter;
}


__ZG_DLL_EXPORT__ int *ZG_getZG( )//third try
{
	return &ZGKillerStarter;
}
void funcX()
{
if( ZGKillerStarter < 10000 ) ZGKillerStarter += 8;
}


//EXE side
__ZG_DLL_IMPORT__ int ZGKillerStarter;//first try
__ZG_DLL_IMPORT__ int ZG_getZG( /*void */);//second try
__ZG_DLL_IMPORT__ int *ZG_getZG( /*void */);//third try

int XXXXX = 0; int IIII = 9;

//DLL side
__ZG_DLL_EXPORT__ int ZG_getZG( )//second try
{
	return ZGKillerStarter;
}


__ZG_DLL_EXPORT__ int *ZG_getZG( )//third try
{
	return &ZGKillerStarter;
}
void funcX()
{
if( ZGKillerStarter < 10000 ) ZGKillerStarter += 8;
}
Posted
Updated 15-Dec-11 3:00am
v4

How do you mean "change it on the DLL side"? What code do you execute to get the variable and then to change it?
 
Share this answer
 
Comments
coffeenet 15-Dec-11 5:14am    
Sample code moved to question.
Richard MacCutchan 15-Dec-11 5:29am    
And what results do you see when you step through this code with your debugger?
coffeenet 15-Dec-11 8:38am    
Compilation went without any errors or warnings.
But, when I run the program:
<pre>
XXXXX = ZG_getZG( );// or even XXXXX = ZGKillerStarter;// XXXXX's value is always -1
</pre>
Richard MacCutchan 15-Dec-11 9:01am    
Try with your debugger as I suggested. Step through the code and see what results you get. I am going to run a similar test to see what I can make of it.
Richard MacCutchan 15-Dec-11 9:16am    
I have just tested this and it works correctly. I can only conclude that something is happening in your code that you are not showing. The debugger will help you to get to the solution.
Let's try looking at the sequence of events:

  1. Clean both projects (ensure output directories are empty).
  2. Build the DLL using the code as shown above. This should produce two files name.lib and name.dll (name is the name you give the project).
  3. Add "name.lib" to the Additional Dependencies section of the Input Linker Properties in the EXE project.
  4. Add the directory path of the name.lib file to the Additional Library Directories section of the General Linker Properties in the EXE project.
  5. Build the EXE project.
  6. Copy the name.dll file into the target firectory of the EXE project, i.e the directory that contains the target .exe file
  7. Run the program and check results.
 
Share this answer
 
Comments
coffeenet 17-Dec-11 19:07pm    
I did what you kindly instructed. But, unfortunately, the program crashes where the imported variable. I really can't understand what I am missing here.
I changed my mind, I am gona use a callback function. (As you can see in my other question.)
Richard MacCutchan 18-Dec-11 3:22am    
If the program crashed then you have a bug in your code. Rather than ignore it you should spend time with your debugger to find out what you have done wrong, in order to avoid a similar problem in the future. Given that I was able to make this work by copying the code you have shown in your original question you must be doing something else that you are not showing us.

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