本文介绍了在mfc vc ++中创建CWinApp的重复对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我经常在vc ++中使用dll项目将大项目分解为小模块.
众所周知,一个dll项目只有一个工作的dll文件是一个项目文件.
也就是说,如果我的项目名称是CheckVarPropDll,则
将创建CheckVarPropDll.cpp和CheckVarPropDll.h.
该文件包含从CWinApp继承的类

如果我具有CWinApp成员的功能
就像关注

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)
{
 .
 .
 .
}



并且该函数调用在用户定义的类中
喜欢

EnterValue.cpp



and that function call is in user define class
like

EnterValue.cpp

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



代码编译和链接很好,但是在mfc应用程序中使用此dll时会出现踩踏错误.

因为创建了CWinApp对象



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
下面的项目的.cpp文件中存在CwinApp的默认对象

like

CWinApp theApp;


所以我的问题从这里开始,是否有任何方法可以创建CWinApp的重复对象.这样我就可以从任何时间和任何代码点调用cwinApp的任何功能.
如果是,那怎么办?
如果没有,为什么呢?
谢谢
关于SANTOSH


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

推荐答案

extern CWinApp theApp;

,以便可以在所有.cpp文件中访问它(当然,请确保它们具有#include "CheckVarPropDll.h").

2-使用全局功能AfxGetApp.此函数返回CWinApp指针(指向theApp的指针).然后,您需要将该指针转换为您的CWinApp派生类.

从个性上讲,我更喜欢解决方案1:

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:

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




Or

void EnterValue::GetValue()
{
    ((CYourWinApp*)AfxGetApp())->CheckVarProp("1234");
}


// The one and only CMFCApp object


暗示CWinApp 不可重复.因此,您的设计(如果有的话)可能很糟糕,或者至少与MFC冲突.

我建议您修改设计.以下问题可能会为您提供帮助:

  • 您不能将功能移出CWinApp吗?
  • 您的方法是否真的需要访问CWinApp实例成员(如果您编写的话)静态方法,那么您无需重复CWinApp)?

  • 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)?

    • 这篇关于在mfc vc ++中创建CWinApp的重复对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 23:50