Skip to content

Commit 838187b

Browse files
authored
Add language and database properties (#10)
1 parent 417f9be commit 838187b

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

.vscode/cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"language": "en",
44
"words": [
55
"cdylib",
6-
76
"INSTALLMESSAGE",
7+
"LANGID",
88
"LPCSTR",
99
"LPSTR",
1010
"msbuild",

src/database.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 Heath Stewart.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
use super::{MSIHANDLE, PMSIHANDLE};
5+
6+
/// The database for the current install session.
7+
pub struct Database {
8+
h: PMSIHANDLE,
9+
}
10+
11+
impl From<MSIHANDLE> for Database {
12+
fn from(h: MSIHANDLE) -> Self {
13+
Database { h: h.to_owned() }
14+
}
15+
}

src/ffi.rs

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

28+
pub fn MsiGetActiveDatabase(hInstall: MSIHANDLE) -> MSIHANDLE;
29+
30+
pub fn MsiGetLanguage(hInstall: MSIHANDLE) -> u16;
31+
2832
pub fn MsiGetLastErrorRecord() -> MSIHANDLE;
2933

3034
pub fn MsiGetMode(hInstall: MSIHANDLE, eRunMode: RunMode) -> BOOL;

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ compile_error!("supported on windows only");
1010

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

13+
mod database;
1314
mod ffi;
1415
mod record;
1516
mod session;
1617

18+
pub use database::Database;
1719
pub use ffi::{
1820
ERROR_FUNCTION_NOT_CALLED, ERROR_INSTALL_FAILURE, ERROR_INSTALL_USEREXIT, ERROR_NO_MORE_ITEMS,
1921
ERROR_SUCCESS,

src/session.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
use super::ffi;
5-
use super::{MessageType, Record, RunMode, MSIHANDLE};
5+
use super::{Database, MessageType, Record, RunMode, MSIHANDLE};
66
use std::ffi::CString;
77
use std::ops::Deref;
88

@@ -27,10 +27,16 @@ use std::ops::Deref;
2727
/// ```
2828
pub struct Session {
2929
h: MSIHANDLE,
30-
owned: bool,
3130
}
3231

3332
impl Session {
33+
/// Returns the active database for the installation. This function returns a read-only [`Database`].
34+
pub fn database(&self) -> Database {
35+
unsafe {
36+
let h = ffi::MsiGetActiveDatabase(self.h);
37+
Database::from(h)
38+
}
39+
}
3440
/// Runs the specified immediate custom action, or schedules a deferred custom action.
3541
/// If `None` the default action is run e.g., `INSTALL`.
3642
///
@@ -77,6 +83,11 @@ impl Session {
7783
self.do_action(Some(action));
7884
}
7985

86+
/// The numeric language ID used by the current install session.
87+
pub fn language(&self) -> u16 {
88+
unsafe { ffi::MsiGetLanguage(self.h) }
89+
}
90+
8091
/// Processes a [`Record`] within the [`Session`].
8192
pub fn message(&self, kind: MessageType, record: &Record) -> i32 {
8293
unsafe { ffi::MsiProcessMessage(self.h, kind, **record) }
@@ -168,20 +179,8 @@ impl Deref for Session {
168179
}
169180
}
170181

171-
impl Drop for Session {
172-
fn drop(&mut self) {
173-
// An MSIHANDLE must be closed when it was created by opening a package,
174-
// but not closed when it was passed to a custom action.
175-
if self.owned {
176-
unsafe {
177-
ffi::MsiCloseHandle(self.h);
178-
}
179-
}
180-
}
181-
}
182-
183182
impl From<MSIHANDLE> for Session {
184183
fn from(h: MSIHANDLE) -> Self {
185-
Session { h, owned: false }
184+
Session { h }
186185
}
187186
}

0 commit comments

Comments
 (0)