Skip to content

Commit

Permalink
Add Background Intelligent Transfer Service (BITS) sample (#2277)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 12, 2023
1 parent 1bfd409 commit 7c65c07
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
>> $env:GITHUB_ENV
- name: Run cargo clippy
run: |
cargo clippy -p sample_bits &&
cargo clippy -p sample_com_uri &&
cargo clippy -p sample_consent &&
cargo clippy -p sample_core_app &&
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ jobs:
}
- name: Test
run: >
cargo test --target ${{ matrix.target }} -p sample_bits &&
cargo test --target ${{ matrix.target }} -p sample_com_uri &&
cargo test --target ${{ matrix.target }} -p sample_consent &&
cargo test --target ${{ matrix.target }} -p sample_core_app &&
Expand Down Expand Up @@ -131,8 +132,8 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_enums &&
cargo test --target ${{ matrix.target }} -p test_error &&
cargo test --target ${{ matrix.target }} -p test_event &&
cargo test --target ${{ matrix.target }} -p test_extensions &&
cargo clean &&
cargo test --target ${{ matrix.target }} -p test_extensions &&
cargo test --target ${{ matrix.target }} -p test_handles &&
cargo test --target ${{ matrix.target }} -p test_helpers &&
cargo test --target ${{ matrix.target }} -p test_implement &&
Expand Down
12 changes: 12 additions & 0 deletions crates/samples/windows/bits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "sample_bits"
version = "0.0.0"
edition = "2018"

[dependencies.windows]
path = "../../../libs/windows"
features = [
"implement",
"Win32_System_Com",
"Win32_Networking_BackgroundIntelligentTransferService",
]
70 changes: 70 additions & 0 deletions crates/samples/windows/bits/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use windows::{
core::*, Win32::Networking::BackgroundIntelligentTransferService::*, Win32::System::Com::*,
};

fn main() -> Result<()> {
unsafe {
CoInitializeEx(None, COINIT_MULTITHREADED)?;

let manager: IBackgroundCopyManager =
CoCreateInstance(&BackgroundCopyManager, None, CLSCTX_LOCAL_SERVER)?;

let mut job = None;

manager.CreateJob(
w!("sample"),
BG_JOB_TYPE_DOWNLOAD,
&mut Default::default(),
&mut job,
)?;

let job = job.unwrap();
job.AddFile(w!("https://kennykerr.ca/favicon.svg"), w!("D:\\rust.svg"))?;

let callback: IBackgroundCopyCallback = Callback.into();
job.SetNotifyInterface(&callback)?;
job.SetNotifyFlags(BG_NOTIFY_JOB_TRANSFERRED | BG_NOTIFY_JOB_ERROR)?;

job.Resume()?;
println!("downloading...");

getchar();
job.Cancel()?;
println!("canceled");
Ok(())
}
}

#[implement(IBackgroundCopyCallback)]
struct Callback;

impl IBackgroundCopyCallback_Impl for Callback {
fn JobTransferred(&self, job: &Option<IBackgroundCopyJob>) -> Result<()> {
let job = job.as_ref().unwrap();
unsafe { job.Complete()? };
println!("done");
std::process::exit(0);
}

fn JobError(
&self,
job: &Option<IBackgroundCopyJob>,
error: &Option<IBackgroundCopyError>,
) -> Result<()> {
let job = job.as_ref().unwrap();
let error = error.as_ref().unwrap();
unsafe {
job.Cancel()?;
println!("{}", error.GetErrorDescription(0)?.display());
}
std::process::exit(0);
}

fn JobModification(&self, _: &Option<IBackgroundCopyJob>, _: u32) -> Result<()> {
Ok(())
}
}

extern "C" {
pub fn getchar() -> i32;
}

0 comments on commit 7c65c07

Please sign in to comment.