Skip to content

Instantly share code, notes, and snippets.

Avatar

Kenny Kerr kennykerr

View GitHub Profile
@kennykerr
kennykerr / sample.rs
Created Jul 14, 2021
Query the service control manager using the Windows crate
View sample.rs
fn main() -> Result<()> {
unsafe {
let scm = OpenSCManagerA(None, None, GENERIC_READ);
let service = OpenServiceA(scm, "Fax", GENERIC_READ);
let mut buffer_size = 0;
QueryServiceStatusEx(
service,
SC_STATUS_PROCESS_INFO,
@kennykerr
kennykerr / file_bytes.rs
Created May 28, 2021
Static memory mapped file in Rust
View file_bytes.rs
pub fn file_bytes(filename: &std::path::PathBuf) -> &'static [u8] {
let mut filename = String::from(filename.as_path().to_str().expect("Unexpected file name"));
filename.push('\0');
let file = unsafe {
CreateFileA(
filename.as_bytes().as_ptr(),
FILE_GENERIC_READ,
FILE_SHARE_READ,
View extend.rs
#[implement(
extend Windows::UI::Xaml::Application,
override OnLaunched,
Windows::Foundation::IStringable,
)]
struct App {
}
impl App {
fn OnLaunched(&self, _: &LaunchActivatedEventArgs) -> Result<()> {
View dependencies.txt
{
"Windows.AI.MachineLearning": {
"Windows.Foundation",
"Windows.Foundation.Collections",
},
"Windows.AI.MachineLearning.Preview": {
"Windows.Foundation.Collections",
},
"Windows.ApplicationModel.AppService": {
"Windows.Foundation",
View UserDataPaths.rs
use bindings::windows::storage::UserDataPaths;
fn main() -> windows::Result<()> {
let path = UserDataPaths::get_default()?.local_app_data()?;
println!("{}", path);
Ok(())
}
View SHGetKnownFolderPath.rs
use bindings::{
windows::win32::com::CoTaskMemFree,
windows::win32::shell::SHGetKnownFolderPath,
windows::win32::system_services::{HANDLE, PWSTR},
};
fn known_folder_path(id: &windows::Guid) -> windows::Result<String> {
unsafe {
let mut path = PWSTR::default();
SHGetKnownFolderPath(id, 0, HANDLE(0), &mut path).ok()?;
View spellchecker.rs
use bindings::{windows::win32::intl::*, windows::*};
fn main() -> Result<()> {
initialize_mta()?;
let factory: ISpellCheckerFactory = create_instance(&SpellCheckerFactory)?;
let locale = HString::from("en-US");
let mut supported = FALSE;
unsafe {
View winrt_declare.rs
winrt::declare!(
// A WinRT struct defined here
pub struct StructType { x: i32, y: i32 };
// A WinRT interface defined here (uuid is inferred)
// Implemented by the class itself.
pub interface IClassType {
fn method(&self) -> Result<i32>;
}
View com_declare.rs
com::declare!(
// A COM interface
[uuid(...)]
pub interface IComInterface {
fn foo(&self);
fn bar(&self);
}
// A public COM class must have a [uuid]
View declare.rs
com::declare!(
// A COM interface declared here
[uuid(...)]
interface IOldSchool {...}
// A COM class declared here (not defined here)
[uuid(...)]
class ExternalClass : IOldSchool;