【问题标题】:How to write an application to control a driver which is pnp and in kmdf?如何编写应用程序来控制 pnp 和 kmdf 的驱动程序?
【发布时间】:2021-08-20 09:23:15
【问题描述】:

所以我会详细说明,以便我们容易理解

我必须为 pcie 卡制作一个驱动程序,我已经有了我在 kmdf 中编写的驱动程序,现在我正在使用这个驱动程序,不幸的是我发现自己卡住了,我必须编写一个应用程序(例如调用我在 IoDeviceControl 的 switch case 中定义的 METHOD_IN_DIRECT 函数)

因此,我尝试从 github 上的示例开始并对其进行修改以使其正常工作……但显然,由于此示例适用于 NONpnp 驱动程序,因此不适用于我的 pnp 驱动程序。

所以我寻找与 pnp 驱动程序一起工作的应用程序示例以查看模型/形状,但我找不到有关实现这个著名应用程序的教程/站点/示例,这是唯一说话的站点之一关于它的意思是:

“设置界面指南,以便应用程序可以找到设备并与之对话。”

现在我的问题是:

“如何编写应用程序来控制 PNP 驱动程序”

“test.c”中的主要内容:

int __cdecl
main(
    _In_              ULONG argc,
    _In_reads_(argc) PCHAR argv[]
    )
{
    HANDLE   hDevice;
    DWORD    errNum = 0;
    CHAR     driverLocation[MAX_PATH];
    BOOL     ok;
    LONG     error;
//    ULONG bytesReturned;

    printf("main start. \n");


    // 
    //open the device
    printf("createFile. \n");
    
    hDevice = CreateFileA(DRIVER_NAME,
                        GENERIC_READ | GENERIC_WRITE,
                        0,
                        NULL,
                        CREATE_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);


    if (hDevice == INVALID_HANDLE_VALUE){...}
    
    printf("press enter \n");

    int c = getchar();

    printf("reception d'un charactere . \n");

    if (c) {
        
        printf("ioctl go \n");

        DoIoctls(hDevice);

        printf("ioctl end \n");

        //
        // Close the handle to the device before unloading the driver.
        //
        CloseHandle(hDevice);

        //
        // Unload the driver.  Ignore any errors.
        //
        ManageDriver(DRIVER_NAME, driverLocation, DRIVER_FUNC_REMOVE);
    }
    c = getchar();

    return;


}

这里是“test.c”的主要部分,它是 nonpnp 的基础,但我修改后说我不知道​​如何在我的应用程序中嵌入 GUID 的使用(我想这是因为它不起作用)。

DoIoctl 函数:

VOID
DoIoctls(
    HANDLE hDevice
)
{
    char OutputBuffer[100];
    char InputBuffer[200];
    BOOL bRc;
    ULONG bytesReturned;


    //
    // Printing Input & Output buffer pointers and size
    //

    printf("\nInputBuffer Pointer = %p, BufLength = %Id\n", InputBuffer,sizeof(InputBuffer));
        
    printf("OutputBuffer Pointer = %p BufLength = %Id\n", OutputBuffer,sizeof(OutputBuffer));
        

    //
    // Performing METHOD_IN_DIRECT
    //

    printf("\nCalling DeviceIoControl METHOD_IN_DIRECT\n");

    if (FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer),"this String is from User Application; using METHOD_IN_DIRECT"))) 
    {
        return;
    }

    if (FAILED(StringCchCopy(OutputBuffer, sizeof(OutputBuffer),"This String is from User Application in OutBuffer; using METHOD_IN_DIRECT"))) 
    {
        return;
    }

    bRc = DeviceIoControl(hDevice,
                        (DWORD)Spw_PCIe_IOCTL_IN_BUFFERED,
                        InputBuffer,
                        (DWORD)strlen(InputBuffer) + 1,
                        OutputBuffer,
                        sizeof(OutputBuffer),
                        &bytesReturned,
                        NULL
                        );

    if (!bRc)
    {
        printf("Error in DeviceIoControl : %d \n", GetLastError());
        return;
    }

    printf("    Number of bytes transfered from OutBuffer: %d\n",bytesReturned);    


    //
    // Performing METHOD_OUT_DIRECT
    //

    printf("\nCalling DeviceIoControl METHOD_OUT_DIRECT\n");
    if (FAILED(StringCchCopy(InputBuffer, sizeof(InputBuffer), "this String is from User Application; using METHOD_OUT_DIRECT"))) {
        return;
    }

    memset(OutputBuffer, 0, sizeof(OutputBuffer));

    bRc = DeviceIoControl(hDevice,
                        (DWORD)Spw_PCIe_IOCTL_OUT_BUFFERED,
                        InputBuffer,
                        (DWORD)strlen(InputBuffer) + 1,
                        OutputBuffer,
                        sizeof(OutputBuffer),
                        &bytesReturned,
                        NULL
                        );

    if (!bRc)
    {
        printf("Error in DeviceIoControl : : %d", GetLastError());
        return;
    }

    printf("    OutBuffer (%d): %s\n", bytesReturned, OutputBuffer);

    return;
}

