Win32 DLLs Constant variables?

Started by
5 comments, last by Erzengeldeslichtes 19 years, 3 months ago
Is there any way to basically do this in win32:

//SomeDLL
const int NeededInt=3;
//SomeApplication
#include <windows.h>
void main()
{
    HANDLE DLL = LoadLibrary("SomeDLL");
    int ResultantInt = GetInt(DLL, "NeededInt");
}

Basically what I need is a constant integer in a DLL that can NOT be changed at any time. Currently I have something like this:

//SomeDLL
extern "C" __declspec(dllexport) int GetNeededInt();
int GetNeededInt { return 3; }
//SomeApplication
#include <windows.h>
typedef int (*IntFncVoid)();
void main()
{
    LoadLibrary("SomeDLL");
    IntFncVoid TheFunc = GetProcAddress(DLL, "GetNeededInt");
    if(!TheFunc) return;
    int ResultantInt = TheFunc();
}

The problem here is I can make GetNeededInt() any function I want. I could make it call other functions. I could make it variable. But it's not supposed to be variable, it's supposed to stay the same, and things are likely to go awry if it is variable. If there isn't a way, well then I guess it's the programmer's job to make sure DLL's work right (I'd just like to idiot proof the code when I can)
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
Advertisement
I kind of get the feeling you're barking up the wrong tree. This kind of thing would usually just be a constant in the header file that goes along with the dll file.

I don't really see anything wrong with your current solution. If you're the write of the DLL, then you can ensure that the value in the function is not modified.

Also, do you need this constant to be a specific value e.g. 5, or can it just be any value so long as it never changes.

btw, you can export variables as well as function, not that it would help, I think.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Well the thing is that the DLLs are supposed to be dynamically loaded and changable without changing and recompiling the base program. They are also supposed to be modifiable by anyone who understands what the program expects. The constant variables (which there are actually a few) are things about the DLL that are supposed to always be the same within the instance of the DLL, but when the program is closed could be changed to change the way the program acts.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
The __declspec(dllexport) keyword allows for exporting data too. dllexport, dllimport.

Here's an example from the above link:

__declspec( dllexport ) int i = 10;


In actual use, a more specific variable name is recommended.

Here's more info on exporting data: How To Export Data from a DLL or an Application.

On the other end of the operation, see Importing Data Using __declspec(dllimport), Importing into an Application Using __declspec(dllimport).


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
It is my understanding that __declspec(dllimport) will allow me to directly access the data as if it were a part of the application's data right? The thing is I have this:
Application:
-enumerates DLLs in Plugin directory
-for each plugin found
--create plug in object
--load plug in
--assign each expected function pointer to member function pointers, throw an error if can't find one.
--run through functions the application expects to be constant, store return value.
-uses plug ins.

Now I only have 1 header file for the plug ins. Wouldn't dllimport just give me the one of each exported variable?

Of course, in this method the constant data is held application side and so remains constant as far as the application is concerned. There really isn't a problem with that, but a plugin that expects it to change will be disappointed, and might throw a tantrum (Crash).

I don't know, it just doesn't seem right that I have to use a function to get constant global data.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?
It would probably be better to use GetProcAddress to obtain the address of the data and then dereference that to obtain the data value. Something like this:

int *ptrImportInt = (int*)GetProcAddress(DLL, "NeededInt");


ymmv

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by LessBread
It would probably be better to use GetProcAddress to obtain the address of the data and then dereference that to obtain the data value. Something like this:

int *ptrImportInt = (int*)GetProcAddress(DLL, "NeededInt");


ymmv

Oh? I can do that with variables as well as functions? Nice. Thanks.
----Erzengel des Lichtes光の大天使Archangel of LightEverything has a use. You must know that use, and when to properly use the effects.♀≈♂?

This topic is closed to new replies.

Advertisement