Skip to content

Commit

Permalink
flutter windows main.cpp get app name from rust (rustdesk#7316)
Browse files Browse the repository at this point in the history
Signed-off-by: 21pages <[email protected]>
  • Loading branch information
21pages authored Mar 6, 2024
1 parent f3686b2 commit dd44bb2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
14 changes: 11 additions & 3 deletions flutter/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

typedef char** (*FUNC_RUSTDESK_CORE_MAIN)(int*);
typedef void (*FUNC_RUSTDESK_FREE_ARGS)( char**, int);
const char* uniLinksPrefix = "rustdesk://";
typedef int (*FUNC_RUSTDESK_GET_APP_NAME)(wchar_t*, int);
/// Note: `--server`, `--service` are already handled in [core_main.rs].
const std::vector<std::string> parameters_white_list = {"--install", "--cm"};

Expand All @@ -39,6 +39,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
std::cout << "Failed to get free_c_args." << std::endl;
return EXIT_FAILURE;
}
std::wstring app_name = L"RustDesk";
FUNC_RUSTDESK_GET_APP_NAME get_rustdesk_app_name = (FUNC_RUSTDESK_GET_APP_NAME)GetProcAddress(hInstance, "get_rustdesk_app_name");
if (get_rustdesk_app_name) {
wchar_t app_name_buffer[512] = {0};
if (get_rustdesk_app_name(app_name_buffer, 512) == 0) {
app_name = std::wstring(app_name_buffer);
}
}
std::vector<std::string> command_line_arguments =
GetCommandLineArguments();
// Remove possible trailing whitespace from command line arguments
Expand All @@ -61,7 +69,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
free_c_args(c_args, args_len);

// Uri links dispatch
HWND hwnd = ::FindWindow(_T("FLUTTER_RUNNER_WIN32_WINDOW"), _T("RustDesk"));
HWND hwnd = ::FindWindow(_T("FLUTTER_RUNNER_WIN32_WINDOW"), app_name.c_str());
if (hwnd != NULL) {
// Allow multiple flutter instances when being executed by parameters
// contained in whitelists.
Expand Down Expand Up @@ -112,7 +120,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
Win32Window::Point origin(10, 10);
Win32Window::Size size(800, 600);
if (!window.CreateAndShow(
is_cm_page ? L"RustDesk - Connection Manager" : L"RustDesk", origin,
is_cm_page ? app_name + L" - Connection Manager" : app_name, origin,
size, !is_cm_page)) {
return EXIT_FAILURE;
}
Expand Down
11 changes: 11 additions & 0 deletions src/flutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ pub unsafe extern "C" fn free_c_args(ptr: *mut *mut c_char, len: c_int) {
// Afterwards the vector will be dropped and thus freed.
}

#[cfg(windows)]
#[no_mangle]
pub unsafe extern "C" fn get_rustdesk_app_name(buffer: *mut u16, length: i32) -> i32 {
let name = crate::platform::wide_string(&crate::get_app_name());
if length > name.len() as i32 {
std::ptr::copy_nonoverlapping(name.as_ptr(), buffer, name.len());
return 0;
}
-1
}

#[derive(Default)]
struct SessionHandler {
event_stream: Option<StreamSink<EventToUI>>,
Expand Down
2 changes: 1 addition & 1 deletion src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1748,7 +1748,7 @@ pub fn get_double_click_time() -> u32 {
unsafe { GetDoubleClickTime() }
}

fn wide_string(s: &str) -> Vec<u16> {
pub fn wide_string(s: &str) -> Vec<u16> {
use std::os::windows::prelude::OsStrExt;
std::ffi::OsStr::new(s)
.encode_wide()
Expand Down

0 comments on commit dd44bb2

Please sign in to comment.