Device drivers : Facing issues while running windows service

  • Thread starter Thread starter hemanth kumar velooru
  • Start date Start date
H

hemanth kumar velooru

Guest
Hello team,

I am new to device drivers development, In order to establish communication between user mode application and genericusbfn.sys , we are using windows service below i gave code which we are using .we are able to install ,start ,stop and delete the service. But after strating the service also control is not entering into Servicemain().Please help us what modifications i need to do.

here i gave symoblic link of keyboard as GUID

1573765.png


/*Module Name:

ServiceWin32API.cpp

Abstract:

Implements functions to start/run/stop the service.

Environment:

User mode

--*/

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;
ofstream svc;
//
// Settings of the service
//
//#define SERVICE_NAME L"OsrUsdUmUserSvc"
#define SERVICE_NAME TEXT("UsdUmUserSvc")
#define SERVICE_FLAGS_RUNNING 0x1
#pragma comment(lib, "advapi32.lib")
//
// Service context
//
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE SvcStatusHandle = NULL;
volatile LONG SvcControlFlags = 0;
HANDLE SvcStoppedEvent = NULL;
HANDLE SvcStopRequestEvent = NULL;
HANDLE SvcStopWaitObject = NULL;
HMODULE H_handle;
//
// Device interface context
//
HCMNOTIFICATION InterfaceNotificationHandle = NULL;
SRWLOCK DeviceListLock = SRWLOCK_INIT;
DEVICE_LIST_ENTRY DeviceList;

VOID SvcInstall()
{
svc.open("service.txt");
svc << "Inside svc install";
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];

if (!GetModuleFileName(H_handle, szPath, MAX_PATH))
{
printf("Cannot install service (%d)\n", GetLastError());
return;
}

// Get a handle to the SCM database.

schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights

if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}

// Create the service

schService = CreateService(
schSCManager, // SCM database
SERVICE_NAME, // name of service
SERVICE_NAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password

if (schService == NULL)
{
printf("CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");

CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
svc.close();
}


//
// Debugging support during development time
//
//#define DEBUG_SUPPORT

/*++

Routine Description:

Entry point for the service.

Arguments:

Argc - The number of command line arguments

Argv - The array of command line arguments

Return Value:

VOID

--*/
INT
wmain(
INT Argc,
WCHAR *Argv[]
)
{
svc.open("service.txt");
svc << "Inside main ";
if (lstrcmpi(Argv[1], TEXT("install")) == 0)
{
SvcInstall();
}

// UNREFERENCED_PARAMETER(Argc);
// UNREFERENCED_PARAMETER(Argv);
SERVICE_TABLE_ENTRY serviceTable[] =
{
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{ NULL, NULL}
};
return StartServiceCtrlDispatcher(serviceTable);
svc.close();
}

VOID WINAPI ServiceMain(
DWORD Argc,
PWSTR *Argv
)
{
svc.open("service.txt");
svc << "Inside service main";
DWORD Err = ERROR_SUCCESS;

//UNREFERENCED_PARAMETER(Argc);
//UNREFERENCED_PARAMETER(Argv);
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-MAIN");
//
// Initialize global variables
//
InitializeDeviceListHead(&DeviceList);

#ifdef DEBUG_SUPPORT

while (!IsDebuggerPresent()) {
Sleep(1000);
}

__debugbreak();

#endif

SvcStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME,
ServiceCtrlHandler);

if (SvcStatusHandle == NULL)
{
Err = GetLastError();
goto cleanup;
}

UpdateServiceStatus(SvcStatusHandle,
SERVICE_START_PENDING,
ERROR_SUCCESS);

//
// Setup device interface context
//
Err = SetupDeviceInterfaceContext();

UpdateServiceStatus(SvcStatusHandle,
SERVICE_START_PENDING,
ERROR_SUCCESS);

//
// Initialize device control
//
OsrFx2InitializeDevice();

//
// Register callback function for stop event
//
SvcStopRequestEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (SvcStopRequestEvent == NULL)
{
Err = GetLastError();
goto cleanup;
}

if (!RegisterWaitForSingleObject(&SvcStopWaitObject,
SvcStopRequestEvent,
ServiceStopCallback,
NULL,
INFINITE,
WT_EXECUTEONLYONCE | WT_EXECUTEINPERSISTENTTHREAD))
{
Err = GetLastError();
goto cleanup;
}

//
// Create stopped event for the running worker thread
//
SvcStoppedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (SvcStoppedEvent == NULL)
{
Err = GetLastError();
goto cleanup;
}

UpdateServiceStatus(SvcStatusHandle,
SERVICE_START_PENDING,
ERROR_SUCCESS);

//
// Queue the main service function for execution in a worker thread.
//
QueueUserWorkItem(&ServiceRunningWorkerThread,
NULL,
WT_EXECUTELONGFUNCTION);

UpdateServiceStatus(SvcStatusHandle,
SERVICE_RUNNING,
ERROR_SUCCESS);
svc.close();
cleanup:

if (Err != ERROR_SUCCESS)
{
ServiceStop(Err);
svc.close();
}
}

