Describe the bug

Trying out the debugging config from the docs here, but running into an error with the below snippet (using windows-rs to load a DLL).

When running tauri dev, the load_library_command returns "Big success". But when debugging, it errors out with "The system cannot find the file specified". This happens with both the LLDB and C++ extensions.

use anyhow::Result;
use windows::core::PCWSTR;
use windows::Win32::Foundation::{HANDLE, HINSTANCE};
use windows::Win32::System::LibraryLoader::{
  LoadLibraryExW, LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE,
};

pub fn load_library_ex(library_path: String) -> Result<HINSTANCE> {
  let utf16_path = PCWSTR::from(&library_path.into());

  unsafe {
    let lib_handle = LoadLibraryExW(
      utf16_path,
      HANDLE::default(),
      LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE,
    )?;

    return Ok(lib_handle);
  }
}

#[tauri::command]
async fn load_library_command() -> Result<String, String> {
  let lib_handle =
    load_library_ex("C:\\Windows\\Resources\\Themes\\aero\\aero.msstyles".into());

  match lib_handle {
    Ok(_) => Ok("Big success".into()),
    Err(err) => Err(err.to_string()),
  }
}

fn main() {
  tauri::Builder::default()
    .invoke_handler(tauri::generate_handler![load_library_command])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Reproduction

No response

Expected behavior

Consistent behavior when debugging vs tauri dev

Platform and versions

Environment › OS: Windows 10.0.19044 X64 › Webview2: 105.0.1343.53 › MSVC: › Node.js: 16.16.0 › npm: 8.17.0 › pnpm: 7.12.2 › yarn: 1.22.19 › rustup: 1.25.1 › rustc: 1.64.0 › cargo: 1.64.0 › Rust toolchain: stable-x86_64-pc-windows-msvc

Packages › @tauri-apps/cli [NPM]: 1.1.1 › @tauri-apps/api [NPM]: Not installed! › tauri [RUST]: 1.1.1, › tauri-build [RUST]: 1.1.1, › tao [RUST]: 0.14.0, › wry [RUST]: 0.21.1,

App › build-type: bundle › CSP: unset › distDir: ../desktop-fe/dist/desktop-fe › devPath: http://localhost:4200/

App directory structure ├─ .git ├─ .vscode ├─ apps ├─ libs └─ node_modules

Stack trace

No response

Additional context

No response

0

If I hardcode the path in the call to LoadLibraryExW, then the snippet above works both when debugging and when running tauri dev:

let lib_handle = LoadLibraryExW(
  w!("C:\\Windows\\Resources\\Themes\\aero\\aero.msstyles"),
  HANDLE(0),
  LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE,
)?;

So it seems like there's some difference in how the string conversion behaves for some reason:

let utf16_path = PCWSTR::from(&library_path.into());
0

The w! macro encodes the string as UTF-16 and adds a null terminator, so you need to do that before passing to PCWSTR. Here is what we use at TAO https://github.com/tauri-apps/tao/blob/0637c605bd74eaf6ac9995a340bcc650a46664e8/src/platform_impl/windows/util.rs#L50

This still doesn't explain why it works in tauri dev but worth giving a shot

0

The w! macro encodes the string as UTF-16 and adds a null terminator, so you need to do that before passing to PCWSTR. Here is what we use at TAO https://github.com/tauri-apps/tao/blob/0637c605bd74eaf6ac9995a340bcc650a46664e8/src/platform_impl/windows/util.rs#L50

This still doesn't explain why it works in tauri dev but worth giving a shot

After adding the null terminator, it works fine in both cases now. Don't know why it worked in the first place when running tauri dev.

0
© 2022 pullanswer.com - All rights reserved.