You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been trying to load and use dsound.dll at runtime and it fails for some reason I don't understand. In the code snippet below, I tried to comment my thought process throughout:
use core::ptr::*;use winapi::shared::guiddef::*;use winapi::um::dsound::*;use winapi::um::libloaderapi::*;use winapi::um::unknwnbase::*;use winapi::um::winnt::*;fnmain(){unsafe{letDirectSoundCreate = {// copy the type signature of the original DirectSoundCreatetypeFnDirectSoundCreate = unsafeextern"system"fn(pcGuidDevice:LPCGUID,ppDS:*mutLPDIRECTSOUND,pUnkOuter:LPUNKNOWN,) -> HRESULT;// typecheck the above type against type of original functionlet _typecheck:FnDirectSoundCreate = winapi::um::dsound::DirectSoundCreate;println!("_typecheck, [{:?}]", &_typecheck as *const _);// try load dsound.dll using LoadLibraryAlet dsound_lib = LoadLibraryA("dsound.dll\0".as_ptr()as*mut_);if dsound_lib.is_null(){println!("could not load dsound.dll");return;}println!("loaded dsound.dll, [{:?}]", dsound_lib);// try get address of DirectSoundCreate()let fn_ptr = GetProcAddress(dsound_lib,"DirectSoundCreate\0".as_ptr()as*mut_);if fn_ptr asusize == 0{println!("could not load DirectSoundCreate");return;}println!("loaded DirectSoundCreate, [{:?}]", fn_ptr);// TOGGLE THIS// choose between using the original function vs. the one we just loaded// switch this to true and I get a STATUS_ACCESS_VIOLATION later on// switch this to false and code runs to the end.iffalse{*(fn_ptr as*constFnDirectSoundCreate)}else{
_typecheck
}};letmut dsound_obj = null_mut();// not important// try to use our function// leads to 0xc0000005, STATUS_ACCESS_VIOLATION if I use the loaded function// runs without issues with original winapi::um::dsound::DirectSoundCreateprintln!("calling DirectSoundCreate()");ifDirectSoundCreate(null(),&mut dsound_obj,null_mut()) != DS_OK{println!("cound not create direct sound object");return;}println!("end");}}
With the toggle true, the output is as follows (program crashes on its own):
Hello,
I've been trying to load and use dsound.dll at runtime and it fails for some reason I don't understand. In the code snippet below, I tried to comment my thought process throughout:
With the toggle true, the output is as follows (program crashes on its own):
With the toggle false, the output is as follows:
How do I make the loaded version of the function work as well?
Any help would be very much appreciated.
The text was updated successfully, but these errors were encountered: