Sign in to follow this  
giugio

dll , main and singleton

Recommended Posts

giugio    246
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.

Share this post


Link to post
Share on other sites
XaserIII    102
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"

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

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

[code]
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);
}
}
[/code]

which basically is half copied from here
[url="http://msdn.microsoft.com/de-de/library/64tkc9y5%28v=vs.80%29.aspx"]http://msdn.microsof...v=vs.80%29.aspx[/url]

Hope that helped!
Xaser

Share this post


Link to post
Share on other sites
haegarr    7372
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?

Share this post


Link to post
Share on other sites
giugio    246
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!!!

Share this post


Link to post
Share on other sites
XaserIII    102
You definitely should update to the current Version of VS 2010. Its free in the express version and contains everything you need.
[url="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express"]Download Here[/url] 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

Share this post


Link to post
Share on other sites
giugio    246
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.

Share this post


Link to post
Share on other sites
rip-off    10976
[quote]
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.

Share this post


Link to post
Share on other sites
giugio    246
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.

Share this post


Link to post
Share on other sites
Antheus    2409
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this