dll , main and singleton

Started by
8 comments, last by Antheus 12 years, 9 months ago
Hello.
I have a main program (an .exe) that contains a singleton.
I must create a dll project that accesses to the main instance of that singleton.
If I includes the .h file of the singleton in the .dll project, when I log to the singleton i create a new instance of the singleton , and this is wrong , i would access to the same address of the instance of the main program.
I saw GetProcAddress(may be a solution?) but i cant understand how it works.

thanks.
Advertisement
Hi!

I don't quite understand what you are trying to do. Are you trying to access a Log-Class which is a singleton from your DLL? If So GetProcAddress won't be of any help. This function is used to get a function-pointer of a function that is exported by another module. For example if you have in your DLL "myDLL"


extern "C"
__declspec(dllexport) bool isFive(int myVal)
{
if(myVal == 5)
return true;
else
return false;
}


than you can import that DLL in your main program and in the main .exe use


HINSTANCE hDLL; // Handle to DLL LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
int valToTest = 4;

hDLL = LoadLibrary("MyDLL");

if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "isFive");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return SOME_ERROR_CODE;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(valToTest);
}
}


which basically is half copied from here
http://msdn.microsof...v=vs.80%29.aspx

Hope that helped!
Xaser
Not that I'm familiar with Windows programming, but it seems me that GetModuleHandle can be used (e.g. instead of LoadLibrary in the example above posted by Xaser) to get access to the .exe itself, so that GetProcAddress can perhaps also be invoked from inside a DLL to get addresses from the main module.

However, normally I would say that there is a software design flaw. 1st a DLL grabs process addresses from main module, and 2nd there is a singleton involved. Giugio, are you sure that you want such a construct?
i'm using vs 2003 , what is
LPFNDLLFUNC1???

i must creaty my personal function pointer?
so, is trueheagarr you are right , i wish get the same instance of the main singleton!!!
Why can't the DLL have its own logger?
You definitely should update to the current Version of VS 2010. Its free in the express version and contains everything you need.
Download Here I would highly recommend the All-In-One iso.

Back to the Problem. LPFNDLLFUNC1 is just a function pointer.

About haegarrs Idea: I don't think that GetModuleHandle will be of much use, because it will just give you the pointer to the Main Module, but you can't do anything really with it when the main doesn't export the Singleton via dllexport, but I'm not sure how dllexport works if it is used in an executable not in a DLL.

Guigio could you try and explain what exactly you want to achieve please.

Xaser
I'm try to explain better what's i wish do:

I have a large c++ project and i would create a singleton factory for instantiate classes from in the dll from the main project.
Because I I would not include the huge main project.
Then i have create a system of classes registration with an his FactoryCreate() function that is in the register classes.
The register classes add it to the factory singleton in a map that have a string key and a function pointer value.
Then the singleton factory is capable to instantiate all registered classes from everywhere.
My problem is that if i do the access to the sigleton from the dll i create a new instance of the singleton and instead i would get the same istance of the main project singleton instance.

thanks.

I'm not sure how dllexport works if it is used in an executable not in a DLL.
[/quote]
Pretty much the same way, I believe. DLLs and executables are both PE files, and they both support this mode of operation.

@OP:
I can't really parse that last post. I think the DLL has a factory function in it? I'm not sure what registration you're allowing - does the exextuable register the classes, or other DLLs, or is it internal?

Instead of trying to access the singleton directly, consider passing the pointer into the DLL, or simply allowing the DLL to have its own logger.
I tried to do:
code:

FNPTR NFP;
HMODULE mod = GetModuleHandle ("C: \ \ testdata \ \ frg \ \ Debug \ \ crtx.exe");

NFP = (FNPTR) GetProcAddress (mod, "FactoryDS");

Factory1 * F;
NFP (F);
CBeamDSChess pChess * = F-> CreateDS ();
int n = pChess-> m_nChessBoard;

////////////////////////////////////////////////// ////////////////////////////////////////
////////////////////////////////////////////////// ////////////////////////////////////////
void _declspec (dllexport) FactoryDS (Factory1 & * pF)
{
* Pf = Factory1 Factory1: get_instance ();
pF = pf;
}


by exporting the instance of the singleton function FactoryDS and going to fish for instance with GetProcAddress.
The problem now is the inclusion of the files, I would like to include all the headers I need in the main program and not in the form and where I get the address of the instance of the singleton.
is it possible?
otherwise the problem is that many functions pChess-> request headers that are already included in the main project, but not in the project that creates the dll, and I would not make too much this last, otherwise it makes no sense to split the two projects.
Thank you.
Singletons don't work with DLLs, limitation of the C++ model, also known as the static initialization fiasco.

You need a regular global, plain C type, which is manually initialized at appropriate points.

It's possible to use various hacks, but none of them are reliable solution.


Even the Apache logging library could not reliably solve this problem. It's just one of those undefined things that are part of C++ development.

This topic is closed to new replies.

Advertisement