BowTiedGnome
BowTiedGnome
DDeno
Created by BowTiedGnome on 8/11/2024 in #help
FFI on windows and handles
I keep hitting an issue of getting a windows error code of 6, which is invalid handle on windows. Are there any extra steps that I'm missing that I need to do for invoking windows APIs with deno FFI that is maybe undocumented?. I can get this to work in other languages, but FFI/interop uses different types. so its not a 1:1 translation.
code is trying to determine if process is elevated/privileged. the following should be run with deno run -A --unstable-ffi ./main.ts will have to split over multiple posts due to discord limits
/**
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
HANDLE GetCurrentProcess();

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken
BOOL GetTokenInformation(
[in] HANDLE TokenHandle,
[in] TOKEN_INFORMATION_CLASS TokenInformationClass,
[out, optional] LPVOID TokenInformation,
[in] DWORD TokenInformationLength,
[out] PDWORD ReturnLength
);


https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-gettokeninformation
BOOL OpenProcessToken(
[in] HANDLE ProcessHandle,
[in] DWORD DesiredAccess,
[out] PHANDLE TokenHandle
);
*/


const kernel32 = Deno.dlopen("kernel32.dll", {
GetCurrentProcess: { parameters: [], result: "pointer" },
CloseHandle: { parameters: ["pointer"], result: "i32" },
GetLastError: { parameters: [], result: "i32" }
});

const advapi32 = Deno.dlopen("advapi32.dll", {
OpenProcessToken: {
parameters: ["pointer", "i32", "pointer"],
result: "bool",
},
GetTokenInformation: {
parameters: ["pointer", "u32", "buffer", "u32", "buffer"],
result: "bool",
},
});




/**
https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
HANDLE GetCurrentProcess();

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken
BOOL GetTokenInformation(
[in] HANDLE TokenHandle,
[in] TOKEN_INFORMATION_CLASS TokenInformationClass,
[out, optional] LPVOID TokenInformation,
[in] DWORD TokenInformationLength,
[out] PDWORD ReturnLength
);


https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-gettokeninformation
BOOL OpenProcessToken(
[in] HANDLE ProcessHandle,
[in] DWORD DesiredAccess,
[out] PHANDLE TokenHandle
);
*/


const kernel32 = Deno.dlopen("kernel32.dll", {
GetCurrentProcess: { parameters: [], result: "pointer" },
CloseHandle: { parameters: ["pointer"], result: "i32" },
GetLastError: { parameters: [], result: "i32" }
});

const advapi32 = Deno.dlopen("advapi32.dll", {
OpenProcessToken: {
parameters: ["pointer", "i32", "pointer"],
result: "bool",
},
GetTokenInformation: {
parameters: ["pointer", "u32", "buffer", "u32", "buffer"],
result: "bool",
},
});




4 replies