Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,
I'm frequently using dll project in vc++ for breaking big project into small modules.
As we known that a dll project has only one working dll file is a project file.
that is if my project name is CheckVarPropDll then
CheckVarPropDll.cpp and CheckVarPropDll.h are created.
this file contain class that inherited from CWinApp

if i have function that member of CWinApp
like follow
void CWinApp::CheckVarProp(CString str)
{
 .
 .
 .
}


and that function call is in user define class
like

EnterValue.cpp

void EnterValue::GetValue()
{
CWinApp myapp;
myapp.CheckVarProp("1234");
}


code compile and link so well but while using this dll in mfc application gives treading error.

Becouse of creating object of CWinApp

there is default object of CwinApp present in .cpp file of project just below of
InitInstance()
like
CWinApp theApp;

so my question start here can there is any method to create duplicate object of CWinApp. so that i can call any function of cwinApp from any point of time and any point of code.
if Yes then how it?
if No then Why it?
Thanks
Regard SANTOSH
Posted
Updated 10-Mar-11 20:41pm
v4

Do not instanciate CWinApp object! There should be only one instance per project (the one declared as theApp). If you want to access that object, then you there have two options:

1- add
extern CWinApp theApp;
at the end of CheckVarPropDll.h so it can be accessed in all your .cpp files (of course, make sure they have #include "CheckVarPropDll.h").

2- use the global function AfxGetApp. This function returns CWinApp pointer (a pointer to theApp). You nee then to cast that pointer into your CWinApp derived class.

Personnaly, I prefer solution 1:

C++
void EnterValue::GetValue()
{
    theApp.CheckVarProp("1234");
}

Or
C++
void EnterValue::GetValue()
{
    ((CYourWinApp*)AfxGetApp())->CheckVarProp("1234");
}
 
Share this answer
 
v2
Comments
[no name] 11-Mar-11 3:38am    
Thanks for reply after,
can you tell me why not possible to create object CWinApp.
Olivier Levrey 11-Mar-11 3:40am    
Basically, this class contains the entry point of your application (or dll) : kind of main function. If you instanciate the class more than once, you will have more than 1 main.
Olivier Levrey 11-Mar-11 3:42am    
Have a look to this link for more details about CWinApp: http://msdn.microsoft.com/en-us/library/akdx0603(v=vs.80).aspx
[no name] 11-Mar-11 3:44am    
But its a class it has lot of functions.
we can say that InitInstance() can call one time but why all class functions.
and functions that not called multiple time we can declare it as private so water become water and milk become milk.
[no name] 11-Mar-11 3:46am    
And how my application can compile and link problem came when dll used in mfc application
This remark before the definition of the CWinApp's (actually your CWinApp inherited class) instance
C++
// The one and only CMFCApp object

is an hint that CWinApp is not meant to be duplicated. Hence your design (if I got you) is probably bad or, at least, it clashes with MFC.

I suggest you to revise your design. Possibly the following questions may help you:
  • Cannot you move your functionality out of CWinApp?
  • Do your methods really need to access CWinApp's instance members (if you write static methods, then you have no need to duplicate CWinApp)?
 
Share this answer
 
Comments
[no name] 11-Mar-11 4:01am    
Hey that make some sense,
but why my dll application get complile and link.
problem come while it used.
Olivier Levrey 11-Mar-11 4:28am    
"Can compile" doesn't mean "can't crash".
[no name] 11-Mar-11 4:34am    
How then it link.
what do you mean by crash pls tell me in details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900