我正在使用IDesktopWallpaper::SetWallpaperwindows crate 中的方法。此方法的第二个参数是PCWSTR指向要设置为壁纸的图像的完整路径的(指针)。问题是该PCWSTR对象的类型是*const u16not *const String。如何PCWSTR从路径/字符串获取对象?
let path = "Path_to_image.jpg".to_string();
let ptr = &path as *const String;
let wallpaper = PCWSTR::from_raw(ptr);
// ^^^ expected raw pointer `*const u16`
// found raw pointer `*const String`
unsafe { desktop.SetWallpaper(None, wallpaper)};
Run Code Online (Sandbox Code Playgroud) 我最近发现了windows-rs框架,并一直在寻求通过实现其ICredentialProvider COM 接口来在 Rust 中构建Windows 凭据提供程序。
我一直在使用现有问题之一中汇总的信息进行概念验证实现,但我不确定如何实际将编译后的 Rust 公开为正确的 DLL,然后向 Windows 系统注册。
use std::cell::RefCell;
use windows::{
core::implement,
Win32::UI::Shell::{ICredentialProvider, ICredentialProvider_Impl},
};
fn main() -> windows::core::Result<()> {
#[implement(ICredentialProvider)]
struct Provider {
mutable_state: RefCell<u32>,
}
impl Provider {
fn new() -> Self {
Self {
mutable_state: RefCell::new(0),
}
}
}
impl ICredentialProvider_Impl for Provider {
fn Advise(
&self,
pcpe: &core::option::Option<windows::Win32::UI::Shell::ICredentialProviderEvents>,
upadvisecontext: usize,
) -> windows::core::Result<()> {
*self.mutable_state.borrow_mut() = 42;
todo!();
}
fn GetCredentialAt(
&self,
dwindex: u32,
) …Run Code Online (Sandbox Code Playgroud) 我有一个 Windows 应用程序,带有用 Rust 和 winapi 编写的 GUI。尽管有 GUI,但它的行为就像控制台应用程序。当exe文件启动时,Command Prompt会弹出一个窗口,并从中运行应用程序。这不是我想要的;应该打开一个主窗口,就像在所有真正的桌面应用程序中一样。我如何使用 winapi 在 Rust 中实现这个目标?
我研究了一些选择。您可以使用Tauri或gtk-rs开发 Windows 桌面应用程序,但这两种技术在用于 Windows 应用程序时都有缺点。可以在这里找到更多选项。我还尝试了互联网上提供的windows-rs 示例,但它们都是带有图形用户界面的控制台应用程序,这不是我想要的。
我还注意到,C++ 桌面应用程序使用该函数int APIENTRY wWinMain(...)作为入口点,而控制台应用程序使用int main(...), 并且wWinMain在 rust winapi 中似乎不可用。
在文档中有一个结构“IDesktopWallpaper”,其方法名为“GetWallpaper”。该方法引用“self”,但没有“IDesktopWallpaper”的构造函数方法。
use windows::{
core::*,
Win32::UI::Shell::IDesktopWallpaper,
};
fn main() -> Result<()> {
unsafe {
// ?????
IDesktopWallpaper::GetWallpaper(&self, monitorid);
}
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
为了达到我想要的结果,我应该做什么?
我试图使用EnumProcessesWin32 (psapi) 提供的函数枚举 Windows 进程。生活很顺利,所以我使用 Rust 而不是 C++。代码如下。
Cargo.toml
[dependencies.windows]
features = [
"Win32_Foundation",
"Win32_System_ProcessStatus"
]
Run Code Online (Sandbox Code Playgroud)
主程序.rs
use windows::Win32::System::ProcessStatus::*;
fn main() {
unsafe{
let mut cb_needed: u32 = 0;
let mut a_processes: Vec<u32> = Vec::with_capacity(1024);
let result: windows::Win32::Foundation::BOOL = EnumProcesses(a_processes.as_mut_ptr(), 1024*4, &mut cb_needed);
let mut _c_processes: u32 = 0;
_c_processes = cb_needed / 4;
println!("{:?}",_c_processes);
let mut count: u32 = 0;
while count < _c_processes {
println!("{:?}",a_processes[count as usize]);
count += 1;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我调试代码时,变量a_processes显示长度为零。然而,变量 …