global variable usage for dll

T

thomas

in Exe.
----------------
Type* g_pGlobal;
func(){
g_pGlobal->doSth();
}
---------------
in another dll
---------------
func2(){
func()
}
-------------

It compiles ok, but "g_pGlobal" equals 0x000000 when running.

I guess the address space of the dll and exe are different, which
causes the problem.

Can anyone help?

If I set the global variable pointer to a local pointer in the class
holding func(), it works. But modification is supposed to be done to
the dll only.
 
A

Alain Ketterlin

thomas said:
in Exe.
----------------
Type* g_pGlobal;
func(){
g_pGlobal->doSth();
}

Not really surprising. What exactly did you expect? Where is g_pGlobal
supposed to be initalized?
I guess the address space of the dll and exe are different, which
causes the problem.

What's the problem exactly? (An address space is attached to a process,
not to a file or a library, so the above sentence doesn't really make
sense.)
If I set the global variable pointer to a local pointer in the class
holding func(), it works. But modification is supposed to be done to
the dll only.

You don't tell us enough. How do you initialize the pointer? What does
it point to? Where is it modified? What class are you talking about?
What's the link between g_pGlobal and that class? What does local mean?
Etc.

-- Alain.
 
T

thomas

Not really surprising. What exactly did you expect? Where is g_pGlobal
supposed to be initalized?

g_pGlobal is already initialized in EXE(not shown).
What's the problem exactly? (An address space is attached to a process,
not to a file or a library, so the above sentence doesn't really make
sense.)

Yeah.. I think it's an address space problem because g_pGlobal points
to a valid address in EXE, but it's 0x00 when used in dll.
You don't tell us enough. How do you initialize the pointer? What does
it point to? Where is it modified? What class are you talking about?
What's the link between g_pGlobal and that class? What does local mean?
Etc.

The pointer is initialized in EXE, should be earlier than the loading
of dll(will this be an issue?).
It's not modified once it's initialized.
The class has several member functions, which use the global variable
for fetching info.
By local, I mean using a local variable for fetching info instead of
the global variable. The local variable will be initialized when
instance is constructed for the class with the global variable.
 
T

thomas

In your code g_pGlobal is not initialized, so of course it is zero. It
seems like you have left out the initialization code.

It's initialized in EXE.
Please provide more relevant code demonstrating the problem, also stating
if the exe is compiled and linked against the dll (and how the dll is
able to call a function in the exe in this case) or if the dll is
compiled and linked agaist the exe (and how the dll gets loaded in this
case). Also, you might get better help in a platform-specific forum, DLL-
s are not covered by the C++2003 standard.

Yes, the exe is compiled and linked against the dll(the corresponding
library of couse).
It compiles because the dll needn't know the global variable in the
exe for compilation.
The dll include the header file declaring func(), and it works well if
func() only refers to some local variables.
But it goes wrong when a global variable is referenced.
 
R

RaZiel

in Exe.
----------------
Type* g_pGlobal;
func(){
g_pGlobal->doSth();
}
---------------
in another dll
---------------
func2(){
func()
}
-------------

It compiles ok, but "g_pGlobal" equals 0x000000 when running.

I guess the address space of the dll and exe are different, which
causes the problem.

Can anyone help?

If I set the global variable pointer to a local pointer in the class
holding func(), it works. But modification is supposed to be done to
the dll only.

Use a callback function (function pointer).

- RaZ
 
T

thomas

This should produce a linker error when compiling the dll. Are you sure
you don't have another copy of func() in the dll code?

Or maybe func() is inline in the header file, together with the
definition of g_pGlobal? This would indeed create two different g_pGlobal
variables.

In short, present a compilable and runnable example demonstrating the
problem. Most probably you would solve it by yourself while doing this.

hth
Paavo

Well, it seems a little confusing and difficult to state clearly.
Luckily I found a link talking about an issue exactly like mine.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_26763777.html

It requires registration to view solutions and I am trying to follow
the steps.
Anyway, it's quoted here if you are interested.

edit: woops!! It requires my credit card number to start 30-day free
trial when registration..

-----following quoted from that page-------

Global variable in .exe and .dll in windows
Asked by Quantster in Microsoft Operating Systems, Microsoft
Applications, C++ Programming Language
Tags: VS2008 .dll .exe
Dear all,

I have run into an issue which I have no idea how to reslove.
I have 3 projects in a VS2008 solution
1) .exe
2) .dll project which is used by the exe
3) .lib project which is used by both .dll and .exe

I have a global pointer variable that is defined in the .lib file.
There is a class in the .lib project that the .exe project
instantiates and that intializes the global pointer. I call this class
from the .exe project. (I hope you are still with me :)).

I have an extern definition in both the .dll and the .exe files for
the global pointer. The files in the .exe project can acccess the
gobal pointer without any issues but for the .dll file the global
pointer reamins null.

I hope the porblem is clear. I am developing on VS2008 but the
application needs to run on both windows and linux. The application is
in C++

Any help will be greatly appreciated.

Thanks
quantster
 
T

thomas

Well, it seems a little confusing and difficult to state clearly.
Luckily I found a link talking about an issue exactly like mine.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Q_2676...

It requires registration to view solutions and I am trying to follow
the steps.
Anyway, it's quoted here if you are interested.

edit: woops!! It requires my credit card number to start 30-day free
trial when registration..

-----following quoted from that page-------

Global variable in .exe and .dll in windows
Asked by Quantster in Microsoft Operating Systems, Microsoft
Applications, C++ Programming Language
Tags: VS2008 .dll .exe
Dear all,

 I have run into an issue which I have no idea how to reslove.
I have 3 projects in a VS2008 solution
1) .exe
2) .dll project which is used by the exe
3) .lib project which is used by both .dll and .exe

I have a global pointer variable that is defined in the .lib file.
There is a class in the .lib project that the .exe project
instantiates and that intializes the global pointer. I call this class
from the .exe project. (I hope you are still with me :)).

I have an extern definition in both the .dll and the .exe files for
the global pointer. The files in the .exe project can acccess the
gobal pointer without any issues but for the .dll file the global
pointer reamins null.

I hope the porblem is clear. I am developing on VS2008 but the
application needs to run on both windows and linux. The application is
in C++

Any help will be greatly appreciated.

Thanks
quantster

Hmm.. I found another link. It's exactly my problem.
Well, it seems the definition of the global variable in .lib causes
the problem.
Omg, I should have noticed that...

http://social.msdn.microsoft.com/Fo.../thread/684413dc-d6d2-446f-85c5-76142ab9af2d/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top