Skip to content

Commit e0c6b0e

Browse files
author
lynnux
committed
dynamic get QueryFullProcessImageNameA for XP support
1 parent 36750b9 commit e0c6b0e

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/symbolize/libbacktrace.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -382,18 +382,42 @@ unsafe fn init_state() -> *mut bt::backtrace_state {
382382
}
383383

384384
unsafe fn query_full_name(buf: &mut [i8]) -> Result<&[i8], ()> {
385-
let p1 = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
386-
let mut len = buf.len() as u32;
387-
let rc = QueryFullProcessImageNameA(p1, 0, buf.as_mut_ptr(), &mut len);
388-
CloseHandle(p1);
389-
385+
let dll = GetModuleHandleA(b"kernel32.dll\0".as_ptr() as *const i8);
386+
if dll.is_null() {
387+
return Err(())
388+
}
389+
let ptrQueryFullProcessImageNameA =
390+
GetProcAddress(dll, b"QueryFullProcessImageNameA\0".as_ptr() as *const _) as usize;
391+
let mut len: u32;
392+
if ptrQueryFullProcessImageNameA == 0
393+
{
394+
len = GetModuleFileNameA(0 as HMODULE, buf.as_mut_ptr(), buf.len() as u32);
395+
}
396+
else
397+
{
398+
use core::mem;
399+
let p1 = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, GetCurrentProcessId());
400+
len = buf.len() as u32;
401+
let pfnQueryFullProcessImageNameA : extern "system" fn(
402+
hProcess: HANDLE,
403+
dwFlags: DWORD,
404+
lpExeName: LPSTR,
405+
lpdwSize: PDWORD,
406+
) -> BOOL = mem::transmute(ptrQueryFullProcessImageNameA);
407+
408+
let rc = pfnQueryFullProcessImageNameA(p1, 0, buf.as_mut_ptr(), &mut len);
409+
CloseHandle(p1);
410+
if rc == 0 {
411+
return Err(())
412+
}
413+
}
390414
// We want to return a slice that is nul-terminated, so if
391415
// everything was filled in and it equals the total length
392416
// then equate that to failure.
393417
//
394418
// Otherwise when returning success make sure the nul byte is
395419
// included in the slice.
396-
if rc == 0 || len == buf.len() as u32 {
420+
if len == buf.len() as u32 {
397421
Err(())
398422
} else {
399423
assert_eq!(buf[len as usize], 0);

src/windows.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! A module to define the FFI definitions we use on Windows for `dbghelp.dll`
1+
//! A module to define the FFI definitions we use on Windows for `dbghelp.dll`
22
//!
33
//! This module uses a custom macro, `ffi!`, to wrap all definitions to
44
//! automatically generate tests to assert that our definitions here are the
@@ -343,19 +343,19 @@ ffi! {
343343
pub fn RtlCaptureContext(ContextRecord: PCONTEXT) -> ();
344344
pub fn LoadLibraryA(a: *const i8) -> HMODULE;
345345
pub fn GetProcAddress(h: HMODULE, name: *const i8) -> FARPROC;
346+
pub fn GetModuleHandleA(name: *const i8) -> HMODULE;
346347
pub fn OpenProcess(
347348
dwDesiredAccess: DWORD,
348349
bInheitHandle: BOOL,
349350
dwProcessId: DWORD,
350351
) -> HANDLE;
351352
pub fn GetCurrentProcessId() -> DWORD;
352353
pub fn CloseHandle(h: HANDLE) -> BOOL;
353-
pub fn QueryFullProcessImageNameA(
354-
hProcess: HANDLE,
355-
dwFlags: DWORD,
356-
lpExeName: LPSTR,
357-
lpdwSize: PDWORD,
358-
) -> BOOL;
354+
pub fn GetModuleFileNameA(
355+
hModule: HMODULE,
356+
lpFilename: LPSTR,
357+
nSize: DWORD)
358+
-> DWORD;
359359
pub fn CreateFileA(
360360
lpFileName: LPCSTR,
361361
dwDesiredAccess: DWORD,

0 commit comments

Comments
 (0)