Device Driver not calling Xxx_Init

2019-08-12 05:41发布

问题:

I am just starting out with driver development, and am trying to initialize a device driver through the operating system on start up. The driver is for Windows Embedded CE 6.0.

I have been attempting to have my device send a message through a serial port to my PC whenever it gets initialized.

DWORD MYD_Init(LPCTSTR pContext, LPCVOID lpvBusContext)
{
    DWORD dwResult = 1;

    RETAILMSG(TRUE, (TEXT("MyDriver: Initializing!\n")));
    DEBUGMSG(TRUE, (TEXT("MyDriver: Initializing!\n")));

    return dwResult;
} //end MYD_Init

The DLLEntry function is gets called:

BOOL DllEntry(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) 
{
    switch (dwReason) {

        case DLL_PROCESS_ATTACH:
            RETAILMSG(TRUE, (TEXT("MyDriver - Process Attached\n")));
            DEBUGMSG(TRUE, (TEXT("MyDriver - Process Attached\n")));
            break;

        case DLL_PROCESS_DETACH: 
            RETAILMSG(TRUE, (TEXT("MyDriver - Process Detached\n")));
            DEBUGMSG(TRUE, (TEXT("MyDriver - Process Detached\n")));
            break;

        .......

        default:
            break;
    } //end Switch

    return TRUE;

} //end DllEntry

And here are a few lines from the serial port log:

FMD_Init: Blocks reserved for the bootloader/run-time image: 3124
I2C Driver: Intialization Started
MyDriver - Process AttachedMyDriver - Process DetachedTurn on clocks for Device block (in OTG_CTRL)
2589 CUSBFN::IsConfigurationSupportable
2593 CUSBFN::IsEndpointSupportable
2595 CUSBFN::IsEndpointSupportable

Line three shows that the driver gets loaded (I think), but MYD_Init is not called afterward. I'm not sure what I am missing. Maybe a missing reference from another file. My .def file includes the MYD_Init under EXPORTS. The .reg key is within the BuiltIn path: [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\MyDriver]. Not sure if there are any others.

I guess my real question is what are possible things I am missing/forgetting to get the MYD_Init function to get called?

Thanks for any help you guys can provide to help me get this work!

Edit:

I have realized that the CreateFile function is never called and feel this is the reason for my problem. What are some steps that I could take to figure out where this function should be placed so that MYD_Init is not called before DllEntry?

Or maybe the ActivateDevice/ActiveDeviceEx functions? Does the OS automatically call these for the drivers listed under [HKEY_LOCAL_MACHINE\Drivers\BuiltIn], or do they have to be called manually?

Thanks again for any insight that can be provided!

Edit 2:

Here are the registry settings easily readable:

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\MyDriver]
    "Prefix"="MYD"
    "Dll"="myDriver.dll"
    "Index"=dword:1
    "Flags"=dword:0

Edit 3:

This is the output after trying to open the driver through an application:

USBFN: dwDeviceStatus: 0x11, m_fAttached: Attached
USBFN: dwDeviceStatus: 0x1, m_fAttached: Attached
MyDriver - Process Attached 
MyDriver - Process Detached 
COM_Open
COM_Open
IOCTL_SERIAL_SET_COMMTIMEOUTS (50,1,50,5,500)
IOCTL_SERIAL_SET_COMMTIMEOUTS (50,1,50,5,500)
IOCTL_SERIAL_SET_COMMTIMEOUTS (-1,0,0,2,500)
Testing Driver 
MyDriver - Process Attached 
MyDriver - Process Detached 
Driver Tested

Line 3 and 4 is the Load attempt during system boot. The last four lines are output when the application to load the driver is run. "Testing Driver" and "Driver Tested" were placed at the beginning and end of the application.

Edit 4: Dumpbin Output

Dump of file myDriver.dll

File Type: DLL

Section contains the following exports for myDriver.dll

    00000000 characteristics
    501012DA time date stamp Wed Jul 25 10:38:02 2012
        0.00 version
           1 ordinal base
           9 number of functions
           9 number of names

    ordinal hint RVA      name

          1    0 000013D4 DllEntry = DllEntry
          2    1 00001348 MYD_Close = MYD_Close
          3    2 0000130C MYD_Deinit = MYD_Deinit
          4    3 000013B8 MYD_IOControl = MYD_IOControl
          5    4 000012B8 MYD_Init = MYD_Init
          6    5 00001328 MYD_Open = MYD_Open
          7    6 00001364 MYD_Read = MYD_Read
          8    7 0000139C MYD_Seek = MYD_Seek
          9    8 00001380 MYD_Write = MYD_Write

Summary

        1000 .data
        1000 .pdata
        1000 .reloc
        1000 .text

回答1:

Xxx_Init will be called when the driver is loaded. It might be loaded by calling ActivateDevice manually, or by having it automatically loaded by DeviceManager by setting the proper registry settings in [HKLM\Drivers\BuiltIn] (show us what keys you are using).

Xxx_Init is guaranteed to be called after DllEntry because DllEntry gets called when the DLL is loaded by the OS, which has to occur before Device Manager can load it.

CreateFile calls the Xxx_Open method in the driver, which is after all of these actions occur.

So the order of events is:

  1. OS loads the DLL, calling DllEntry
  2. Device Manager loads the driver (either manually or automatic), calling Xxx_Init
  3. App opens the driver by calling CreateFile, which makes Device Manager call Xxx_Open

There are possibly some power management pieces that can fall at 2.5 on the list, but they are optional and likely unrelated to the issue you're seeing.

Typpically I'll use an explicit call to ActivateDevice/DeactivateDevice from a test app when first testing out driver bring-up. It lets you replace the driver DLL without having to reboot the system every time.

So the questions to you are:

  1. If you manually call ActivateDevice from an app, what happens?
  2. What are the registry keys you're currently using to try to get device manager to load the driver?