⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 control.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
int CTRL_stopService( const char *pServiceName )
{
int iRunning;
	assert( pServiceName );

	SC_HANDLE scH = Internal_openServiceManager();
	if ( !scH )
		{ return -1; };

	HANDLE schService = OpenService(
		scH, pServiceName, SERVICE_STOP  | SERVICE_QUERY_STATUS );

	Internal_closeServiceManager( scH );
			
	if ( !schService )
		{ return -1; };

	/*
	** Stop service
	*/
	SERVICE_STATUS ssStatus;
	if ( !ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
		{ goto stop_service_fail; };

	/*
	** Track service while it stops
	*/
//	if ( Internal_trackServiceState( schService )!=0 )
//		{ goto stop_service_fail; };
iRunning = 1;
while( iRunning )
{
switch( Internal_trackServiceState( schService ) )
	{
	case 0: /* stopped */ 
			iRunning = 0;
			break;
	case 1: /* still running */				
			Sleep( 1000 );
			break;
	default: /* error */
			goto stop_service_fail;
	};
};

	/*
	** Ok!
	*/
	CloseServiceHandle( schService );
	return 1;

stop_service_fail:
	CloseServiceHandle( schService );
	return -1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_isPi3Running( const char *pPi3NamedSemaphore )
{
	assert( pPi3NamedSemaphore );

	/*
	** Try to open the semaphore
	*/
	HANDLE hSema = ::OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, 
		pPi3NamedSemaphore );

	if ( !hSema )
		{
		/*
		** Semaphore does not exist, process is not running
		*/
		return 0;
		}

	CloseHandle( hSema );

	/*
	** Process is running
	*/
	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_startPi3( const char *pCommandLine,
		const char * /* pPi3NamedSemaphore */ )
{
	assert( pCommandLine );

	STARTUPINFO tStartUp;
	memset(&tStartUp, 0, sizeof(tStartUp));
	tStartUp.cb=sizeof(tStartUp);

	PROCESS_INFORMATION tProcessInfo;

	/* --- start the program --- */
	BOOL bProcessCreated = CreateProcess(
		NULL,				/* address of module name */
		(char *)pCommandLine,	/* address of command line */
		NULL,				/* address of process security attributes */
		NULL,				/* address of thread security attributes */
		FALSE,				/* new process inherits handles */
		0,					/* creation flags */
		NULL,				/* address of new environment block */
		NULL,				/* address of current directory name */
		&tStartUp,			/* address of STARTUPINFO */
		&tProcessInfo		/* address of PROCESS_INFORMATION */
		);

	if ( bProcessCreated )
		{
		::CloseHandle( tProcessInfo.hThread );
		::CloseHandle( tProcessInfo.hProcess );
		return 1;
		};

	return -1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_stopPi3( const char *pPi3NamedSemaphore )
{
	assert( pPi3NamedSemaphore );

	/*
	** Try to open the semaphore
	*/
	HANDLE hSema = ::OpenSemaphore( SEMAPHORE_ALL_ACCESS, FALSE, 
		pPi3NamedSemaphore );

	if ( !hSema )
		{
		/*
		** Semaphore does not exist, process is not running
		*/
		return -1;
		};

	/*
	** Release the semaphore
	*/
	BOOL bRet = ::ReleaseSemaphore( hSema, 1, NULL );

	CloseHandle( hSema );

	return bRet ? 1 : -1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void CTRL_semaphoreNameFromConfigPath( const char *pConfig,
	PIString &sSemaphore )
{
	/*
	** Semaphore name for application is the full
	** path to the configuration file in upper case
	*/
	PIString sSemaphoreName;
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];
	::GetCurrentDirectory( BUF_SIZE, szBuf );
	RelativeToAbsolutePath( szBuf, pConfig, sSemaphoreName );
	sSemaphoreName.ConvertToUpperCase();
	/*
	** Replace '\\' with '/'. Semaphore names cannot have '\\' in
	** them, see CreateSemaphore(), though this does not seem to be
	** a problem on Windows 95.
	*/
	SwapSubStrings sSwap( sSemaphoreName, "\\", "/" );	
	sSemaphore = sSwap.GetString();
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 on success, 0 on failure.
\*____________________________________________________________________________*/
int CTRL_openURL( HWND hWnd, const char *pURL )
{
	assert( pURL );

	if ( (int)::ShellExecute( hWnd, NULL, pURL, NULL,
		NULL, SW_SHOWNORMAL)<=32 )
		{ return 0; };

	return 1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_isStartupInstalled( const char *pSemaphoreName )
{
	assert( pSemaphoreName);

	HKEY hKey;

	LONG lRet = ::RegQueryValueEx(
		HKEY_LOCAL_MACHINE,				// handle of key to query 
		REGSTR_PATH_RUNSERVICES,		// address of name of value to query 
		0,								// reserved 
		0,								// address of buffer for value type 
		0,								// address of data buffer 
		0								// address of data buffer size 
		);

#if 0
	switch( lRet )
		{
		case ERROR_SUCCESS: break;
		case ERROR_FILE_NOT_FOUND: return 0;
		default:
			return -1;
		};

	if ( ::RegOpenKeyEx(
		HKEY_LOCAL_MACHINE,
		REGSTR_PATH_RUNSERVICES,
		0, KEY_ALL_ACCESS,
		&hKey )!=ERROR_SUCCESS )
		{ return -1; };
#endif

	lRet = ::RegOpenKeyEx(
		HKEY_LOCAL_MACHINE,
		REGSTR_PATH_RUNSERVICES,
		0, KEY_ALL_ACCESS,
		&hKey );

	switch( lRet )	
		{
		case ERROR_SUCCESS: break;
		case ERROR_FILE_NOT_FOUND: return 0;
		default:
			return -1;
		};

	int iRet = (::RegQueryValueEx( hKey, pSemaphoreName, 0, 0, 0, 0 )==
		ERROR_SUCCESS ) ? 1 : 0;

	::RegCloseKey( hKey );
	
	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_installStartup( const char *pSemaphoreName, const char *pCmdLine )
{
	assert( pSemaphoreName && pCmdLine );

	HKEY hKey = 0;
	DWORD dwDisp;

	if ( ::RegCreateKeyEx(
		HKEY_LOCAL_MACHINE,				// handle of an open key 
		REGSTR_PATH_RUNSERVICES,		// address of subkey name 
		0,								// reserved 
		"",								// address of class string 
		0,								// special options flag 
		KEY_ALL_ACCESS,					// desired security access 
		NULL,							// address of key security structure 
		&hKey,							// address of buffer for opened handle  
		&dwDisp							// address of disposition value buffer
		)!=ERROR_SUCCESS )
		{ goto an_error; };
	
	if ( ::RegSetValueEx(
		hKey,							// handle of key to set value for  
		pSemaphoreName,					// address of value to set 
		0,								// reserved
		REG_SZ,							// flag for value type 
		(const unsigned char *)pCmdLine,// address of value data
		1								// size of value data 
		)!=ERROR_SUCCESS )
		{ goto an_error; };	

	::RegCloseKey( hKey );

	return 1;

an_error:
	if ( hKey ) { ::RegCloseKey( hKey ); };

	return -1;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
int CTRL_unInstallStartup( const char *pSemaphoreName )
{
	assert( pSemaphoreName );

	HKEY hKey;

	if ( ::RegOpenKeyEx(
		HKEY_LOCAL_MACHINE,
		REGSTR_PATH_RUNSERVICES,
		0, KEY_ALL_ACCESS,
		&hKey )!=ERROR_SUCCESS )
		{ return -1; };

	int iRet = (::RegDeleteValue(hKey, pSemaphoreName )==ERROR_SUCCESS ) ?
		1 : -1;

	::RegCloseKey( hKey );

	return iRet;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -