Can anyone see the problem-newbie [modified]
-
Hi again Just when i thought i'm done this comes up i wrote this code for testing it compiles ok but it then crashes at a certain point can anyone help me find the error
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") using namespace std; void main() { string token="viagra"; HRESULT hr = S_OK; CoInitialize(NULL); try { _ConnectionPtr m_pConn; HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =bayesfilter.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; // Formerly opened connection pointer string command="SELECT token,Spam,Ham,P,app FROM tblDictionary WHERE token='"+ token; command+="'"; pCommand->CommandText = command.data(); //the problem is definetly in the following part _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; int valField3; double valField4; int valField5; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5); pRecordset->MoveNext(); } } else cout<<"No record found insert?"<Close(); }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } CoUninitialize(); }
Any ideas thanks again -- modified at 14:41 Thursday 1st June, 2006 -
Hi again Just when i thought i'm done this comes up i wrote this code for testing it compiles ok but it then crashes at a certain point can anyone help me find the error
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") using namespace std; void main() { string token="viagra"; HRESULT hr = S_OK; CoInitialize(NULL); try { _ConnectionPtr m_pConn; HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =bayesfilter.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; // Formerly opened connection pointer string command="SELECT token,Spam,Ham,P,app FROM tblDictionary WHERE token='"+ token; command+="'"; pCommand->CommandText = command.data(); //the problem is definetly in the following part _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; int valField3; double valField4; int valField5; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5); pRecordset->MoveNext(); } } else cout<<"No record found insert?"<Close(); }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } CoUninitialize(); }
Any ideas thanks again -- modified at 14:41 Thursday 1st June, 2006 -
Hi again Just when i thought i'm done this comes up i wrote this code for testing it compiles ok but it then crashes at a certain point can anyone help me find the error
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") using namespace std; void main() { string token="viagra"; HRESULT hr = S_OK; CoInitialize(NULL); try { _ConnectionPtr m_pConn; HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =bayesfilter.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; // Formerly opened connection pointer string command="SELECT token,Spam,Ham,P,app FROM tblDictionary WHERE token='"+ token; command+="'"; pCommand->CommandText = command.data(); //the problem is definetly in the following part _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; int valField3; double valField4; int valField5; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5); pRecordset->MoveNext(); } } else cout<<"No record found insert?"<Close(); }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } CoUninitialize(); }
Any ideas thanks again -- modified at 14:41 Thursday 1st June, 2006antonaras wrote:
it then crashes at a certain point
which point is that? Cleek | Image Toolkits | Thumbnail maker
-
Hi again Just when i thought i'm done this comes up i wrote this code for testing it compiles ok but it then crashes at a certain point can anyone help me find the error
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") using namespace std; void main() { string token="viagra"; HRESULT hr = S_OK; CoInitialize(NULL); try { _ConnectionPtr m_pConn; HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =bayesfilter.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; // Formerly opened connection pointer string command="SELECT token,Spam,Ham,P,app FROM tblDictionary WHERE token='"+ token; command+="'"; pCommand->CommandText = command.data(); //the problem is definetly in the following part _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; int valField3; double valField4; int valField5; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5); pRecordset->MoveNext(); } } else cout<<"No record found insert?"<Close(); }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } CoUninitialize(); }
Any ideas thanks again -- modified at 14:41 Thursday 1st June, 2006Without actually knowing where it is crashing (and not being able to run it myself), here are the most likely places based on the code:
antonaras wrote:
valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5);
If GetItem returns NULL for any of those items at any point in time, you will get an access violation. Also, if the types don't match up (that is, if the field is a double and you are checking the int value), you will get strange results that could blow up your stack in a weird place. Additionally, the printf statement will blow up your stack the way it is written. When you have 2 arguments in the printf string, you should only have 2 variables to match them (you have 2 in the string and 4 trying to match them).
antonaras wrote:
m_pConn->Close();
You didn't close the recordset, so at this point, the connection is being closed. Now, when the Recordset smart pointer goes out of scope, it will attempt to close, but it will no longer have a connection. Try adding a
pRecordset->Close();
call prior to the connection's close call. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -- modified at 15:21 Thursday 1st June, 2006 -
Hi again Just when i thought i'm done this comes up i wrote this code for testing it compiles ok but it then crashes at a certain point can anyone help me find the error
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \ no_namespace rename("EOF", "EndOfFile") using namespace std; void main() { string token="viagra"; HRESULT hr = S_OK; CoInitialize(NULL); try { _ConnectionPtr m_pConn; HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection)); if (FAILED( hr )) cout<<"Can't create an intance of ADO.Connection"<Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =bayesfilter.MDB"), _bstr_t( "" ), _bstr_t( "" ), adModeUnknown ))) cout<<"Can't open datasource"<ActiveConnection = m_pConn; // Formerly opened connection pointer string command="SELECT token,Spam,Ham,P,app FROM tblDictionary WHERE token='"+ token; command+="'"; pCommand->CommandText = command.data(); //the problem is definetly in the following part _RecordsetPtr pRecordset; pRecordset.CreateInstance (__uuidof (Recordset)); pRecordset->CursorLocation = adUseClient; pRecordset->Open ( (IDispatch *) pCommand, vtMissing, adOpenStatic, adLockBatchOptimistic, adCmdUnknown); _bstr_t valField1; int valField2; int valField3; double valField4; int valField5; pRecordset->MoveFirst(); if (!pRecordset->EndOfFile) { while(!pRecordset->EndOfFile) { valField1 = pRecordset->Fields->GetItem("Token")->Value; valField2 = pRecordset->Fields->GetItem("Spam")->Value.intVal; valField3 = pRecordset->Fields->GetItem("Ham")->Value.intVal; valField4 = pRecordset->Fields->GetItem("P")->Value.dblVal; valField5 = pRecordset->Fields->GetItem("app")->Value.intVal; printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5); pRecordset->MoveNext(); } } else cout<<"No record found insert?"<Close(); }catch( _com_error &ce ) { printf("Error:%s\n",ce.Description); } CoUninitialize(); }
Any ideas thanks again -- modified at 14:41 Thursday 1st June, 2006antonaras wrote:
printf("%d - %s\n",(LPCSTR)valField1,valField2,valField3,valField5);
This should be:
printf("%d - %s\n", valField2, (LPCSTR) valField1);
"The largest fire starts but with the smallest spark." - David Crow