Hi,
I am creating multiple threads to access a database and retrieve records using ADO. I am getting runtime error "abnormal program termination'.
My main() function creates an Database connection and recordset object. it also creates multiple thread which accesses ThreadFunc() . it passes the recordset object to ThreadFunc().
ThreadFunc() function receives the recordset object prints the record and moves the pointer to the next record.
I am attaching the code below. please correct me.
ThanksCode:#import "C:\Program files\Common Files\System\Ado\msado15.dll" no_namespace rename("EOF", "ADOEOF") #include <windows.h> #include <stdio.h> #include <ole2.h> #include <conio.h> #define NUM_THREADS 12 CRITICAL_SECTION cs; HRESULT hr; _ConnectionPtr pConn; _RecordsetPtr pRs; /* ThreadFunc is responsible for accessing the Recordset. and moving through recordset. */ DWORD WINAPI ThreadFunc(LPVOID lpParam) { EnterCriticalSection(&cs); _bstr_t val = pRs->Fields->Item[_variant_t("BTN")]->Value; printf("%s\n",(char*)val); pRs->MoveNext(); //printf("Hello World, I'm thread# %d\n", (int)lpParam); //printf("hello world \n"); LeaveCriticalSection(&cs); return 0; }//ThreadFunc void main(){ DWORD dwThreadId; HANDLE hThread[NUM_THREADS]; int n; CoInitialize(NULL); InitializeCriticalSection(&cs); // Create an ADO connection to database try{ hr = pConn.CreateInstance(__uuidof(Connection)); hr = pRs.CreateInstance(__uuidof(Recordset)); _bstr_t strConn("Provider=sqloledb;server=SINF005;Trusted_Connection=yes;database=Core;"); pConn->Open(strConn,"","",adConnectUnspecified); pRs->Open("SELECT top 10 Npa_Num from Npa (NOLOCK)", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText); //pRs->Open("SELECT BTN from mbs_GetSpitFireEasyPay (NOLOCK)", pConn.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText); }catch(_com_error &e){ printf("Error\n"); printf("\tCode meaning = %s", e.ErrorMessage()); } // create all the threads for (n = 0; n < NUM_THREADS; n++) { hThread[n] = CreateThread(NULL, 0, ThreadFunc, &pRs, 0, &dwThreadId); if (hThread == NULL) fprintf(stderr, "Failed to create thread# %d", n); }//for for (n = 0; n < NUM_THREADS; n++) { if (hThread[n] != NULL) WaitForSingleObject(hThread[n], INFINITE); }//for DeleteCriticalSection(&cs); printf("This is after all threads exited\n"); CoUninitialize(); }



LinkBack URL
About LinkBacks


