最近用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],"a")){
parse.OnExtractUnb();
}
else if (argc==2 && !strcmp(argv[1], "b")) {
parse.OnExtractAll();
}
else
{
MessageBox(NULL,"输入参数错误!","信息提示",MB_OK);
}
return 0;
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 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 <afx.h>
#include <afxdb.h>
#include <iostream.h>
int main()
{
// try to initialize MFC
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))