sharing variable between a DLL and an application

Hello,
I have a c++ coded DLL, which exports some functions in a flat C header file, so that the DLL can be used with several languages.

My problem is now to share some class static member (a singleton) between the application and the DLL. I can't export the member directly since my interface is not using classes.

I then tried change the implementation of this static member, and to turn it as a static variable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Manager& Manager::GetInstance(int managerNumber)
  { 
  static std::vector< std::tr1::shared_ptr<Manager> > Instances;
 

  if(managerNumber>= ((int)Instances.size()))
  {
    for(int i = ((int)Instances.size()); i < managerNumber+1 ; i++)
    {
      //the shared_ptr call CustomDeleter() automatically once they go out of scope
      std::tr1::shared_ptr<Manager> pManager( new Manager(),CustomDeleter<Manager>() );
      pManager->_managerNumber=managerNumber;
      Instances.push_back(pManager);
    }
  }
  return *(Instances[managerNumber]); 
  }


In my application I created the same function, so the static vector is created with the same name.

Unfortunately, while running the debugger, it seems that 2 instances of this static vector are created, one "in" the DLL, and one "in" the application.
Is there any mechanism which would allow me to do that properly?
Thanks for any help.
Last edited on
Hum, after searching on the internet I got that a static variable declared in a function, even though it has the same name than another static variable declared in another function, will be different at run-time.

But then is there a way to do that properly? I could create a global scope variable in my DLL and export it so that the application can use it, but I find this solution very ugly :/
I can't export the member directly since my interface is not using classes.
Can you please elaborate further. It's not clear on what you're trying to do.

Clearly there's a solution to this problem, but we need to understand what your constrains and requirements are.
What I here call "interface" is the header file in my DLL where I am doing the exports.
This file is written in C++ which is compatible with C ( actually the whole header file is in a extern"C" tag). Thus the exported DLL is not compiler specific and can use C naming convention.

That's also why i don't want to export a class with this DLL, since there is no class in C.

I read that static local variables in class member functions will only have 1 instance of themselves, even throughout different translation units. And that's what I would like to use, to share this vector between my DLL and my application.


It's functioning good when I declare a static &Manager, but when I'm using this static vector it's not working any more.

Maybe I should declare every shared_pointer I'm adding to the vector as static?
Last edited on
Actually, I want to use what is described here:
http://msdn.microsoft.com/en-us/library/w8c3skw3.aspx

"Static local data and strings in inline functions maintain the same identities between the DLL and client as they would in a single program (that is, an executable file without a DLL interface)."
closed account (S6k9GNh0)
Your question isn't very clear... but I can give some hints and tips:

static class variables and globals are absolutely evil. DO NOT export them from a DLL. You're just asking for issues with threading and various other things.

Instead, you can a) export functions that fetch the global variables and rather put them in either an anonymous namespace or give them the static attribute, don't directly expose the global variables, or b) pass the user a struct/class/pointer to some data that contains the variables needed for your library to work. In this case, void pointers or empty class pointers are not bad.
Last edited on
I got it, so you want a to make a library in C++ with a C interface. That's straight forward.

Can you show what the C functions look like? It'll help me provide more specific help.
Exactly, that's what I want to do,
my functions are exported with the following method,I just copied 2 of them since the file is quite big:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//C interface header file:

#	ifdef	__BUILD_DLL__
#		define	DLLEXPORT	__declspec(dllexport) _cdecl
#	else
#		define	DLLEXPORT	__declspec(dllimport) _cdecl
#	endif

// Use flat C interface.
#ifdef	__cplusplus
extern "C" {
#endif

...
...
int DLLEXPORT ManagerInit();
...
...
ErrorIndex DLLEXPORT Start(int deviceId);
...
...
Last edited on
I think I understood the problem.
It was actually linked to a misunderstanding of the code that I'm modifying.

There is a class called Manager which is declared in the client application header file, and there is a class called Manager which is declared in the DLL itself.

But a Manager instance in the application can't be the same instance than a Manager instance in the DLL, right?

Then when I instantiate a Manager in the application I have to create an instance in the DLL, which will be a copy of the one in the application.
I will then modify the method Manager() in the application, to make it call Manager() in the DLL.

Thank you for the help :-)

Last edited on
Topic archived. No new replies allowed.