关于无效指针
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里也提示“无效指针”是不是记录集游标造成的?。。。。。