Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with connecting to MySQL db from my own windows service application

Problem with connecting to MySQL db from my own windows service application

Scheduled Pinned Locked Moved C / C++ / MFC
databasedebugginghelpcsharpc++
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A anumadhu

    yes, i am initalizing the ServiceStatus in the myServiceMain(int arg, char**argv) itself. i am connecting MySQL to database using this statement: try { usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); } catch(CException *e) { WriteIntoLogFile("db open Exception here");// And my log file is showing this stmt as the last stmt in it. return; } myServiceMain is as follows.... void myServiceMain(int argc, char **argv) { int result=-1; usbObj.WriteIntoLogFile("At DataSentinelMain"); ServiceStatus.dwServiceType = SERVICE_WIN32; ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwCurrentState = SERVICE_START_PENDING; ServiceStatus.dwControlsAccepted =SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN; ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwWaitHint =0; ServiceStatus.dwWin32ExitCode = 0; hStatus=RegisterServiceCtrlHandlerEx((LPCWSTR)SERVICE_NAME_USB,(LPHANDLER_FUNCTION_EX)ControlHandler, 0); usbObj.WriteIntoLogFile("Aftr calling RegisterServiceCtrlHandler"); if(hStatus==(SERVICE_STATUS_HANDLE)0) { usbObj.WriteIntoLogFile("hstatus returned SERVICE_STATUS_HANDLE INVALID"); return; } SetServiceStatus(hStatus,&ServiceStatus); usbObj.WriteIntoLogFile("before RegisterDeviceNotifications"); result=usbObj.RegisterForDeviceNotifications (hStatus); ServiceStatus.dwCurrentState=SERVICE_RUNNING; SetServiceStatus(hStatus,&ServiceStatus); if(result!=1) { usbObj.WriteIntoLogFile("RegisterDeviceNotification returned invalid value"); return; } usbObj.WriteIntoLogFile("After the call of RegisterDeviceNotification"); InitService();//27March 07 usbObj.WriteIntoLogFile("After the call to InitService"); /* Enumerate the USB DEVICES */ result=0; while(ServiceStatus.dwCurrentState = SERVICE_RUNNING) { result++; usbObj.WriteIntoLogFile("Call to USBServiceFunc from While of ServiceStatus_running"); usbObj.USBService (); usbObj.WriteIntoLogFile("after and end of USBServiceFunc"); if(result>100) { ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hStatus,&ServiceStatus); return; } Sleep(5000); } ServiceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hStatus,&ServiceStatus); return; } usbObj.USBService () { /*this contains a method called usbObj.OpenDevices()..where the db is opened using the above stms..*/ } i have seen even the MySQL.. service also.. which is getting started before the start of m

    J Offline
    J Offline
    JudyL_MD
    wrote on last edited by
    #4

    Just looking at the code, I don't see anything wrong. You'll have to use the debugger and see where it's hanging up. Put a call to DebugBreak at the start of your ServiceMain - this will force a breakpoint to occur and you can then use VS to debug it. Judy

    A 1 Reply Last reply
    0
    • J JudyL_MD

      Just looking at the code, I don't see anything wrong. You'll have to use the debugger and see where it's hanging up. Put a call to DebugBreak at the start of your ServiceMain - this will force a breakpoint to occur and you can then use VS to debug it. Judy

      A Offline
      A Offline
      anumadhu
      wrote on last edited by
      #5

      Thanks Judy! Yeah, i have even debugged this service in following way: 1) first using the exe, i created the service : sc create MyService binpath= D:\MyService.exe 2) Then started it from the control pannel. After it shows the status as "Started". I go to step 3. 3) Then using the Visual Studio (my development environment) + Debug + Attach Process + MyService.exe+attach. 4) And kept breakpoints in the starting of the MyServiceMain() and at the while() and also at the controlHandler function. void ControlHandler(DWORD dwServiceCtrlEvent, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) { switch(dwServiceCtrlEvent) { case SERVICE_CONTROL_DEVICEEVENT: usbObj.OnMyDeviceEvent(dwEventType,lpEventData); break; case SERVICE_CONTROL_STOP: usbObj.UnRegisterEvents(); ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;//SERVICE_STOPPED; SetServiceStatus(hStatus,&ServiceStatus); break; case SERVICE_CONTROL_PAUSE: break; case SERVICE_CONTROL_CONTINUE: break; case SERVICE_CONTROL_INTERROGATE: break; default: break; } SetServiceStatus(hStatus,&ServiceStatus); } When i connect the USB device, then only the breakpoint is getting triggered and that too at the controlHandler()'s SERVICE_CONTROL_DEVICEEVENT. But never in or at the beginning at the MyServiceMain(). That is why, i used a WriteIntoLogFile() to get the required information regarding the order of execution of lines in my application. Are you sure.. that my code have no errors?? Then i am bit worried..abt the cause of problem. Thanks in advance.. Anee

      J 1 Reply Last reply
      0
      • A anumadhu

        Thanks Judy! Yeah, i have even debugged this service in following way: 1) first using the exe, i created the service : sc create MyService binpath= D:\MyService.exe 2) Then started it from the control pannel. After it shows the status as "Started". I go to step 3. 3) Then using the Visual Studio (my development environment) + Debug + Attach Process + MyService.exe+attach. 4) And kept breakpoints in the starting of the MyServiceMain() and at the while() and also at the controlHandler function. void ControlHandler(DWORD dwServiceCtrlEvent, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) { switch(dwServiceCtrlEvent) { case SERVICE_CONTROL_DEVICEEVENT: usbObj.OnMyDeviceEvent(dwEventType,lpEventData); break; case SERVICE_CONTROL_STOP: usbObj.UnRegisterEvents(); ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;//SERVICE_STOPPED; SetServiceStatus(hStatus,&ServiceStatus); break; case SERVICE_CONTROL_PAUSE: break; case SERVICE_CONTROL_CONTINUE: break; case SERVICE_CONTROL_INTERROGATE: break; default: break; } SetServiceStatus(hStatus,&ServiceStatus); } When i connect the USB device, then only the breakpoint is getting triggered and that too at the controlHandler()'s SERVICE_CONTROL_DEVICEEVENT. But never in or at the beginning at the MyServiceMain(). That is why, i used a WriteIntoLogFile() to get the required information regarding the order of execution of lines in my application. Are you sure.. that my code have no errors?? Then i am bit worried..abt the cause of problem. Thanks in advance.. Anee

        J Offline
        J Offline
        JudyL_MD
        wrote on last edited by
        #6

        It sounds like the problem is at the beginning. Setting a breakpoint at the beginning of ServiceMain _after_ the service is started won't do you any good since that code has already executed. You need to use the DebugBreak function at the very start of your ServiceMain. It will force a breakpoint and the OS will allow you to select a debugger to attach to the process - in this case your service. You can then debug your startup logic which, by your description, is where your problem is. I don't know that your code doesn't have any errors. In a visual inspection, I don't see one but I don't know what your problem domain is and your thinking about how your code solves that problem. I don't have your whole program nor do I want you to post it. Your problem is at the beginning - you need to debug that, conparing your intended logic flow with what your code actually does. The only way to debug the beginning of a service is by using the DebugBreak .... or coding an infinite loop and attaching the debugger as you are already doing and then breaking the loop. :) Use DebugBreak Judy

        A 1 Reply Last reply
        0
        • J JudyL_MD

          It sounds like the problem is at the beginning. Setting a breakpoint at the beginning of ServiceMain _after_ the service is started won't do you any good since that code has already executed. You need to use the DebugBreak function at the very start of your ServiceMain. It will force a breakpoint and the OS will allow you to select a debugger to attach to the process - in this case your service. You can then debug your startup logic which, by your description, is where your problem is. I don't know that your code doesn't have any errors. In a visual inspection, I don't see one but I don't know what your problem domain is and your thinking about how your code solves that problem. I don't have your whole program nor do I want you to post it. Your problem is at the beginning - you need to debug that, conparing your intended logic flow with what your code actually does. The only way to debug the beginning of a service is by using the DebugBreak .... or coding an infinite loop and attaching the debugger as you are already doing and then breaking the loop. :) Use DebugBreak Judy

          A Offline
          A Offline
          anumadhu
          wrote on last edited by
          #7

          Thanks Judy! I kept DebugBreak() in the MyServiceMain()... and when i traversed throughout the debug.. the problem was same what i told before.. the usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); is not getting opened and therefore the catch block is getting executed. Whereas i want the database to be connected. I don know... how to solve..lets hope for the any other right way. Thanks in Advance Anee

          J 1 Reply Last reply
          0
          • A anumadhu

            Thanks Judy! I kept DebugBreak() in the MyServiceMain()... and when i traversed throughout the debug.. the problem was same what i told before.. the usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); is not getting opened and therefore the catch block is getting executed. Whereas i want the database to be connected. I don know... how to solve..lets hope for the any other right way. Thanks in Advance Anee

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #8

            That I can't help with - databases are not my thing. At least now you know that it is exclusively a database problem and not a service problem. Make sure you step into the Open and trace it as far into the guts of the provided source code as you can. It might give some help on finding the problem. One thing to be aware of when running as a service is your permissions. You are not a "normal" program. You are running in the Local System account. You haven't mentioned which OS this is on. XP is usually pretty good about letting services do all the usual things (excepting UI). Vista is much more restrictive. Good luck!! Judy

            A 1 Reply Last reply
            0
            • J JudyL_MD

              That I can't help with - databases are not my thing. At least now you know that it is exclusively a database problem and not a service problem. Make sure you step into the Open and trace it as far into the guts of the provided source code as you can. It might give some help on finding the problem. One thing to be aware of when running as a service is your permissions. You are not a "normal" program. You are running in the Local System account. You haven't mentioned which OS this is on. XP is usually pretty good about letting services do all the usual things (excepting UI). Vista is much more restrictive. Good luck!! Judy

              A Offline
              A Offline
              anumadhu
              wrote on last edited by
              #9

              Great Thanks for helping me out in fixing the exact problem area. And now moving towards the solution part, i am using the process explorer to get the exact information of the services and processes and their threads,DLLs etc. After placing DebugBreak()... in the catch block of : try{ DebugBreak(); usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); } catch(CException *e) { WriteIntoLogFile("db open Exception in the User_USB_login_data"); DebugBreak();//Here the breakpoint is getting triggered at last. and the error window is showing like this: } Error window displays: Windows has triggered a breakpoint in MyService.exe. This may be due to a corruption of the heap, and indicates a bug in MyService.exe or any of the DLLs it has loaded. The output window may have more diagnostic information ............................. my development environment is Windows XP and VC++.Net 2005 Any ideas..pls! :confused: Thanks in Advance

              Anee

              J 1 Reply Last reply
              0
              • A anumadhu

                Great Thanks for helping me out in fixing the exact problem area. And now moving towards the solution part, i am using the process explorer to get the exact information of the services and processes and their threads,DLLs etc. After placing DebugBreak()... in the catch block of : try{ DebugBreak(); usbDB.OpenEx(szConnectString_USB,CDatabase::noOdbcDialog); } catch(CException *e) { WriteIntoLogFile("db open Exception in the User_USB_login_data"); DebugBreak();//Here the breakpoint is getting triggered at last. and the error window is showing like this: } Error window displays: Windows has triggered a breakpoint in MyService.exe. This may be due to a corruption of the heap, and indicates a bug in MyService.exe or any of the DLLs it has loaded. The output window may have more diagnostic information ............................. my development environment is Windows XP and VC++.Net 2005 Any ideas..pls! :confused: Thanks in Advance

                Anee

                J Offline
                J Offline
                JudyL_MD
                wrote on last edited by
                #10

                You should be hitting the DebugBreak in the try section. When the Windows message appears, there should be a "debug" option / button somewhere in the window. If you select that, you should get a window from the Visual Studio Just-In-Time debugger listing the possible debuggers to use. Select Visual Studion 2005 - if you have your service project already open, it should go right to the line with the DebugBreak. Use "set next statement" to skip to the next line of code. Is this not happening? To check the obvious, you are running a debug build of your service, yes? Make sure Just-In-Time debugging is enabled (Tools -> OPtions -> Debugging). Judy

                A 2 Replies Last reply
                0
                • J JudyL_MD

                  You should be hitting the DebugBreak in the try section. When the Windows message appears, there should be a "debug" option / button somewhere in the window. If you select that, you should get a window from the Visual Studio Just-In-Time debugger listing the possible debuggers to use. Select Visual Studion 2005 - if you have your service project already open, it should go right to the line with the DebugBreak. Use "set next statement" to skip to the next line of code. Is this not happening? To check the obvious, you are running a debug build of your service, yes? Make sure Just-In-Time debugging is enabled (Tools -> OPtions -> Debugging). Judy

                  A Offline
                  A Offline
                  anumadhu
                  wrote on last edited by
                  #11

                  Hello, Now i can see the cause of CDBException. It shows that "Data source name not found and no default driver specified". I am with the administrative privileges in windows XP. And also the dsn connection is checked with MySQL ODBC driver. And also the same dsn is used with another application to check for its working.. and the dsn was working fine in that application(which is not a windows service..it is an ordinary VC++.Net2005 application). But i am unable to locate why this dsn is not accessible from my windows service application. :~ :confused: :suss: I guess, the dsn (of MySQL ODBC 3.51 driver) is not getting accessible from my windows service, probably because of some security issues or something else..which i am unable to see or fix.(probably related to windows internals) if anybody has atleast a clue... is greatly appreciable... Thanks in advance..

                  Anee

                  1 Reply Last reply
                  0
                  • J JudyL_MD

                    You should be hitting the DebugBreak in the try section. When the Windows message appears, there should be a "debug" option / button somewhere in the window. If you select that, you should get a window from the Visual Studio Just-In-Time debugger listing the possible debuggers to use. Select Visual Studion 2005 - if you have your service project already open, it should go right to the line with the DebugBreak. Use "set next statement" to skip to the next line of code. Is this not happening? To check the obvious, you are running a debug build of your service, yes? Make sure Just-In-Time debugging is enabled (Tools -> OPtions -> Debugging). Judy

                    A Offline
                    A Offline
                    anumadhu
                    wrote on last edited by
                    #12

                    Hello, After debugging what i found is that, the CDBException at the following statement raises an exception: cdb.openEx(szConnectstring, CDatabase::noODBCDialog) And the message throwed is: "Data source name not found and no default driver specified" Whereas when i check for this dsn's status..from control panel the test connection is showing success. And also i am using the same dsn and same database from other VC++.Net2005 application ( this is not a windows service application), this application is getting connected to MySQL database using the same dsn and also able to read the records from one of the tables there(which shown in the applications UI window). I am not creating the windows service using the .Net framework. I am creating the my windows service application using the winsvc.h and advapi32.dll. I guess, since the application from which i am trying to connect to MySQL database is a windows service application, so probably causing some problem (due to some hidden windows internals..issues).. which i am unable to fix... Can anybody.. help me out by giving the clues.. Thank you..

                    Anee

                    A 1 Reply Last reply
                    0
                    • A anumadhu

                      Hello, After debugging what i found is that, the CDBException at the following statement raises an exception: cdb.openEx(szConnectstring, CDatabase::noODBCDialog) And the message throwed is: "Data source name not found and no default driver specified" Whereas when i check for this dsn's status..from control panel the test connection is showing success. And also i am using the same dsn and same database from other VC++.Net2005 application ( this is not a windows service application), this application is getting connected to MySQL database using the same dsn and also able to read the records from one of the tables there(which shown in the applications UI window). I am not creating the windows service using the .Net framework. I am creating the my windows service application using the winsvc.h and advapi32.dll. I guess, since the application from which i am trying to connect to MySQL database is a windows service application, so probably causing some problem (due to some hidden windows internals..issues).. which i am unable to fix... Can anybody.. help me out by giving the clues.. Thank you..

                      Anee

                      A Offline
                      A Offline
                      anumadhu
                      wrote on last edited by
                      #13

                      Thanks to all specially to JudyL_FL.. who tried to help me out in fixing my problem. And there is a good news, the problem got fixed.... :) . Actually i am not sure that whether i m right or not in my analysis. What i did to solve this: I just created System DSN now and everything was same. And it worked fine, without any problem. Before this i using User DSN. The cause of problem would be: What i guess is, since the windows service which i am developing is a system service so it is failing to open MySQL db through the User DSN. :~ Am i right? or is there anything else which is causing the problem... Thanks in advance.

                      Anee

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups