ShelleyWang
May 15th, 2006, 09:30 AM
I had create a dialog(named "add new") class as below:
this the .h file:
class CAddNewDialog
{
public:
CAddNewDialog(int);
virtual ~CAddNewDialog(void);
.....
.....
protected:
_ConnectionPtr pCon;
_RecordsetPtr pRs;
_RecordsetPtr pRsLeixing;
HRESULT hr;
}
and this is the .cpp file:
CAddNewDialog::CAddNewDialog(int nID)
{
hr = S_OK;
pCon = NULL;
pRs = NULL;
pRsLeixing = NULL;
::CoInitialize(NULL);
try
{
hr = pCon.CreateInstance(__uuidof(Connection));
hr = pCon->Open(_bstr_t(TEXT("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb")), "", "", adModeUnknown);
hr = pRs.CreateInstance(TEXT("ADODB.Recordset"));
hr = pRsLeixing.CreateInstance(TEXT("ADODB.Recordset"));
}
catch(_com_error e)
{
MessageBox(NULL, e.ErrorMessage(), TEXT("Error"), MB_OK | MB_ICONSTOP);
}
}
CAddNewDialog::~CAddNewDialog(void)
{
if( pRsLeixing && (pRsLeixing->GetState() != adStateClosed))
pRsLeixing->Close();
pRsLeixing = NULL;
if(pRs && (pRs->GetState() != adStateClosed))
pRs->Close();
pRs = NULL;
if(pCon && (pCon->State != adStateClosed))
pCon->Close();
pCon = NULL;
::CoUninitialize();
}
the problem is when I push the "new" button in main form(the parent window of "add new") for first time, the application runs normally, after the "add new" dialog created and I push the "brower" button(it open the common 'Open File Dialog') and selected a bitmap file, then exit the "add new" dialog.
When I push the "new" button again in main form, the application hangs.
I had debuged the code , and it hangs at the RED code below:
CAddNewDialog::CAddNewDialog(int nID)
{
hr = S_OK;
pCon = NULL;
pRs = NULL;
pRsLeixing = NULL;
::CoInitialize(NULL);
try
{
hr = pCon.CreateInstance(__uuidof(Connection));
hr = pCon->Open(_bstr_t(TEXT("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb")), "", "", adModeUnknown);
hr = pRs.CreateInstance(TEXT("ADODB.Recordset"));
hr = pRsLeixing.CreateInstance(TEXT("ADODB.Recordset"));
}
catch(_com_error e)
{
MessageBox(NULL, e.ErrorMessage(), TEXT("Error"), MB_OK | MB_ICONSTOP);
}
}
i had tried and tried, I cannot find any problem with my code.
Anyone can tell me where the mistakes with my code ?
BTW: I wrote all code with Win32
this the .h file:
class CAddNewDialog
{
public:
CAddNewDialog(int);
virtual ~CAddNewDialog(void);
.....
.....
protected:
_ConnectionPtr pCon;
_RecordsetPtr pRs;
_RecordsetPtr pRsLeixing;
HRESULT hr;
}
and this is the .cpp file:
CAddNewDialog::CAddNewDialog(int nID)
{
hr = S_OK;
pCon = NULL;
pRs = NULL;
pRsLeixing = NULL;
::CoInitialize(NULL);
try
{
hr = pCon.CreateInstance(__uuidof(Connection));
hr = pCon->Open(_bstr_t(TEXT("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb")), "", "", adModeUnknown);
hr = pRs.CreateInstance(TEXT("ADODB.Recordset"));
hr = pRsLeixing.CreateInstance(TEXT("ADODB.Recordset"));
}
catch(_com_error e)
{
MessageBox(NULL, e.ErrorMessage(), TEXT("Error"), MB_OK | MB_ICONSTOP);
}
}
CAddNewDialog::~CAddNewDialog(void)
{
if( pRsLeixing && (pRsLeixing->GetState() != adStateClosed))
pRsLeixing->Close();
pRsLeixing = NULL;
if(pRs && (pRs->GetState() != adStateClosed))
pRs->Close();
pRs = NULL;
if(pCon && (pCon->State != adStateClosed))
pCon->Close();
pCon = NULL;
::CoUninitialize();
}
the problem is when I push the "new" button in main form(the parent window of "add new") for first time, the application runs normally, after the "add new" dialog created and I push the "brower" button(it open the common 'Open File Dialog') and selected a bitmap file, then exit the "add new" dialog.
When I push the "new" button again in main form, the application hangs.
I had debuged the code , and it hangs at the RED code below:
CAddNewDialog::CAddNewDialog(int nID)
{
hr = S_OK;
pCon = NULL;
pRs = NULL;
pRsLeixing = NULL;
::CoInitialize(NULL);
try
{
hr = pCon.CreateInstance(__uuidof(Connection));
hr = pCon->Open(_bstr_t(TEXT("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb")), "", "", adModeUnknown);
hr = pRs.CreateInstance(TEXT("ADODB.Recordset"));
hr = pRsLeixing.CreateInstance(TEXT("ADODB.Recordset"));
}
catch(_com_error e)
{
MessageBox(NULL, e.ErrorMessage(), TEXT("Error"), MB_OK | MB_ICONSTOP);
}
}
i had tried and tried, I cannot find any problem with my code.
Anyone can tell me where the mistakes with my code ?
BTW: I wrote all code with Win32