前题,sql server 安装的是默认名字
BOOL StartMsSqlServer()
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
BOOL bSuccess = FALSE;
bSuccess = CreateProcess(
NULL,
"scm -Silent 1 -Action 1 -Service mssqlserver",
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si, //返回的结构中有进程的句柄
&pi);
if(bSuccess)
{
CloseHandle(pi.hThread);
//用进程句柄等待进程结束
if(WaitForSingleObject(pi.hProcess,INFINITE) == WAIT_FAILED)
{
bSuccess = FALSE;
}
CloseHandle(pi.hProcess);
}
return bSuccess;
}
static BOOL CloseMsSqlServer()
{
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
BOOL bSuccess = FALSE;
bSuccess = CreateProcess(
NULL,
"scm -Silent 1 -Action 6 -Service mssqlserver",
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si, //返回的结构中有进程的句柄
&pi);
if(bSuccess)
{
CloseHandle(pi.hThread);
//用进程句柄等待进程结束
if(WaitForSingleObject(pi.hProcess,INFINITE) == WAIT_FAILED)
{
bSuccess = FALSE;
}
CloseHandle(pi.hProcess);
}
return bSuccess;
}