📄 init.c
字号:
sizeof(L"PortName"), ComPort, sizeof(ComPort));
D_INIT(DbgPrint("FAKEMODEM: PortName %ws\n",ComPort);)
if (Status != STATUS_SUCCESS)
{
ExFreePool(SymbolicLink.Buffer);
ZwClose(keyHandle);
return Status;
}
RtlAppendUnicodeToString(&SymbolicLink, ComPort);
ZwClose(keyHandle);
if (Create)
{
UNICODE_STRING PdoName;
PdoName.Length=0;
PdoName.MaximumLength=sizeof(WCHAR)*256;
PdoName.Buffer=ExAllocatePool(PagedPool,
PdoName.MaximumLength+sizeof(WCHAR));
if (PdoName.Buffer == NULL)
{
ExFreePool(SymbolicLink.Buffer);
return STATUS_INSUFFICIENT_RESOURCES;
}
RtlZeroMemory(PdoName.Buffer,PdoName.MaximumLength);
Status=IoGetDeviceProperty(Pdo, DevicePropertyPhysicalDeviceObjectName,
(ULONG)PdoName.MaximumLength, PdoName.Buffer, &StringLength);
if (!NT_SUCCESS(Status))
{
D_INIT(DbgPrint("FAKEMODEM: IoGetDeviceProperty() failed %08lx\n",
Status);)
ExFreePool(SymbolicLink.Buffer);
return Status;
}
PdoName.Length+=(USHORT)StringLength-sizeof(UNICODE_NULL);
D_INIT(DbgPrint("FAKEMODEM: PdoName: %ws\n",PdoName.Buffer);)
Status=IoCreateSymbolicLink(&SymbolicLink, &PdoName);
Status=IoRegisterDeviceInterface(Pdo, &GUID_CLASS_MODEM, NULL,
InterfaceName);
if (NT_SUCCESS(Status))
{
IoSetDeviceInterfaceState(InterfaceName, TRUE);
} else
{
D_INIT(DbgPrint("FAKEMODEM: IoRegisterDeviceInterface() failed %08lx\n",Status);)
}
Status = RtlWriteRegistryValue(RTL_REGISTRY_DEVICEMAP, L"SERIALCOMM",
PdoName.Buffer, REG_SZ, ComPort,
(wcslen(ComPort) + 1) * sizeof(WCHAR));
if (!NT_SUCCESS(Status))
{
D_INIT(DbgPrint("FAKEMODEM: RtlWriteRegistryValue() failed %08lx\n",Status);)
ExFreePool(SymbolicLink.Buffer);
ExFreePool(PdoName.Buffer);
return Status;
}
ExFreePool(PdoName.Buffer);
} else {
Status=IoDeleteSymbolicLink(&SymbolicLink);
D_INIT(DbgPrint("FAKEMODEM: Deleted symbolic link\n");)
}
ExFreePool(SymbolicLink.Buffer);
D_INIT(DbgPrint("FAKEMODEM: End of handle symbolic link\n");)
return Status;
}
NTSTATUS
QueryDeviceCaps(
PDEVICE_OBJECT Pdo,
PDEVICE_CAPABILITIES Capabilities
)
{
PDEVICE_OBJECT deviceObject=Pdo;
PIRP irp;
PIO_STACK_LOCATION NextSp;
KEVENT Event;
NTSTATUS Status;
// Get a pointer to the top most device object in the stack of
// devices, beginning with the deviceObject.
while (deviceObject->AttachedDevice)
{
deviceObject = deviceObject->AttachedDevice;
}
// Begin by allocating the IRP for this request. Do not charge
// quota to the current process for this IRP.
irp = IoAllocateIrp(
#if DBG
(UCHAR)(deviceObject->StackSize+1),
#else
deviceObject->StackSize,
#endif
FALSE);
if (irp == NULL)
{
return STATUS_INSUFFICIENT_RESOURCES;
}
#if DBG
{
// Setup a current stack location, so the debug code can see the
// MJ value
PIO_STACK_LOCATION irpSp=IoGetNextIrpStackLocation(irp);
irpSp->MajorFunction=IRP_MJ_PNP;
IoSetNextIrpStackLocation(irp);
}
#endif
irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
irp->IoStatus.Information = 0;
RtlZeroMemory(Capabilities,sizeof(DEVICE_CAPABILITIES));
Capabilities->Size=sizeof(DEVICE_CAPABILITIES);
Capabilities->Version=1;
Capabilities->Address=-1;
Capabilities->UINumber=-1;
// Get a pointer to the stack location of the first driver which will be
// invoked. This is where the function codes and parameters are set.
NextSp = IoGetNextIrpStackLocation(irp);
NextSp->MajorFunction=IRP_MJ_PNP;
NextSp->MinorFunction=IRP_MN_QUERY_CAPABILITIES;
NextSp->Parameters.DeviceCapabilities.Capabilities=Capabilities;
Status=WaitForLowerDriverToCompleteIrp(deviceObject, irp, FALSE );
IoFreeIrp(irp);
return Status;
}
NTSTATUS
ModemSetRegistryKeyValue(
IN PDEVICE_OBJECT Pdo,
IN ULONG DevInstKeyType,
IN PWCHAR KeyNameString,
IN ULONG DataType,
IN PVOID Data,
IN ULONG DataLength)
{
NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
HANDLE Handle;
UNICODE_STRING keyName;
PAGED_CODE();
D_ERROR(DbgPrint("MODEM: Current IRQL %d\n",KeGetCurrentIrql());)
ntStatus = IoOpenDeviceRegistryKey(Pdo, DevInstKeyType, KEY_ALL_ACCESS,
&Handle);
if (NT_SUCCESS(ntStatus))
{
RtlInitUnicodeString(&keyName,KeyNameString);
ntStatus = ZwSetValueKey(Handle, &keyName, 0, DataType, Data,
DataLength);
if (!NT_SUCCESS(ntStatus))
{
D_ERROR(DbgPrint("MODEM: Could not set value, %08lx\n",ntStatus);)
}
} else
{
ZwClose(Handle);
D_ERROR(DbgPrint("MODEM: Could not open dev registry key, %08lx\n",
ntStatus);)
}
return ntStatus;
}
NTSTATUS
ModemGetRegistryKeyValue (
IN PDEVICE_OBJECT Pdo,
IN ULONG DevInstKeyType,
IN PWCHAR KeyNameString,
IN PVOID Data,
IN ULONG DataLength
)
{
UNICODE_STRING keyName;
ULONG length;
PKEY_VALUE_PARTIAL_INFORMATION PartialInfo;
NTSTATUS ntStatus = STATUS_INSUFFICIENT_RESOURCES;
HANDLE Handle;
PAGED_CODE();
ntStatus = IoOpenDeviceRegistryKey(Pdo, DevInstKeyType,
STANDARD_RIGHTS_READ, &Handle);
if (NT_SUCCESS(ntStatus))
{
RtlInitUnicodeString (&keyName, KeyNameString);
length = sizeof(KEY_VALUE_FULL_INFORMATION) + DataLength;
PartialInfo = ALLOCATE_PAGED_POOL(length);
if (PartialInfo)
{
ntStatus = ZwQueryValueKey (Handle, &keyName,
KeyValuePartialInformation, PartialInfo, length, &length);
if (NT_SUCCESS(ntStatus))
{
//
// If there is enough room in the data buffer, copy the output
//
if (DataLength >= PartialInfo->DataLength)
{
RtlCopyMemory (Data, PartialInfo->Data,
PartialInfo->DataLength);
}
} else
{
D_ERROR(DbgPrint("MODEM: could not query value, %08lx\n",
ntStatus);)
}
FREE_POOL(PartialInfo);
}
ZwClose(Handle);
} else {
D_ERROR(DbgPrint("MODEM: could open device reg key, %08lx\n",ntStatus);)
}
return ntStatus;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -