关于无效指针

GetForHelpHere 2009-03-24 12:06:58
MFC单文档 ADO数据库 《精通MFC》21章
将窗口分成两部分,Frame中调用其它类的函数提示找不到指针,本人新手,实在找不出为什么

代码如下,可能比较长。。。。。。

全局变量 _ConnectionPtr m_pConnection; _RecordsetPtr m_pRecordset;
CMainFrame::CMainFrame()
{
// m_bAutoMenuEnable = FALSE;
// TODO: add member initialization code here
//初始化COM
::CoInitialize(NULL);

m_pConnection.CreateInstance("ADODB.Connection");

//连接数据库
try
{
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;User ID=admin;Data Source=.\\info.mdb;; ","","",adModeUnknown);

}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败!");
}
CString strSql="select * from Info";

try
{
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(strSql.GetBuffer(0), m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{AfxMessageBox("打开记录集失败!");}

catch (...) {AfxMessageBox("打开记录集失败!");}
}
------------------------------------------------------------

CMainFrame()中创建两个视图:

if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftListView), CSize(rect.Width()/6, 0), pContext))
{
TRACE0("Failed to create left pane view\n");
return FALSE;
}
//右窗格是一个包含几个具有不同视图的辅助框架窗口
if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CRightPaneFrame), CSize(0, 0), pContext))
{
TRACE0("Failed to create right pane frame\n");
return FALSE;
}
----------------------------------------------------------------------------------------------------
class CRightPaneFrame : public CFrameWnd //右视图再次分成两个视图
BOOL CRightPaneFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
m_wndSplitter.CreateStatic(this, 2, 1);
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CRightListView), CSize(150,0), pContext);
m_wndSplitter.CreateView(1,0,RUNTIME_CLASS(CPerInfoView), CSize(0,0), pContext);
//初始化指针
m_pListView=(CRightListView*)m_wndSplitter.GetPane(0,0);//ListContrl(Dlg)
m_pPerInfoView=(CPerInfoView*)m_wndSplitter.GetPane(1,0);//Dlg
RecalcLayout();
return TRUE;
}
-----------------------------------------------------------
void CRightListView::AddDateToRightList()//ListContrl中显示记录集数据
{
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
int i=0;
_variant_t varValue;
while(!m_pRecordset->adoEOF)
{
CString str;
//负责插入条目
m_RightList.InsertItem (i,str);
//卡号
varValue=m_pRecordset->GetFields()->GetItem("卡号")->Value;
str=VariantToCString(varValue);
m_RightList.SetItemText (i, 0, str);
.
.
.
i++;
m_pRecordset->MoveNext();
}

m_pRecordset->Close();
m_pRecordset=NULL;
}
-----------------------------------------------
新加一个菜单 用来调用void CRightListView::AddDateToRightList()//加在CMainFrame里 加在CRightPaneFrame里菜单总是灰的 请教..
void CMainFrame::OnViewData()
{
CString strError;
try
{
// TODO: Add your command handler code here
if(m_pRecordset->BOF)
{
AfxMessageBox("88");
return;
}
// CRightListView::AddDateToRightList();

CMainFrame* pMainFrm=(CMainFrame*)AfxGetMainWnd();
CRightPaneFrame* pRightFrm;
CRightListView* pListView;
pRightFrm=(CRightPaneFrame*)pMainFrm->m_wndSplitter.GetPane(0,1);
pListView=pRightFrm->m_pListView;

pListView->AddDateToRightList();


}catch(_com_error e)
{
strError.Format("%s",e.ErrorMessage());
AfxMessageBox(strError);
}

}
菜单可用 但是一点就报出“无效指针” 不知道为什么。。。。。

还有就是在LISTCONTRL里响应单击消息,显示在INFO DLG里也提示“无效指针”是不是记录集游标造成的?。。。。。
...全文
400 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiangorqiang 2009-03-24
  • 打赏
  • 举报
回复
m_pRecordset还没有初始化。
GetForHelpHere 2009-03-24
  • 打赏
  • 举报
回复
找到原因了 感谢大家 1L 说对了
菜牛 2009-03-24
  • 打赏
  • 举报
回复
有一点不理解,AfxGetMainWnd()获取的应该就是CMainFrame对象的指针,那么为什么要在CMainFrame::OnViewData里面再调用AfxGetMainWnd()?另外这个m_pRecordset到底是CMainFrame的成员还是CRightListView的成员?
levelmouse 2009-03-24
  • 打赏
  • 举报
回复
调试进去看看

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