函数管理驱动:

BOOLEAN
ManageDriver(                                //   <- ManageDriver 
    IN LPCTSTR  DriverName,
    IN LPCTSTR  ServiceName,
    IN USHORT   Function
)
{

    SC_HANDLE   schSCManager;
    BOOLEAN rCode = TRUE;

    schSCManager = OpenSCManager(NULL,                   // local machine
                                 NULL,                   // local database
                                 SC_MANAGER_ALL_ACCESS   // access required
                                 )   

    // Do the requested function.
    switch (Function) {;

    case DRIVER_FUNC_REMOVE:       //  REMOVE

        printf("remove case. \n");
        // Stop the driver.

        StopDriver(schSCManager,DriverName);
            
        // Remove the driver service.

        RemoveDriver(schSCManager,DriverName);

        // Ignore all errors.

        rCode = TRUE;

        break;

    default:
        printf("Unknown ManageDriver() function. \n");
        rCode = FALSE;

        break;

     }


    // Close handle to service control manager.
    if (schSCManager) {

        CloseServiceHandle(schSCManager);
    }


    return rCode;
}   // ManageDriver  fin 

功能删除:

BOOLEAN
RemoveDriver(                                 //   <- RemoveDriver
    _In_ SC_HANDLE    SchSCManager,
    _In_ LPCTSTR      DriverName
)
{
    SC_HANDLE   schService;
    BOOLEAN     rCode;


    // Open the handle to the existing service.

    schService = OpenService(SchSCManager,DriverName,SERVICE_ALL_ACCESS);

    // Mark the service for deletion from the service control manager database.

    DeleteService(schService)

if (schService) {

        CloseServiceHandle(schService);
    }

    return rCode;

}   // RemoveDriver  fin 

函数启动驱动:

BOOLEAN
StartDriver(
    _In_ SC_HANDLE    SchSCManager,
    _In_ LPCTSTR      DriverName
)
{
    SC_HANDLE   schService;
    DWORD       err;


    // Open the handle to the existing service.

    schService = OpenService(SchSCManager, DriverName,SERVICE_ALL_ACCESS );
       
    // Start the execution of the service (i.e. start the driver).

    StartService(schService,     // service identifier
                      0,              // number of arguments
                      NULL            // pointer to arguments
                      )


    // Close the service object.

    if (schService) {

        CloseServiceHandle(schService);
    }

    return TRUE;

}   // StartDriver   fin

函数 StopDriver :

BOOLEAN
StopDriver(
    _In_ SC_HANDLE    SchSCManager,
    _In_ LPCTSTR      DriverName
)
{
    BOOLEAN         rCode = TRUE;
    SC_HANDLE       schService;
    SERVICE_STATUS  serviceStatus;

    //
    // Open the handle to the existing service.
    //

    schService = OpenService(SchSCManager,
                 DriverName,
                 SERVICE_ALL_ACCESS
                 );

    //
    // Request that the service stop.
    //

    ControlService(schService,
                   SERVICE_CONTROL_STOP,
                   &serviceStatus
                   )

    //
    // Close the service object.
    //

    if (schService) {

        CloseServiceHandle(schService);
    }

    return rCode;

}   //  StopDriver   fin


我删除了所有调试器,否则肯定会不清楚

如果您有任何迹象,也许我对应用程序的性质有误,也许解决方案非常愚蠢,但如果您知道有关为 pnp 驱动程序编写应用程序的任何事情,我就是接受者

缩短它:

我需要一个应用程序框架,但不只是任何一个,我需要一个适用于 pnp 驱动程序的框架。

(只要是 pnp,哪个驱动都没有关系) 这是为了能够与我的应用程序进行比较,看看我的应用程序缺少什么来支持即插即用

衷心感谢大家

【问题讨论】:

  • 完全不清楚你的问题是什么。
  • 不,我不明白你在问什么。从对您问题的回答来看,其他人也没有。通过阐明您要做什么来帮助我们帮助您。
  • 我编辑问题,这样更好吗?
  • 这仍然令人困惑,但是您只是想从用户模式与驱动程序通信吗?这听起来类似于this question。在这种情况下,它只是对DeviceIoControl() 的简单调用和DRIVER_DISPATCH 的相应实现。我不认为 PNP 或 KMDF 是相关的,除了 KMDF 在DRIVER_DISPATCH 周围有某种包装器。
  • 我想要写一个程序,让我告诉我的飞行员该做什么,有一个 .exe 让我让我的飞行员做事

标签: windows driver kmdf plug-and-play windows-application-driver


【解决方案1】:

您需要使用SetupDi函数获取设备路径,如this answer所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2013-04-03
    • 2014-04-01
    相关资源
    最近更新 更多