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

📄 pi3srv32.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	remove( PI3_SERVICE_WARNING_FILE );
	PILog_writeDiagnostics( pDB, PILOG_WARNING, PI3_SERVICE_WARNING_FILE );

	/* --- notify service is pending, checkpoint 3 --- */
	ssStatus.dwControlsAccepted = 0;
	ssStatus.dwCurrentState = SERVICE_START_PENDING;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = dwCheck++;
	ssStatus.dwWaitHint = DEFAULT_WAIT_TIME;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); goto CLEANUP; };

	/* --- start another thread to run the logic of the program --- */
	pThread = PIThread_new( 0, 0 );
	if ( !pThread )
		{ StopService( "Error creating service thread" ); goto CLEANUP; };

	if ( PIThread_begin( pThread, Pi3Fn, 0, PITHREAD_PRIORITY_MED, 0 ))
		{ StopService( "Error begin service thread" ); goto CLEANUP; };
	
	/* --- notify service is running --- */
	ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
	ssStatus.dwCurrentState = SERVICE_RUNNING;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = 0;
	ssStatus.dwWaitHint = 0;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); goto CLEANUP; };

	/* --- wait for completion --- */
	WaitForSingleObject( hTerminateEvent, INFINITE );
	
	goto CLEANUP;

DO_ERROR:
	StopService( (char *)(pErr ? pErr : "") );

CLEANUP:
	if ( pThread )
		{ PIThread_delete( pThread ); };
	
	if ( ___pObject )
		{ PIObject_delete( ___pObject, 0, 0 ); };

	if ( hTerminateEvent )
		{ CloseHandle( hTerminateEvent ); };
	
	if ( sshStatusHandle )
		{
		ssStatus.dwControlsAccepted = 0;
		ssStatus.dwCurrentState = SERVICE_STOPPED;
		ssStatus.dwWin32ExitCode = GetLastError();
		ssStatus.dwCheckPoint = 0;
		ssStatus.dwWaitHint = 0;
		if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
			{ StopService( "SetServiceStatus" ); };
		};

	iRet = (int)ssStatus.dwWin32ExitCode;
	return;
}


/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static HWND __hWndDlgOK = 0;
static HWND __hWndDlgPasswd = 0;
struct passphrase {
	char **buf;
	int bufsize;
} * __passphrase;

static BOOL CALLBACK fnPassDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
	LPARAM lParam)
{
	switch (uMsg)
		{
		case WM_INITDIALOG:
			__hWndDlgOK = GetDlgItem( hDlg, IDOK );
			__hWndDlgPasswd = GetDlgItem( hDlg, IDC_PASSWORD );
			__passphrase = (passphrase *)lParam;
			assert( __hWndDlgOK && __hWndDlgPasswd );
			SendMessage( __hWndDlgPasswd, EM_SETLIMITTEXT, __passphrase->bufsize, 0 );
			EnableWindow( __hWndDlgOK, FALSE );
			break;

		case WM_COMMAND:
			switch (GET_WM_COMMAND_ID(wParam, lParam))
				{
				case IDOK:
					{
					/*
					** Get content of edit field
					*/
					SendMessage( __hWndDlgPasswd, WM_GETTEXT, __passphrase->bufsize,
						(LPARAM)*(__passphrase->buf) );
					EndDialog( hDlg, 1 );
					break;
					}

				case IDCANCEL:
					EndDialog( hDlg, 0 );
					break;

				case IDC_PASSWORD:
					if ( GET_WM_COMMAND_CMD(wParam, lParam)==EN_CHANGE )
						{
						if ( SendMessage( GET_WM_COMMAND_HWND(wParam, lParam ), EM_LINELENGTH,
							0, 0 )>0 )
							{ EnableWindow( __hWndDlgOK, TRUE ); }
						else
							{ EnableWindow( __hWndDlgOK, FALSE ); }
						}
					else
						return (FALSE);
					break;

				default:;
				};

		default:
			return (FALSE);
		};

	return (TRUE);
}