VOID
WINAPI
ServiceCtrlHandler(
DWORD Ctrl
)
{
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
switch (Ctrl)
{
case SERVICE_CONTROL_Snbsp; //
// Set service stop event
//
UpdateServiceStatus(SvcStatusHandle, SERVICE_STOP_PENDING, ERROR_SUCCESS);
SetEvent(SvcStopRequestEvent);
break;

default:
break;
}
}

#define SERVICE_WAIT_HINT_TIME 30000 // 30 seconds

BOOL
UpdateServiceStatus(
__in_opt SERVICE_STATUS_HANDLE hSvcHandle,
__in DWORD dwCurrentState,
__in DWORD dwWin32ExitCode
)
{
SERVICE_STATUS SvcStatus;

static DWORD dwCheckPoint = 1;

SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
SvcStatus.dwCurrentState = dwCurrentState;
SvcStatus.dwWin32ExitCode = dwWin32ExitCode;
SvcStatus.dwServiceSpecificExitCode = ERROR_SUCCESS;
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
if (dwCurrentState == SERVICE_START_PENDING)
{
SvcStatus.dwControlsAccepted = 0;
}
else
{
SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
}

if ((dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED))
{
SvcStatus.dwCheckPoint = 0;
SvcStatus.dwWaitHint = 0;
}
else
{
SvcStatus.dwCheckPoint = dwCheckPoint++;
SvcStatus.dwWaitHint = SERVICE_WAIT_HINT_TIME;
}

return SetServiceStatus(hSvcHandle, &SvcStatus);
}

DWORD
WINAPI
ServiceRunningWorkerThread(
_In_ PVOID lpThreadParameter
)
{
PDEVICE_CONTEXT DeviceContext = NULL;
PDEVICE_LIST_ENTRY Link = NULL;

UNREFERENCED_PARAMETER(lpThreadParameter);
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
InterlockedOr(&SvcControlFlags, SERVICE_FLAGS_RUNNING);

//
// Periodically check if the service is stopping.
//
while ((InterlockedOr(&SvcControlFlags, 0) & SERVICE_FLAGS_RUNNING) != 0)
{
AcquireSRWLockShared(&DeviceListLock);

for (Link = DeviceList.Flink; Link != &DeviceList; Link = Link->Flink)
{
DeviceContext = CONTAINING_DEVICE_RECORD(Link);

OsrFx2ControlDevice(DeviceContext);
}

ReleaseSRWLockShared(&DeviceListLock);

Sleep(2000); // Simulate some lengthy operations.
}

//
// Signal the stopped event.
//
SetEvent(SvcStoppedEvent);

return 0;
}

VOID
CALLBACK
ServiceStopCallback(
_In_ PVOID lpParameter,
_In_ BOOLEAN TimerOrWaitFired
)
{
UNREFERENCED_PARAMETER(TimerOrWaitFired);
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
//
// Since wait object can not be unregistered in callback function, queue
// another thread
//
QueueUserWorkItem(ServiceStopWorkerThread,
lpParameter,
WT_EXECUTEDEFAULT);
}

DWORD
WINAPI
ServiceStopWorkerThread(
_In_ PVOID lpThreadParameter
)
{
UNREFERENCED_PARAMETER(lpThreadParameter);
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
ServiceStop(ERROR_SUCCESS);

return 0;
}

VOID
ServiceStop(
_In_ DWORD ExitCode
)
{
printf("inside smarc_IOCTL_GENERICUSBFN_CONTROL_STATUS_HANDSHAKE_OUT-1");
if (SvcStatusHandle == NULL)
{
return;
}

UpdateServiceStatus(SvcStatusHandle, SERVICE_STOP_PENDING, ExitCode);

//
// Notify the working thread to stop
//
if ((InterlockedOr(&SvcControlFlags, 0) & SERVICE_FLAGS_RUNNING) != 0)
{
InterlockedAnd(&SvcControlFlags, ~SERVICE_FLAGS_RUNNING);
WaitForSingleObject(SvcStoppedEvent, INFINITE);
}

//
// Clean up device context after the worker thread has finished.
//
CleanupDeviceInterfaceContext();

//
// cleanup work
//
if (SvcStopWaitObject != NULL)
{
UnregisterWait(SvcStopWaitObject);
}

if (SvcStopRequestEvent != NULL)
{
CloseHandle(SvcStopRequestEvent);
}

if (SvcStoppedEvent != NULL)
{
CloseHandle(SvcStoppedEvent);
}

UpdateServiceStatus(SvcStatusHandle, SERVICE_STOPPED, ExitCode);
}

Continue reading...
 
Back
Top