示例#1
0
//
// Purpose: 
//   The service code
//
// Parameters:
//   dwArgc - Number of arguments in the lpszArgv array
//   lpszArgv - Array of strings. The first string is the name of
//     the service and subsequent strings are passed by the process
//     that called the StartService function to start the service.
// 
// Return value:
//   None
//
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
	// TO_DO: Declare and set any required variables.
	//   Be sure to periodically call ReportSvcStatus() with 
	//   SERVICE_START_PENDING. If initialization fails, call
	//   ReportSvcStatus with SERVICE_STOPPED.

	// Create an event. The control handler function, SvcCtrlHandler,
	// signals this event when it receives the stop control code.

	ghSvcStopEvent = CreateEvent(
		NULL,    // default security attributes
		TRUE,    // manual reset event
		FALSE,   // not signaled
		NULL);   // no name

	if ( ghSvcStopEvent == NULL)
	{
		ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
		return;
	}

	if ( !LoadSupportedBluetoothDevices() )
	{
		ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
		return;
	}

	GUID hidGuid;
	HidD_GetHidGuid(&hidGuid);

	DEV_BROADCAST_DEVICEINTERFACE dbvNotificationFilter;

	ZeroMemory( &dbvNotificationFilter, sizeof(dbvNotificationFilter) );
	dbvNotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
	dbvNotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	dbvNotificationFilter.dbcc_classguid = hidGuid;

	HDEVNOTIFY hDeviceNotification = RegisterDeviceNotification( 
		gSvcStatusHandle, &dbvNotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE );

	if ( hDeviceNotification == NULL)
	{
		ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
		return;
	}

	TryToSwitchAllDevices( TRUE );

	// Report running status when initialization is complete.
	ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );

	while(1)
	{
		// Check whether to stop the service.
		WaitForSingleObject(ghSvcStopEvent, INFINITE);
		break;
	}

	UnregisterDeviceNotification(hDeviceNotification);
	ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
}
示例#2
0
void SvcInit(DWORD dwArgc, LPTSTR* lpszArgv)
{
	SECURITY_ATTRIBUTES securityAttributes;
	if (!GetSecurityAttributes(securityAttributes))
	{
		ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	HANDLE pipe = CreateNamedPipeA(
		"\\\\.\\pipe\\TagServicePipe",
		PIPE_ACCESS_DUPLEX,
		PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
		PIPE_UNLIMITED_INSTANCES,
		4096,
		4096,
		50,
		&securityAttributes);

	if (pipe == INVALID_HANDLE_VALUE)
	{
		SvcReportWinFuncError(TEXT("CreateNamedPipeA"));

		FreeSecurityAttributes(securityAttributes);
		ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	ghSvcStopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);

	if (ghSvcStopEvent == nullptr)
	{
		SvcReportWinFuncError(TEXT("CreateEvent"));

		FreeSecurityAttributes(securityAttributes);
		CloseHandle(pipe);
		ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	SvcReportInfo(TEXT("Service running"));
	ReportSvcStatus(SERVICE_RUNNING, NO_ERROR, 0);

	while (WaitForSingleObject(ghSvcStopEvent, 0) == WAIT_TIMEOUT)
	{
		if (ConnectNamedPipe(pipe, nullptr))
		{
			//SvcReportInfo(TEXT("Connected pipe"));
			char buffer[4096];
			DWORD bytesRead;
			if (ReadFile(pipe, buffer, 4096, &bytesRead, nullptr))
			{
				for (unsigned int i = 0; i < bytesRead / 2; ++i)
				{
					char temp = buffer[i];
					buffer[i] = buffer[bytesRead - i - 1];
					buffer[bytesRead - i - 1] = temp;
				}

				DWORD bytesWritten;
				WriteFile(pipe, buffer, bytesRead, &bytesWritten, nullptr);
			}

			FlushFileBuffers(pipe);
			DisconnectNamedPipe(pipe);
		}
	}

	FreeSecurityAttributes(securityAttributes);
	CloseHandle(pipe);
	ReportSvcStatus(SERVICE_STOPPED, NO_ERROR, 0);
}