Skip to content

Commit

Permalink
Add language and database properties (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
heaths authored Aug 8, 2022
1 parent 417f9be commit 838187b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"language": "en",
"words": [
"cdylib",

"INSTALLMESSAGE",
"LANGID",
"LPCSTR",
"LPSTR",
"msbuild",
Expand Down
15 changes: 15 additions & 0 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 Heath Stewart.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

use super::{MSIHANDLE, PMSIHANDLE};

/// The database for the current install session.
pub struct Database {
h: PMSIHANDLE,
}

impl From<MSIHANDLE> for Database {
fn from(h: MSIHANDLE) -> Self {
Database { h: h.to_owned() }
}
}
4 changes: 4 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ extern "C" {
#[link_name = "MsiDoActionA"]
pub fn MsiDoAction(hInstall: MSIHANDLE, szAction: LPCSTR) -> u32;

pub fn MsiGetActiveDatabase(hInstall: MSIHANDLE) -> MSIHANDLE;

pub fn MsiGetLanguage(hInstall: MSIHANDLE) -> u16;

pub fn MsiGetLastErrorRecord() -> MSIHANDLE;

pub fn MsiGetMode(hInstall: MSIHANDLE, eRunMode: RunMode) -> BOOL;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ compile_error!("supported on windows only");

// See https://docs.microsoft.com/windows/win32/msi/automation-interface-reference

mod database;
mod ffi;
mod record;
mod session;

pub use database::Database;
pub use ffi::{
ERROR_FUNCTION_NOT_CALLED, ERROR_INSTALL_FAILURE, ERROR_INSTALL_USEREXIT, ERROR_NO_MORE_ITEMS,
ERROR_SUCCESS,
Expand Down
29 changes: 14 additions & 15 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

use super::ffi;
use super::{MessageType, Record, RunMode, MSIHANDLE};
use super::{Database, MessageType, Record, RunMode, MSIHANDLE};
use std::ffi::CString;
use std::ops::Deref;

Expand All @@ -27,10 +27,16 @@ use std::ops::Deref;
/// ```
pub struct Session {
h: MSIHANDLE,
owned: bool,
}

impl Session {
/// Returns the active database for the installation. This function returns a read-only [`Database`].
pub fn database(&self) -> Database {
unsafe {
let h = ffi::MsiGetActiveDatabase(self.h);
Database::from(h)
}
}
/// Runs the specified immediate custom action, or schedules a deferred custom action.
/// If `None` the default action is run e.g., `INSTALL`.
///
Expand Down Expand Up @@ -77,6 +83,11 @@ impl Session {
self.do_action(Some(action));
}

/// The numeric language ID used by the current install session.
pub fn language(&self) -> u16 {
unsafe { ffi::MsiGetLanguage(self.h) }
}

/// Processes a [`Record`] within the [`Session`].
pub fn message(&self, kind: MessageType, record: &Record) -> i32 {
unsafe { ffi::MsiProcessMessage(self.h, kind, **record) }
Expand Down Expand Up @@ -168,20 +179,8 @@ impl Deref for Session {
}
}

impl Drop for Session {
fn drop(&mut self) {
// An MSIHANDLE must be closed when it was created by opening a package,
// but not closed when it was passed to a custom action.
if self.owned {
unsafe {
ffi::MsiCloseHandle(self.h);
}
}
}
}

impl From<MSIHANDLE> for Session {
fn from(h: MSIHANDLE) -> Self {
Session { h, owned: false }
Session { h }
}
}

0 comments on commit 838187b

Please sign in to comment.