
最近用MFC写了一个控制台程序,然后在C#环境下用个Process来调用,出现如下错误:
afxwin1.inl assert at line:22
现象:打开afxwin1.inl 发现assert(afxCurrentInstanceHandle != NULL)出错
说明MFC没有做初始化,afxCurrentInstanceHandle没有初始化。解决办法如下:见红色部分
#include "parse.h"
#include "iostream.h"
#include "afxwin.h" //afxWinInit对应的头文件
int main(int argc, char* argv[])
{
AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); //控制台程序中初始化MFC
CParse parse;
if (argc==2 && !strcmp(argv[1],"1")){
parse.OnExtractUnb();
}
else if (argc==2 && !strcmp(argv[1], "1")) {
parse.OnExtractAll();
}
else
{
MessageBox(NULL,"输入参数错误!信息提示",MB_OK);
}
return 0;
=====================================================================
今天把exe程序改成了dll形式,但问题有出现了,就是不知道把 AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); //控制台程序中初始化MFC放在哪里,因为建立的Dll不带有DLLmain()函数。
郁闷,各位熟悉的高手,请赐教!
=====================================================================
触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因 收藏
这种错误的原因是由于要使用MFC库中的某些资源,但是MFC并没有被初始化.
这种情况多发生在atl和其它非MFC工程,后追加MFC的头文件获得了部分支持
现象:1) 编译可以通过 2)有些MFC类可以使用 3) 使用CRectTracker, CMenu.LoadMenu....时出现触发ASSERT(afxCurrentResourceHandle != NULL)错误
解决办法:
在模块初始化后,要使用的MFC类前,添加如下代码:
//!!!!MFC初始化
if(!AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0))
{
//_Module.LogEvent(_T("MFC初始化错误!"), EVENTLOG_ERROR_TYPE );
return 1;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/genaman/archive/2009/03/09/3970982.aspx
在dll中使用MFC时由于afxCurrentResourceHandle=NULL会引起对话框在创建时调用AfxGetResourceHandle()方法断言失败,解决该问题需要在程序使用MFC类之前调用如下方法即可
AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0);
http://qufulin.blog.163.com/blog/static/8405212009112133626835/
=====================================================================
AfxWinInit
BOOL AFXAPI AfxWinInit( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
Parameters
hInstance
The handle of the currently running module.
hPrevInstance
A handle to a previous instance of the application. For a Win32-based application, this parameter is always NULL.
lpCmdLine
Points to a null-terminated string specifying the command line for the application.
nCmdShow
Specifies how the main window of a GUI application would be shown.
Remarks
This function is called by the MFC-supplied WinMain function, as part of the CWinApp initialization of a GUI-based application, to initialize MFC. For a console application, which does not use the MFC-supplied WinMain function, you must call AfxWinInit directly to initialize MFC.
If you call AfxWinInit yourself, you should declare an instance of a CWinApp class. For a console application, you might choose not to derive your own class from CWinApp and instead use an instance of CWinApp directly. This technique is appropriate if you decide to leave all functionality for your application in your implementation of main.
The < id="alink_1" classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11" type="application/x-oleobject">
TEAR sample shows how to make a console application using MFC.
Example
// this file must be compiled with the /GX and /MT options: // cl /GX /MT thisfile.cpp#include ASSERT(afxCurrentResourceHandle != NULL);出现错误的完美解决办法,其实vc中已经提示说明。 初始化MFC: 上篇文章适合exe 这个方法适合dll // Note! // // If this DLL is dynamically linked against the MFC // DLLs, any functions exported from this DLL which // call into MFC must have the AFX_MANAGE_STATE macro // added at the very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } // // It is very important that this macro appear in each // function, prior to any calls into MFC. This means that // it must appear as the first statement within the // function, even before any object variable declarations // as their constructors may generate calls into the MFC // DLL. // // Please see MFC Technical Notes 33 and 58 for additional // details. AFX_MANAGE_STATE AFX_MANAGE_STATE( AFX_MODULE_STATE* pModuleState ) Parameters pModuleState A pointer to an AFX_MODULE_STATE structure. Remarks Call this macro to protect an exported function in a DLL. When this macro is invoked, pModuleState is the effective module state for the remainder of the immediate containing scope. Upon leaving the scope, the previous effective module state will be automatically restored. The AFX_MODULE_STATE structure contains global data for the module, that is, the portion of the module state that is pushed or popped. By default, MFC uses the resource handle of the main application to load the resource template. If you have an exported function in a DLL, such as one that launches a dialog box in the DLL, this template is actually stored in the DLL module. You need to switch the module state for the correct handle to be used. You can do this by adding the following code to the beginning of the function: AFX_MANAGE_STATE(AfxGetStaticModuleState( )); This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope.
