ofstream(myfile); SERVICE_TABLE_ENTRY DispatchTable[] = { { ptr, ServiceMain },{ NULL, NULL } }; if (!StartServiceCtrlDispatcher(DispatchTable)) { /*myfile.open("D:/New folder/Readerr4.txt", ios_base::app); myfile << endl; myfile << "1" + GetLastError(); myfile.close();*/ } else { ///***Работает*** myfile.open("D:/New folder/Readerr4.txt", ios_base::app); myfile << endl; myfile << "2" + GetLastError(); myfile.close(); //***Не работает*** HRESULT hr = S_OK; _bstr_t valField1; _bstr_t valField2; CoInitialize(NULL); _bstr_t strCnn("Provider=SQLOLEDB;Persist Security Info=False; Initial Catalog= OPUBase ;Data Source=NIK\\SQLEXPRESS;Integrated Security=SSPI;"); _RecordsetPtr pRstAuthors = NULL; // Call Create instance to instantiate the Record set hr = pRstAuthors.CreateInstance(__uuidof(Recordset)); pRstAuthors->Open("SELECT TOP (100) [DocID] ,[DocCode] FROM [OPUBase].[dbo].[DocList] WHERE DocCode = 1", strCnn, adOpenDynamic, adLockBatchOptimistic, adCmdText); pRstAuthors->MoveFirst(); while (!pRstAuthors->EndOfFile) { myfile.open("D:/New folder/Readerr4.txt", ios::app); valField1 = pRstAuthors->Fields->GetItem("DocID")->Value; valField2 = pRstAuthors->Fields->GetItem("DocCode")->Value; cout << valField2 + " " + valField1; cout << endl; myfile << endl; myfile << valField1 + " " + valField2; myfile.close(); pRstAuthors->MoveNext(); } } 

When you run just exe, everything works. When you start a service, it writes only the first part, the while loop (! PRstAuthors-> EndOfFile) is not executed. Maybe someone knows how to track the error in the Service?

  • You are missing part of the code explicitly. - MrBin
  • myfile << "2" + GetLastError(); does absolutely nothing what you suppose - int3

1 answer 1

To track errors in your service you must:

  1. Run the service as a separate process (There is a corresponding registration flag);
  2. Manually set the delay;
  3. The debugger will join the service process;

Voila!

There are a lot of ways, from your own logging to viewing the error logs of services.