Debug Assert error afxwin1.inl line:22

翻译参考 http://wenku.baidu.com/view/146a503987c24028915fc3f6.html

上述错误出现的原因有两个情况,一是对于控制台程序,使用MFC却没有初始化;二是使用MFC编写dll,对导出函数没有响应的宏声明。

一是对于控制台程序,使用MFC却没有初始化;

==================================================================================

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

{ cerr << "MFC failed to initialize!" << endl; return 1; }

// try to connect to an ODBC database that doesn't exist

// (this wouldn't work at all without initializing MFC)

CDatabase db;

try { db.Open("This Databsae Doesn't Exist");

// we shouldn't realistically get here

cout << "Successful!" << endl; cout << "Closing ... ";

db.Close();

cout << "Closed!" << endl; }

catch (CDBException* pEx) {

// we got an exception! print an error message

// (this wouldn't work without initializing MFC)

char sz[1024]; cout << "Error: ";

if (pEx->GetErrorMessage(sz, 1024)) cout << sz; else cout << "No error message was available";

cout << endl; pEx->Delete();

return 1;

}

return 0;

}

二是使用MFC编写dll,对导出函数没有响应的宏声明。

========================================================================================

ASSERT(afxCurrentResourceHandle != NULL);出现错误的完美解决办法,其实vc中已经提示说明。

// 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_STATEpModuleState )

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 until the end of the current scope.

这种错误的原因是由于要使用MFC库中的某些资源,但是MFC并没有被初始化.

这种情况多发生在atl和其它非MFC工程,后追加MFC的头文件获得了部分支持

现象:1) 编译可以通过 2)有些MFC类可以使用  3) 使用CRectTracker, CMenu.LoadMenu....时出现触发ASSERT(afxCurrentResourceHandle != NULL)错误

解决办法:

在模块初始化后,要使用的MFC类前,添加如下代码:

//MFC初始化
   if(!AfxWinInit(::GetModuleHandle(NULL),NULL,::GetCommandLine(),0))
   {
     return 1;
   }

转自:http://blog.csdn.net/genaman/article/details/3970982

[转载]触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因的更多相关文章

  1. php – Laravel 5查询关系导致“调用成员函数addEagerConstraints()on null”错误( 转载)

    php – Laravel 5查询关系导致“调用成员函数addEagerConstraints()on null”错误   我一直在尝试创建一个简单的用户管理系统,但在查询关系时不断遇到障碍.例如,我 ...

  2. 20180911 关于页面加载顺序引发的JS的undefined/null错误

    引用: 百度知道-HTML+JavaScript执行顺序问题 这是我在学习JS滚动播放图片案例意外遇到的一个问题,代码完成后console弹出错误警告: Uncaught TypeError: Can ...

  3. VUE中集成echarts时 getAttribute of null错误

    错误 错误场景一: 错误提示: 在运行Vue项目时出现了上述错误,出现该错误的原因是Echarts的图形容器还未生成就对其进行了初始化所造成的,代码如下: // 基于准备好的dom,初始化echart ...

  4. Atitit. null错误的设计 使用Optional来处理null

    Atitit. null错误的设计 使用Optional来处理null 然后,我们再看看null还会引入什么问题. 看看下面这个代码: String address = person.getCount ...

  5. android 高德地图API 之 java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLibrary returned null错误

    错误场景: 运行android app时,在运行到调用高德地图API时,出现 “java.lang.UnsatisfiedLinkError: Couldn't load amapv3: findLi ...

  6. 深度解析MySQL启动时报“The server quit without updating PID file”错误的原因

    很多童鞋在启动mysql的时候,碰到过这个错误, 首先,澄清一点,出现这个错误的前提是:通过服务脚本来启动mysql.通过mysqld_safe或mysqld启动mysql实例并不会报这个错误. 那么 ...

  7. tomcat项目迁移,无法访问,报“404”错误,原因分析

    首先,导出项目文件和MySQL数据库(项目文件直接tar&&cp:数据库直接mysqldump生成sql文件) 再,进行导入步骤,项目文件拷贝到webapps下,并赋予bin相关文件执 ...

  8. ERR_CONTENT_DECODING_FAILED错误的原因和解决办法

    1. ERR_CONTENT_DECODING_FAILED错误的原因 这种错误通常发生于Http请求中的头部信息标识内容是gzip编码的,但实际上不是. 2. ERR_CONTENT_DECODIN ...

  9. The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案

    异常描述: 数据访问用EF,在数据库中用getdate()设置的默认值,程序中没有赋值. 出现异常. 此错误在百度上在我写此文之前没有多少解决方案,谷歌之等到以下两个有用的页: http://stac ...

随机推荐

  1. rsync+inotify实现实时同步案例--转

    转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...

  2. 近实时运算的利器---presto在公司实践

    1.起因 公司hadoop集群里的datanonde和tasktracker节点负载主要集中于晚上到凌晨,平日工作时间负载不是很高.但在工作时间内,公司业务人员有实时查询需求,现在主要 借助于hive ...

  3. 【Filter 页面重定向循环】写一个过滤器造成的页面重定向循环的问题

    今天做一个过滤器,碰上页面重定向循环的情况: 浏览器的访问路径是:http://192.168.16.104:8080/biologyInfo/login/login/login/login/logi ...

  4. POJ 3080 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3080 题意:给定n个DNA串,求最长公共子串.如果最长公共子串的长度小于3时输出no significant commonalitie ...

  5. iOS10 UI教程视图和子视图的可见性

    iOS10 UI教程视图和子视图的可见性 iOS10 UI教程视图和子视图的可见性,一个父视图可以通过clipsToBounds属性,定义子视图在边界(边界就是父视图的框架也就是父视图可以显示的范围) ...

  6. CSS的clip-path

    在Web网页中主要是以矩形分布的.而平面媒体则倾向于更多不同的形状.造成这种差异的原因是因为缺少合适的工具去实现我们平面媒体中的内容.这也就造成了很多设计师的创意发挥,就算是有创意,前端实现也将付出巨 ...

  7. HDU5737 : Differencia

    注意到$b$不变,考虑用归并树来维护这个$b$序列,对于每个节点有序地维护$b$,同时在归并的时候预处理出每个元素在左右儿子里的排名. 在归并树上额外维护区间内$a\geq b$的个数以及赋值标记. ...

  8. c++ 服务端 客户端

    转载自:http://blog.csdn.net/orange_xxx/article/details/7276868 ,谢谢原作者. 作为个人学习的笔记使用. 服务端: // Server.cpp ...

  9. iOS学习06C语言结构体

    1.结构体的概述 在C语言中,结构体(struct)指的是一种数据结构,是C语言中构造类型的其中之一. 在实际应用中,我们通常需要由不同类型的数据来构成一个整体,比如学生这个整体可以由姓名.年龄.身高 ...

  10. java 程序访问hdfs错误 hadoop2.2.0

    很奇怪的问题,程序在eclipse上跑没问题: 这就代码:FileSystem fs = FileSystem.get(URI.create(hdfs_file),  conf , "use ...