Start MS SQL Server Dynamically

Start MS SQL Server Dynamically

Use this code in your C++ executable to start MS SQL Server dynamically on aWindows NT machine. Link to w95scm.lib, which is provided with the SQL 7.0 CD.

 #include "wn95scm.h" //included with Sql7.0 CDBOOL  StartSqlServer(LPSTR szErrorMsg){     LPSTR       szService = "MSSQLServer";     DWORD       dwServiceState, dwErr;     BOOL        bControlSuccessful;     bControlSuccessful = TRUE;    // Query service state.     dwServiceState = SQLSCMGetLocalServiceState(szService, &dwErr);     while ((dwServiceState != 0) && (bControlSuccessful == TRUE))     {          if (dwServiceState == SERVICE_RUNNING)               break;          switch (dwServiceState)          {               case SERVICE_PAUSED:     // Paused, so continue.                    bControlSuccessful =  SQLSCMLocalServiceControl(szService,SQLSCMCmd_CONTINUE,&dwErr, 0, NULL);                    break;               case SERVICE_STOPPED:     // Stopped, start service.                    bControlSuccessful =  SQLSCMLocalServiceControl(szService,SQLSCMCmd_START, &dwErr, 0, NULL);                    break;               default:// Error if state is 0 otherwise state is changing.                    if (dwServiceState == 0)                    {    // Error. Return failure.                         sprintf(szErrorMsg, "Error %lu on attempt to determineservice state.", dwErr);                         return (FALSE);                    }          }          Sleep(5);          dwServiceState = SQLSCMGetLocalServiceState(szService, &dwErr);     }     if (bControlSuccessful)     {          sprintf(szErrorMsg, "Error %lu returned on attempt to change servicestate.", dwErr);     }     return (bControlSuccessful ? TRUE : FALSE);}

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist