Skip to content

Commit 417f9be

Browse files
authored
Add last_error_record function (#9)
1 parent 3232c78 commit 417f9be

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

src/ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extern "C" {
2525
#[link_name = "MsiDoActionA"]
2626
pub fn MsiDoAction(hInstall: MSIHANDLE, szAction: LPCSTR) -> u32;
2727

28+
pub fn MsiGetLastErrorRecord() -> MSIHANDLE;
29+
2830
pub fn MsiGetMode(hInstall: MSIHANDLE, eRunMode: RunMode) -> BOOL;
2931

3032
#[link_name = "MsiGetPropertyA"]

src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ pub enum RunMode {
7171
Commit = 18,
7272
}
7373

74+
/// Gets the last Windows Installer error for the current process.
75+
///
76+
/// # Example
77+
///
78+
/// ```
79+
/// use msica::*;
80+
///
81+
/// if let Some(error) = last_error_record() {
82+
/// println!("last error: {}", error.format_text());
83+
/// }
84+
/// ```
85+
pub fn last_error_record() -> Option<Record> {
86+
unsafe {
87+
match ffi::MsiGetLastErrorRecord() {
88+
h if !h.is_null() => Some(h.into()),
89+
_ => None,
90+
}
91+
}
92+
}
93+
7494
/// A Windows Installer handle. This handle is not automatically closed.
7595
#[derive(Clone, Copy)]
7696
#[repr(transparent)]
@@ -84,6 +104,10 @@ impl MSIHANDLE {
84104
fn to_owned(&self) -> PMSIHANDLE {
85105
PMSIHANDLE(*self)
86106
}
107+
108+
fn is_null(&self) -> bool {
109+
self.0 == 0
110+
}
87111
}
88112

89113
impl From<u32> for MSIHANDLE {

src/record.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ impl Deref for Record {
263263
}
264264
}
265265

266+
impl From<MSIHANDLE> for Record {
267+
fn from(h: MSIHANDLE) -> Self {
268+
Record { h: h.to_owned() }
269+
}
270+
}
271+
266272
impl From<&str> for Record {
267273
fn from(s: &str) -> Self {
268274
unsafe {

0 commit comments

Comments
 (0)