Skip to content

Commit

Permalink
Merge pull request #9 from TotemArts/comfix2
Browse files Browse the repository at this point in the history
Avoid initializing COM on behalf of the application
  • Loading branch information
SonnyX authored Nov 22, 2023
2 parents 515cddd + 5e7dc11 commit f5f4a3d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/dinput8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,38 @@ pub unsafe extern "C" fn directinput8_create(
dwversion: u32,
riid: *const GUID,
out: *mut Option<IUnknown>,
_outer: Option<IUnknown>,
outer: Option<IUnknown>,
) -> HRESULT {
// Instead of trying to load the original dinput8.dll and calling the original `DirectInput8Create`,
// we can simply load the dinput8 interface via COM and return it up to our caller. This is basically
// what DirectInput8Create does noawadays anyway.
//
// Reference: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ee416756(v=vs.85)
let f = || -> Result<IUnknown, windows::core::Error> {
// Initialize COM with the default apartment type.
// NOTE: Disabled for now. The documentation does not really specify what the original `DirectInput8Create`
// does in this case, and I'm too lazy to disassemble dinput8.dll to figure it out.
// unsafe { CoInitializeEx(None, COINIT_MULTITHREADED)? };

match unsafe { riid.as_ref() } {
Some(&IDirectInput8A::IID) => {
let dinput: IDirectInput8A =
CoCreateInstance(&CLSID_DirectInput8, None, CLSCTX_INPROC_SERVER)?;
CoCreateInstance(&CLSID_DirectInput8, outer.as_ref(), CLSCTX_INPROC_SERVER)?;

dinput.Initialize(hinst, dwversion)?;
// Per the documentation, if pUnkOuter != NULL then the resulting object must be initialized manually.
if outer.is_none() {
dinput.Initialize(hinst, dwversion)?;
}
Ok(dinput.cast()?)
}
Some(&IDirectInput8W::IID) => {
let dinput: IDirectInput8W =
CoCreateInstance(&CLSID_DirectInput8, None, CLSCTX_INPROC_SERVER)?;
CoCreateInstance(&CLSID_DirectInput8, outer.as_ref(), CLSCTX_INPROC_SERVER)?;

dinput.Initialize(hinst, dwversion)?;
// Per the documentation, if pUnkOuter != NULL then the resulting object must be initialized manually.
if outer.is_none() {
dinput.Initialize(hinst, dwversion)?;
}
Ok(dinput.cast()?)
}
_ => return Err(E_NOINTERFACE.into()),
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use anyhow::Context;
use sha2::{Digest, Sha256};

use windows::core::PCWSTR;
use windows::Win32::System::Com::{CoInitializeEx, COINIT_MULTITHREADED};
use windows::Win32::{
Foundation::{HANDLE, HINSTANCE},
System::{
Expand Down Expand Up @@ -102,9 +101,6 @@ fn get_module_slice(info: &MODULEINFO) -> *const [u8] {
/// Called upon DLL attach. This function verifies the UDK and initializes
/// hooks if the UDK matches our known hash.
fn dll_attach() -> anyhow::Result<()> {
// Ensure that COM is initialized.
unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }.context("failed to initialize COM")?;

let process = unsafe { GetCurrentProcess() };
let module = unsafe { GetModuleHandleA(None) }.context("failed to get module handle")?;

Expand Down

0 comments on commit f5f4a3d

Please sign in to comment.