读书人

C++在获取系统磁盘符号时报错:Debug

发布时间: 2012-03-23 12:06:21 作者: rapoo

C++在获取系统磁盘符号时报错:Debug Assertion Failed! 请大家帮忙看一下,怎么解决
用VC++实现一个文件管理器 在获取系统磁盘符号时 报错:Debug Assertion Failed!的错误提示一直没法通过,在网上找了好多也没有解决,贴出来大家帮忙看看是哪儿出问题了,谢谢。
主要涉及到一个类:FilesAssistantDlg类
我在FilesAssistantDlg.h文件中加入了如下两行代码:

C/C++ code
 private:   void InitPath(void); private:    CTreeCtrl m_tcDirectories;


接着在FilesAssistantDlg.cpp中写了一个将系统磁盘符号显示在 Tree control 控件中的方法,如下
C/C++ code
void CFilesAssistantDlg::InitPath(void){    wchar_t *pLogicalDrivers  = new wchar_t[MAX_PATH];    pLogicalDrivers[0] = '\0';    int nLen = GetLogicalDriveStrings(MAX_PATH-1,pLogicalDrivers);    if ( 0 == nLen)    { return ; }int nDriverNameLength = wcslen(L"C:\\")+1;int i  =0;TVINSERTSTRUCT  tvInsertItem;tvInsertItem.hParent = NULL;tvInsertItem.hInsertAfter = NULL;tvInsertItem.item.mask = TVIF_TEXT;do{    tvInsertItem.item.pszText = pLogicalDrivers +i;    m_tcDirectories.InsertItem(&tvInsertItem);    i += nDriverNameLength;}while(i<nLen);delete[] pLogicalDrivers;}


然后在 BOOL CFilesAssistantDlg::OnInitDialog()方法中加入 一行:InitPath();
来调用这个 InitPath(); 方法 但是 在调试运行过程中老是报错:
C/C++ code
   Debug Assertion Failed!   Program:...studio 2010 \Projects=FilesAssistant\Debug\FilesAssistant.exe   File:f:\dd\vctools\vc7libs\ship\atlmfc\include\afxcmn.inl   Line:263     For information on how your program can cause an assertion failure, see the Visual C ++ documentation on asserts.


请大家帮忙看看,小弟是新手,不怎么懂

[解决办法]
ASSERT(::IsWindow(m_hWnd));

你的m_tcDirectories不正确

在OnInitDialog()的时候窗口句柄还没生效!换其它地方调用InitPath();
[解决办法]
就是说在OnInitDialog()的时候m_tcDirectories.InsertItem(&tvInsertItem);这句是错误的,放到其它地方去(比如对话框加载完毕)

[解决办法]
你的CTreeCtrl m_tcDirectories;对象关联了窗口没?

读书人网 >C++

热点推荐