Global Variable Question
-
Hi Coders I have a Visual C++ 2005 solution made up of two projects with lots of source fles (containing functions only) that produce two separate dlls. In one source file I calculate a value in a variable and I want that variable to have global scope across the two projects. Is that possible? The value in the variable is needed by all the other functions in the two projects to work properly. I have tried declaring the variable in header file and before the call to dllmain but neither approach works. I can't use a macro because the I have to recalculate the value each time the dlls are used. Any suggestions would be much appreciated. Regards Jeremy
-
Hi Coders I have a Visual C++ 2005 solution made up of two projects with lots of source fles (containing functions only) that produce two separate dlls. In one source file I calculate a value in a variable and I want that variable to have global scope across the two projects. Is that possible? The value in the variable is needed by all the other functions in the two projects to work properly. I have tried declaring the variable in header file and before the call to dllmain but neither approach works. I can't use a macro because the I have to recalculate the value each time the dlls are used. Any suggestions would be much appreciated. Regards Jeremy
Hi Jeremy, I didn't catch your question very well. If u r talking about two dlls that are called by one executable module, which means that they are running in the same process, I would like you try to declare a variable in the exe file, pass a pointer to it to both dlls and reference this variable to share info. If they are running in different processes, I would suggest you use some ipc apporach, for example Create/OpenFileMapping thing. Regards, Chris
-
Hi Jeremy, I didn't catch your question very well. If u r talking about two dlls that are called by one executable module, which means that they are running in the same process, I would like you try to declare a variable in the exe file, pass a pointer to it to both dlls and reference this variable to share info. If they are running in different processes, I would suggest you use some ipc apporach, for example Create/OpenFileMapping thing. Regards, Chris
-
Thanks Chris That's right I have two dlls that are called by one executable (Excel). I wanted to keep the variable out of Excel/VBA and just pass it between functions within both dlls. I'm very new to programming. Regards Jeremy
I can see this solution: In the first DLL, define a function that returns a pointer to your variable. In the other DLL, dynamically load the first DLL with a call to LoadLibrary, and call the aforementioned function. Rich
-
Thanks Chris That's right I have two dlls that are called by one executable (Excel). I wanted to keep the variable out of Excel/VBA and just pass it between functions within both dlls. I'm very new to programming. Regards Jeremy
Hi Jeremy, I am new to programming too. I think it is really cool to code something in MS Office. I mean that. Well, I think neither of my suggestions work in your case. Em... is your variable accessed very frequently? I mean, how about use registry table to story this variable? The following example really do very well in registry access stuff. http://www.codeproject.com/system/CRegistry.asp[^] Regards, Chris
-
I can see this solution: In the first DLL, define a function that returns a pointer to your variable. In the other DLL, dynamically load the first DLL with a call to LoadLibrary, and call the aforementioned function. Rich