tujvd
asked on
Help me. I am a C++ beginer. I meet some proplems
The code sesion as below:
//====================
#include "stdafx.h"
#import "D:\Program Files\Common Files\System\ado\msado15.d ll" no_namespace rename("EOF","adoEOF")
inline void TESTHR(HRESULT x){if FAILED(x) _com_issue_error(x);};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
_bstr_t SqlStr;
_bstr_t CnnStr;
_bstr_t BookList;
_ConnectionPtr AdoCnn=NULL;
TESTHR(AdoCnn.CreateInstan ce(__uuido f(Connecti on15),NULL )); // Error in this line
_RecordsetPtr AdoRcs=NULL;
TESTHR(AdoRcs.CreateInstan ce(__uuido f(Recordse t))); // And this line too
CnnStr="driver={SQL Server};server=apphan04;ui d=sa;pwd=; database=W ebDemo";
AdoCnn->ConnectionString=C nnStr;
AdoCnn->ConnectionTimeout= 30;
AdoCnn->Open;
SqlStr="SELECT * FROM Books";
AdoRcs=AdoCnn->Execute(Sql Str,0,0);
BookList="";
if(!((AdoRcs->BOF)&&(AdoRc s->adoEOF) ))
{
BookList=AdoRcs->Fields->G etItem("Ti tle")->Val ue;
}
AdoRcs->Close();
AdoCnn->Close();
return 0;
}
//===== END OF CODE ==========
Builing is OK but error while execute program...
Debug Error
Program E:\Projects\AdoTest.exe
abnormal program termination
(Press Rety to debug the application).
After Retry another message
The exception breakpoint
The breakepoint has been reached.
(0x80000003) occurred in the application at location 0x004089e3.
Click on OK to terminate the program
Click on Cancel to debug the program
I don't what happen, pls the me how to solve this proplem.
Thank
//====================
#include "stdafx.h"
#import "D:\Program Files\Common Files\System\ado\msado15.d
inline void TESTHR(HRESULT x){if FAILED(x) _com_issue_error(x);};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
_bstr_t SqlStr;
_bstr_t CnnStr;
_bstr_t BookList;
_ConnectionPtr AdoCnn=NULL;
TESTHR(AdoCnn.CreateInstan
_RecordsetPtr AdoRcs=NULL;
TESTHR(AdoRcs.CreateInstan
CnnStr="driver={SQL Server};server=apphan04;ui
AdoCnn->ConnectionString=C
AdoCnn->ConnectionTimeout=
AdoCnn->Open;
SqlStr="SELECT * FROM Books";
AdoRcs=AdoCnn->Execute(Sql
BookList="";
if(!((AdoRcs->BOF)&&(AdoRc
{
BookList=AdoRcs->Fields->G
}
AdoRcs->Close();
AdoCnn->Close();
return 0;
}
//===== END OF CODE ==========
Builing is OK but error while execute program...
Debug Error
Program E:\Projects\AdoTest.exe
abnormal program termination
(Press Rety to debug the application).
After Retry another message
The exception breakpoint
The breakepoint has been reached.
(0x80000003) occurred in the application at location 0x004089e3.
Click on OK to terminate the program
Click on Cancel to debug the program
I don't what happen, pls the me how to solve this proplem.
Thank
_ConnectionPtr AdoCnn=NULL;
TESTHR(AdoCnn.CreateInstan ce(__uuido f(Connecti on15),NULL ));
_RecordsetPtr AdoRcs=NULL;
TESTHR(AdoRcs.CreateInstan ce(__uuido f(Recordse t))); // And this line too
This is your problem. AdoCnn not set to an instance of _ConnectionPtr, so the AdoCnn.CreateInstance call references a method of an object that isn't even there. You need to instanciate the objects, so they don't point to NULL. Try this instead:
_ConnectionPtr AdoCnn();
TESTHR(AdoCnn.CreateInstan ce(__uuido f(Connecti on15),NULL ));
_RecordsetPtr AdoRcs();
TESTHR(AdoRcs.CreateInstan ce(__uuido f(Recordse t))); // And this line too
Don't be alarmed if this doesn't work, I'm not very familiar with the Windows API. The point is, you try to call a method of an object that doesn't exist. The compiler doesn't get that, but at runtime, this will trigger an error.
Anyway, as a beginner to C++, I wouldn't start doing ADO database calls. This is a little too tough, you may want to gather some experience before doing this sort of stuff.
I hope this helps.
TESTHR(AdoCnn.CreateInstan
_RecordsetPtr AdoRcs=NULL;
TESTHR(AdoRcs.CreateInstan
This is your problem. AdoCnn not set to an instance of _ConnectionPtr, so the AdoCnn.CreateInstance call references a method of an object that isn't even there. You need to instanciate the objects, so they don't point to NULL. Try this instead:
_ConnectionPtr AdoCnn();
TESTHR(AdoCnn.CreateInstan
_RecordsetPtr AdoRcs();
TESTHR(AdoRcs.CreateInstan
Don't be alarmed if this doesn't work, I'm not very familiar with the Windows API. The point is, you try to call a method of an object that doesn't exist. The compiler doesn't get that, but at runtime, this will trigger an error.
Anyway, as a beginner to C++, I wouldn't start doing ADO database calls. This is a little too tough, you may want to gather some experience before doing this sort of stuff.
I hope this helps.
ASKER
I've found the ans for this proplem. Now it's OK after I add a ::CoInitilize(NULL).
ASKER
You know
I use the way that import msado15.dll into my projects. Are there any way to use ADO in a C++ program? Which you offen use?
I'm writing a COM+ component. Following the instruction in a book, I include comsvsc.h file - that file doesn't exists in my PC - and I can find after install Platform SDK...
My PC with W2K Advance SP2, are the any way to write a COM+ component without install new Platform SDK ?
Thank.
I use the way that import msado15.dll into my projects. Are there any way to use ADO in a C++ program? Which you offen use?
I'm writing a COM+ component. Following the instruction in a book, I include comsvsc.h file - that file doesn't exists in my PC - and I can find after install Platform SDK...
My PC with W2K Advance SP2, are the any way to write a COM+ component without install new Platform SDK ?
Thank.
This is the source code in my early project,in data connectiong you must ensure the instance of the connection
is created succeeded .
try
{
hr=m_pConnection.CreateIns tance(__uu idof(Conne ction));
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open(
bstr_t(L"Provider=Microsof t.Jet.OLED B.4.0; Data Source=db.mdb;"),
_bstr_t(L""),
_bstr_t(L""),
adModeUnknown);
if(SUCCEEDED(hr))
{
m_IsConnectionOpen=TRUE;
}
}
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Descript ion());
TRACE("Exception thrown for classes generated by #import");
TRACE("\tCode=%08lx\n",e.E rror());
TRACE("\tCode meaning= %s \n",e.ErrorMessage());
TRACE("\tSource= %s \n",(LPCTSTR)bstrSource);
TRACE("\tDescription= %s \n",(LPCTSTR)bstrDescripti on);
}
catch(...)
{
TRACE("***Unhandled Exception***");
}
is created succeeded .
try
{
hr=m_pConnection.CreateIns
if(SUCCEEDED(hr))
{
hr=m_pConnection->Open(
bstr_t(L"Provider=Microsof
_bstr_t(L""),
_bstr_t(L""),
adModeUnknown);
if(SUCCEEDED(hr))
{
m_IsConnectionOpen=TRUE;
}
}
}
catch(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Descript
TRACE("Exception thrown for classes generated by #import");
TRACE("\tCode=%08lx\n",e.E
TRACE("\tCode meaning= %s \n",e.ErrorMessage());
TRACE("\tSource= %s \n",(LPCTSTR)bstrSource);
TRACE("\tDescription= %s \n",(LPCTSTR)bstrDescripti
}
catch(...)
{
TRACE("***Unhandled Exception***");
}
Dear tujvd
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity, I will suggest to
"refund the points and PAQ at zero points"
since you found you rown solution.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity, I will suggest to
"refund the points and PAQ at zero points"
since you found you rown solution.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you press 'Retry'? It'll take you straight to the source line where the problem is...