int fnPassPhraseDlg(char *buf, int bufsize)
{
	passphrase pphrase;
	pphrase.buf = &buf;
	pphrase.bufsize = bufsize;
	if (DialogBoxParam(
		::GetModuleHandle( NULL ),
		MAKEINTRESOURCE(IDD_PASSPHRASE),
		NULL,
		(DLGPROC)fnPassDialogProc,
		(LPARAM)&pphrase
	) != -1 )
		{
		/* --- notify service is pending, increment checkpoint --- */
		ssStatus.dwControlsAccepted = 0;
		ssStatus.dwCurrentState = SERVICE_START_PENDING;
		ssStatus.dwWin32ExitCode = NO_ERROR;
		ssStatus.dwCheckPoint = dwCheck++;
		ssStatus.dwWaitHint = DEFAULT_WAIT_TIME_INTERACTIVE;
		if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
			{ StopService( "SetServiceStatus" ); };
		return strlen(buf);
		}
	else
		{ StopService( "DialogBoxParam" ); };

		return -1;
}

typedef void REGPWDDLG(void *pPasswdDlg);

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void RegisterPassPhraseDialog()
{
	HINSTANCE hSSL = LoadLibrary("SSL.dll");
	if (hSSL)
	{
	REGPWDDLG *regPwdDlg = (REGPWDDLG *)GetProcAddress(hSSL, "SSL_register_PassphraseDlg");
	if (regPwdDlg!=NULL) regPwdDlg(&fnPassPhraseDlg);
	}

}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
	/* --- save commandline parameters --- */
	dwTheArgc = dwArgc;
	lpszTheArgv = lpszArgv;

#if 0
just experimenting....
	/* --- set the error mode so as not to create messageboxes --- */
	::SetErrorMode( SEM_FAILCRITICALERRORS );
#endif

	/* ---
	set current directory to be directory where binary is.
	This is happening at this point because several debugging files will
	be created in this directory.
	--- */
	const char *pErr = 0;
	int iLen = 0;
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];
	if ( ::GetModuleFileName( ::GetModuleHandle( NULL ), szBuf, BUF_SIZE )<=0 )
		{
		pErr = "Could not get executable path";
		goto DO_ERROR;
		};
	szBuf[BUF_SIZE] = '\0';
	for ( iLen = strlen( szBuf )-1; iLen>=0; iLen-- )
		{
		if ( szBuf[iLen]=='/' || szBuf[iLen]=='\\' || szBuf[iLen]==':' )
			{ break; };
		};
	iLen++; szBuf[iLen]='\0';
	if ( !::SetCurrentDirectory( szBuf ) )
		{
		pErr = "Could not set current directory";
		goto DO_ERROR;
		};

	if ( !dwArgc || !lpszArgv )
		{ goto DO_ERROR; };

	RegisterPassPhraseDialog();

	if ( PIProgram_enter( lpszArgv[0], PIPROGRAM_VERSION_1_0, App ) )
		{ return; };

DO_ERROR:
	/*
	** NOTE:
	** possible do something with pErr;
	*/
	return;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
void ServiceControlHandler( DWORD dwControlCode )
{
	DWORD dwState = SERVICE_RUNNING;

	switch( dwControlCode )
		{
		case SERVICE_CONTROL_STOP:
			/* --- notify service is stopping --- */
			ssStatus.dwControlsAccepted = 0;
			ssStatus.dwCurrentState = SERVICE_STOP_PENDING;
			ssStatus.dwWin32ExitCode = NO_ERROR;
			ssStatus.dwCheckPoint = 1;
			ssStatus.dwWaitHint = DEFAULT_WAIT_TIME;
			if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
				{ StopService( "SetServiceStatus" ); };
			SetEvent( hTerminateEvent );
			return;

		case SERVICE_CONTROL_PAUSE:
		case SERVICE_CONTROL_CONTINUE:
		case SERVICE_CONTROL_INTERROGATE:
		default:
			break;
		};

	ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
	ssStatus.dwCurrentState = dwState;
	ssStatus.dwWin32ExitCode = NO_ERROR;
	ssStatus.dwCheckPoint = 0;
	ssStatus.dwWaitHint = 0;
	if ( !SetServiceStatus( sshStatusHandle, &ssStatus ) )
		{ StopService( "SetServiceStatus" ); };
}

⌨️ 快捷键说明

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