Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Trouble loading a driver

Trouble loading a driver

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++debuggingquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mattzimmerer
    wrote on last edited by
    #1

    I'm trying to write an exe to load my driver into the kernel, I'm getting an error ERROR_PATH_NOT_FOUND. I know my sys file is in the at this path, what am I missing?

    // Loader.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <windows.h>
    #include <stdio.h>

    bool LoadDriver();

    int _tmain(int argc, _TCHAR* argv[])
    {
    bool status;
    status = LoadDriver();

    if (status)
    	printf("Driver load Succeeded!\\n");
    else
    	printf("Driver load Failed!\\n");
    
    return 0;
    

    }

    bool LoadDriver()
    {
    wchar_t PATH[1024] = L"C:\\DLOADER\\DLOADER\\Debug\\target.sys";
    wchar_t DRIVERNAME[1024] = L"target.sys";
    printf("PATH: %ls\n", PATH);
    printf("DRIVERNAME: %ls\n", DRIVERNAME);
    SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if(!sh)
    return false;

    SC\_HANDLE rh = CreateService(sh,
    	DRIVERNAME,
    	DRIVERNAME,
    	SERVICE\_ALL\_ACCESS,
    	SERVICE\_KERNEL\_DRIVER,
    	SERVICE\_DEMAND\_START,
    	SERVICE\_ERROR\_NORMAL,
    	PATH,
    	NULL,
    	NULL,
    	NULL,
    	NULL,
    	NULL);
    
    if(!rh)
    {
    	printf("CreateService Failed(error%i)\\n",GetLastError());
    	if (GetLastError() == ERROR\_SERVICE\_EXISTS)
    	{
    		// service exists
    		rh = OpenService(sh,
    			DRIVERNAME,
    			SERVICE\_ALL\_ACCESS);
    		if(!rh)
    		{
    			printf("OpenService Failed(error%i)\\n",GetLastError());
    			CloseServiceHandle(sh);
    			return false;
    		}
    		else
    			printf("OpenService Success\\n");
    	}
    	else
    	{
    		CloseServiceHandle(sh);
    		return false;
    	}
    }
    // start the drivers
    if(rh)
    {
    	if(0 == StartService(rh, 0, NULL))
    	{
    		printf("StartService Failed(error%i)\\n",GetLastError());
    		if(ERROR\_SERVICE\_ALREADY\_RUNNING == GetLastError())
    		{
    			// no real problem
    		}
    		else
    		{
    			CloseServiceHandle(sh);
    			CloseServiceHandle(rh);
    			return false;
    		}
    	}
    	else
    		printf("StartService Success\\n");
    	CloseServiceHandle(sh);
    	CloseServiceHandle(rh);
    }
    return true;
    

    }

    Here is the output:

    PATH: C:\DLOADER\DLOADER\Debug\target.sys
    DRIVERNAME: target.sys
    CreateService Failed(error1073)
    OpenService Success
    StartService Failed(error3)
    Driver load Failed!

    Any help would be appreciated! Ohh and heres what those error codes are: ERROR_SERVICE_EXISTS = 1073 ERROR_PATH_NOT_FOUND = 3

    M 1 Reply Last reply
    0
    • M Mattzimmerer

      I'm trying to write an exe to load my driver into the kernel, I'm getting an error ERROR_PATH_NOT_FOUND. I know my sys file is in the at this path, what am I missing?

      // Loader.cpp : Defines the entry point for the console application.
      //

      #include "stdafx.h"
      #include <windows.h>
      #include <stdio.h>

      bool LoadDriver();

      int _tmain(int argc, _TCHAR* argv[])
      {
      bool status;
      status = LoadDriver();

      if (status)
      	printf("Driver load Succeeded!\\n");
      else
      	printf("Driver load Failed!\\n");
      
      return 0;
      

      }

      bool LoadDriver()
      {
      wchar_t PATH[1024] = L"C:\\DLOADER\\DLOADER\\Debug\\target.sys";
      wchar_t DRIVERNAME[1024] = L"target.sys";
      printf("PATH: %ls\n", PATH);
      printf("DRIVERNAME: %ls\n", DRIVERNAME);
      SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
      if(!sh)
      return false;

      SC\_HANDLE rh = CreateService(sh,
      	DRIVERNAME,
      	DRIVERNAME,
      	SERVICE\_ALL\_ACCESS,
      	SERVICE\_KERNEL\_DRIVER,
      	SERVICE\_DEMAND\_START,
      	SERVICE\_ERROR\_NORMAL,
      	PATH,
      	NULL,
      	NULL,
      	NULL,
      	NULL,
      	NULL);
      
      if(!rh)
      {
      	printf("CreateService Failed(error%i)\\n",GetLastError());
      	if (GetLastError() == ERROR\_SERVICE\_EXISTS)
      	{
      		// service exists
      		rh = OpenService(sh,
      			DRIVERNAME,
      			SERVICE\_ALL\_ACCESS);
      		if(!rh)
      		{
      			printf("OpenService Failed(error%i)\\n",GetLastError());
      			CloseServiceHandle(sh);
      			return false;
      		}
      		else
      			printf("OpenService Success\\n");
      	}
      	else
      	{
      		CloseServiceHandle(sh);
      		return false;
      	}
      }
      // start the drivers
      if(rh)
      {
      	if(0 == StartService(rh, 0, NULL))
      	{
      		printf("StartService Failed(error%i)\\n",GetLastError());
      		if(ERROR\_SERVICE\_ALREADY\_RUNNING == GetLastError())
      		{
      			// no real problem
      		}
      		else
      		{
      			CloseServiceHandle(sh);
      			CloseServiceHandle(rh);
      			return false;
      		}
      	}
      	else
      		printf("StartService Success\\n");
      	CloseServiceHandle(sh);
      	CloseServiceHandle(rh);
      }
      return true;
      

      }

      Here is the output:

      PATH: C:\DLOADER\DLOADER\Debug\target.sys
      DRIVERNAME: target.sys
      CreateService Failed(error1073)
      OpenService Success
      StartService Failed(error3)
      Driver load Failed!

      Any help would be appreciated! Ohh and heres what those error codes are: ERROR_SERVICE_EXISTS = 1073 ERROR_PATH_NOT_FOUND = 3

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      You need to follow the procedure below: 1) Open SC Manager, 2) Open Service, // You need to move this step here before creating service. 3) Create Service.

      Maxwell Chen

      M 1 Reply Last reply
      0
      • M Maxwell Chen

        You need to follow the procedure below: 1) Open SC Manager, 2) Open Service, // You need to move this step here before creating service. 3) Create Service.

        Maxwell Chen

        M Offline
        M Offline
        Mattzimmerer
        wrote on last edited by
        #3

        Awesome... 5 days after I started I was successful at creating a kernel level driver that I can send commands to from the user mode... wow now I can actually start programming :laugh: ... Thanks man, that ended up not being the route I took but I got a basic understanding of the SCM now! ;P Now if I can just read from physical memory correctly without blue-screening my system.. I'm guesstimating a good 10 days... gotta love free time... :laugh:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups