Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create handle function #5

Closed
vxcall opened this issue Nov 6, 2023 · 0 comments
Closed

create handle function #5

vxcall opened this issue Nov 6, 2023 · 0 comments

Comments

@vxcall
Copy link
Owner

vxcall commented Nov 6, 2023

in init function,

  • it needs opening device handle by NtOpenFile
// L"\\Device\\echo", GENERIC_READ | GENERIC_WRITE
auto retrieve_device_handle(std::wstring device_name, ACCESS_MASK access_mask) -> PHANDLE
{
    NTSTATUS status;
    HANDLE device_handle;
    OBJECT_ATTRIBUTES obj_attr;
    UNICODE_STRING uni_device_name;
    IO_STATUS_BLOCK io_status_block;

    RtlInitUnicodeString(&uni_device_name, device_name);

    InitializeObjectAttributes(&obj_attr, &uni_device_name,
        OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);

    ACCESS_MASK access_mask = ;
    ULONG share_access = 0;
    ULONG open_options = 0;

    status = NtOpenFile(&device_handle,
                        access_mask,
                        &obj_attr,
                        &io_status_block,
                        share_access,
                        open_options);

    if (!NT_SUCCESS(status)) {
        std::cerr << "Failed to open handle. Status code: " << std::hex << status << std::endl;
        return nullptr;
    }
    // This handle has to be closed with CloseHandle(device_handle);
    return device_handle;
}
@vxcall vxcall changed the title write init func create init func Nov 6, 2023
@vxcall vxcall changed the title create init func create handle function Nov 6, 2023
@vxcall vxcall closed this as completed Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant