驅動程式 - Windows Driver Model (WDM) - 使用範例 - C/C++ (DDK) - Use Semaphore



參考資訊:
https://wasm.in/
http://four-f.narod.ru/
https://github.com/steward-fu/ddk

main.c

#include <wdm.h>
 
#define DEV_NAME L"\\Device\\MyDriver"
#define SYM_NAME L"\\DosDevices\\MyDriver"
 
#define MAX_THREAD      3
#define MAX_SEMA_COUNT  2
#define MAX_SEMA_LIMIT  2
 
KSEMAPHORE mySemaphore = {0};
PVOID pThread[MAX_THREAD] = {0};
PDEVICE_OBJECT pNextDevice = NULL;
 
void MyThread(PVOID pParam)
{
    ULONG cc = 0;
    int t = (int)pParam;
    NTSTATUS status = 0;
    LARGE_INTEGER stTime = { 0 };
    
    stTime.HighPart |= -1;
    stTime.LowPart = -10000000;
    DbgPrint("Thread%d, Acquiring Semaphore", t);
    status = KeWaitForSingleObject(&mySemaphore, Executive, KernelMode, FALSE, NULL);
    DbgPrint("Thread%d, Acquired Semaphore", t);
    DbgPrint("Thread%d, Sleeping", t);
    KeDelayExecutionThread(KernelMode, FALSE, &stTime);
    DbgPrint("Thread%d, Releasing Semaphore", t);
    cc = KeReadStateSemaphore(&mySemaphore);
    KeReleaseSemaphore(&mySemaphore, IO_NO_INCREMENT, (cc < MAX_SEMA_LIMIT) ? 1 : 0, FALSE);
    DbgPrint("Thread%d, Released Semaphore", t);
    PsTerminateSystemThread(STATUS_SUCCESS);
}
 
NTSTATUS AddDevice(PDRIVER_OBJECT pMyDriver, PDEVICE_OBJECT pPhyDevice)
{
    PDEVICE_OBJECT pMyDevice = NULL;
    UNICODE_STRING usDeviceName = { 0 };
    UNICODE_STRING usSymbolName = { 0 };
 
    RtlInitUnicodeString(&usDeviceName, DEV_NAME);
    IoCreateDevice(pMyDriver, 0, &usDeviceName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pMyDevice);
    RtlInitUnicodeString(&usSymbolName, SYM_NAME);
    IoCreateSymbolicLink(&usSymbolName, &usDeviceName);
    pNextDevice = IoAttachDeviceToDeviceStack(pMyDevice, pPhyDevice);
    pMyDevice->Flags &= ~DO_DEVICE_INITIALIZING;
    pMyDevice->Flags |= DO_BUFFERED_IO;
    return STATUS_SUCCESS;
}
 
void Unload(PDRIVER_OBJECT pMyDriver)
{
    pMyDriver = pMyDriver;
}
 
NTSTATUS IrpPnp(PDEVICE_OBJECT pMyDevice, PIRP pIrp)
{
    int cc = 0;
    UNICODE_STRING usSymbolName = {0};
    PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
 
    if (pStack->MinorFunction == IRP_MN_REMOVE_DEVICE) {
        for (cc = 0; cc < MAX_THREAD; cc++) {
            if (pThread[cc] != NULL) {
                KeWaitForSingleObject(pThread[cc], Executive, KernelMode, FALSE, NULL);
                ObDereferenceObject(pThread[cc]);
            }
        }
        RtlInitUnicodeString(&usSymbolName, SYM_NAME);
        IoDeleteSymbolicLink(&usSymbolName);
        IoDetachDevice(pNextDevice);
        IoDeleteDevice(pMyDevice);
        IoCompleteRequest(pIrp, IO_NO_INCREMENT);
        return STATUS_SUCCESS;
    }
    IoSkipCurrentIrpStackLocation(pIrp);
    return IoCallDriver(pNextDevice, pIrp);
}
 
NTSTATUS DriverEntry(PDRIVER_OBJECT pMyDriver, PUNICODE_STRING pMyRegistry)
{
    int cc = 0;
    HANDLE hThread = 0;
    NTSTATUS status = 0;
 
    pMyDriver->MajorFunction[IRP_MJ_PNP] = IrpPnp;
    pMyDriver->DriverExtension->AddDevice = AddDevice;
    pMyDriver->DriverUnload = Unload;
 
    KeInitializeSemaphore(&mySemaphore, MAX_SEMA_COUNT, MAX_SEMA_LIMIT);
    for (cc = 0; cc < MAX_THREAD; cc++) {
        status = PsCreateSystemThread(&hThread, THREAD_ALL_ACCESS, NULL, (PHANDLE)-1, NULL, MyThread, (PVOID)cc);
        if (status == STATUS_SUCCESS) {
            ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL, KernelMode, &pThread[cc], NULL);
            ZwClose(hThread);
        }
    }
    return STATUS_SUCCESS;
}

完成