前題,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;
}