Skip to content

Commit 865b9ba

Browse files
committed
Initial commit
0 parents  commit 865b9ba

16 files changed

Lines changed: 1167 additions & 0 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
Cargo.lock
3+
.metadata
4+
.vscode

Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "lnks"
3+
version = "0.1.0"
4+
description = "A library for reading and writing windows shortcuts (.lnk)."
5+
repository = "https://github.com/OpenByteDev/lnks"
6+
documentation = "https://docs.rs/lnks"
7+
license = "MIT"
8+
authors = ["OpenByte <development.openbyte@gmail.com>"]
9+
edition = "2024"
10+
categories = ["api-bindings", "filesystem", "os::windows-apis"]
11+
keywords = ["lnk", "windows", "shell-link", "parser"]
12+
13+
[dependencies]
14+
windows = { version = "0.62", features = [
15+
"std",
16+
"Win32_System_Com",
17+
"Win32_UI_Shell_Common",
18+
"Win32_Storage_FileSystem",
19+
"Win32_UI_WindowsAndMessaging"
20+
], default-features = false }
21+
widestring = { version = "1.2", features = ["std"], default-features = false }
22+
num_enum = { version = "0.7", default-features = false }
23+
thiserror = { version = "2.0", default-features = false }
24+
enumflags2 = { version = "0.7", default-features = false }
25+
26+
[dev-dependencies]
27+
dunce = { version = "1", default-features = false }
28+
uuid = { version = "1", features = ["v4"], default-features = false }

LICENSE

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) OpenByte <development.openbyte@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# lnks
2+
3+
[![CI](https://github.com/OpenByteDev/lnks/actions/workflows/ci.yml/badge.svg)](https://github.com/OpenByteDev/lnks/actions/workflows/ci.yml) [![crates.io](https://img.shields.io/crates/v/lnks.svg)](https://crates.io/crates/lnks) [![Documentation](https://docs.rs/lnks/badge.svg)](https://docs.rs/lnks) [![dependency status](https://deps.rs/repo/github/openbytedev/lnks/status.svg)](https://deps.rs/repo/github/openbytedev/lnks) [![MIT](https://img.shields.io/crates/l/lnks.svg)](https://github.com/OpenByteDev/lnks/blob/master/LICENSE)
4+
5+
<!-- cargo-sync-readme start -->
6+
7+
A Rust library for hosting the .NET Core runtime.
8+
9+
It utilizes the .NET Core hosting API to load and execute managed code from within the current process.
10+
11+
## Usage
12+
### Running an application
13+
The example below will setup the runtime, load `Test.dll` and run its `Main` method:
14+
```rust
15+
let hostfxr = nethost::load_hostfxr().unwrap();
16+
let context = hostfxr.initialize_for_dotnet_command_line(pdcstr!("Test.dll")).unwrap();
17+
let result = context.run_app().value();
18+
```
19+
The full example can be found in [examples/run-app](https://github.com/OpenByteDev/lnks/tree/master/examples/run-app).
20+
21+
### Calling a managed function
22+
A function pointer to a managed method can be aquired using an [`AssemblyDelegateLoader`](https://docs.rs/lnks/*/lnks/hostfxr/struct.AssemblyDelegateLoader.html).
23+
This is only supported for [`HostfxrContext`'s](https://docs.rs/lnks/*/lnks/hostfxr/struct.HostfxrContext.html) that are initialized using [`Hostfxr::initialize_for_runtime_config`](https://docs.rs/lnks/*/lnks/hostfxr/struct.Hostfxr.html#method.initialize_for_runtime_config). The [`runtimeconfig.json`](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/) is automatically generated for executables, for libraries it is neccessary to add `<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>` to the projects `.csproj` file.
24+
25+
#### Using the default signature
26+
The default method signature is defined as follows:
27+
```csharp
28+
public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes);
29+
```
30+
31+
A method with the default signature (see code below) can be loaded using [`AssemblyDelegateLoader::get_function_with_default_signature`](https://docs.rs/lnks/*/lnks/hostfxr/struct.AssemblyDelegateLoader.html#method.get_function_with_default_signature).
32+
33+
**C#**
34+
```cs
35+
using System;
36+
37+
namespace Test {
38+
public static class Program {
39+
public static int Hello(IntPtr args, int sizeBytes) {
40+
Console.WriteLine("Hello from C#!");
41+
return 42;
42+
}
43+
}
44+
}
45+
```
46+
47+
**Rust**
48+
```rust
49+
let hostfxr = nethost::load_hostfxr().unwrap();
50+
let context =
51+
hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
52+
let fn_loader =
53+
context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
54+
let hello = fn_loader.get_function_with_default_signature(
55+
pdcstr!("Test.Program, Test"),
56+
pdcstr!("Hello"),
57+
).unwrap();
58+
let result = unsafe { hello(std::ptr::null(), 0) };
59+
assert_eq!(result, 42);
60+
```
61+
62+
#### Using UnmanagedCallersOnly
63+
A function pointer to a method annotated with [`UnmanagedCallersOnly`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute) can be loaded without specifying its signature (as these methods cannot be overloaded).
64+
65+
**C#**
66+
```cs
67+
using System;
68+
using System.Runtime.InteropServices;
69+
70+
namespace Test {
71+
public static class Program {
72+
[UnmanagedCallersOnly]
73+
public static void UnmanagedHello() {
74+
Console.WriteLine("Hello from C#!");
75+
}
76+
}
77+
}
78+
```
79+
80+
**Rust**
81+
```rust
82+
let hostfxr = nethost::load_hostfxr().unwrap();
83+
let context =
84+
hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
85+
let fn_loader =
86+
context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
87+
let hello = fn_loader.get_function_with_unmanaged_callers_only::<fn()>(
88+
pdcstr!("Test.Program, Test"),
89+
pdcstr!("UnmanagedHello"),
90+
).unwrap();
91+
hello(); // prints "Hello from C#!"
92+
```
93+
94+
95+
#### Specifying the delegate type
96+
Another option is to define a custom delegate type and passing its assembly qualified name to [`AssemblyDelegateLoader::get_function`](https://docs.rs/lnks/*/lnks/hostfxr/struct.AssemblyDelegateLoader.html#method.get_function).
97+
98+
**C#**
99+
```cs
100+
using System;
101+
102+
namespace Test {
103+
public static class Program {
104+
public delegate void CustomHelloFunc();
105+
106+
public static void CustomHello() {
107+
Console.WriteLine("Hello from C#!");
108+
}
109+
}
110+
}
111+
```
112+
113+
**Rust**
114+
```rust
115+
let hostfxr = nethost::load_hostfxr().unwrap();
116+
let context =
117+
hostfxr.initialize_for_runtime_config(pdcstr!("Test.runtimeconfig.json")).unwrap();
118+
let fn_loader =
119+
context.get_delegate_loader_for_assembly(pdcstr!("Test.dll")).unwrap();
120+
let hello = fn_loader.get_function::<fn()>(
121+
pdcstr!("Test.Program, Test"),
122+
pdcstr!("CustomHello"),
123+
pdcstr!("Test.Program+CustomHelloFunc, Test")
124+
).unwrap();
125+
hello(); // prints "Hello from C#!"
126+
```
127+
128+
The full examples can be found in [examples/call-managed-function](https://github.com/OpenByteDev/lnks/tree/master/examples/call-managed-function).
129+
130+
### Passing complex parameters
131+
Examples for passing non-primitive parameters can be found in [examples/passing-parameters](https://github.com/OpenByteDev/lnks/tree/master/examples/passing-parameters).
132+
133+
## Features
134+
- `nethost` - Links against nethost and allows for automatic detection of the hostfxr library.
135+
- `download-nethost` - Automatically downloads the latest nethost binary from [NuGet](https://www.nuget.org/packages/Microsoft.NETCore.DotNetHost/).
136+
137+
<!-- cargo-sync-readme end -->
138+
139+
140+
## Related crates
141+
- [nethost-sys](https://crates.io/crates/nethost-sys) - bindings for the nethost library.
142+
- [hostfxr-sys](https://crates.io/crates/hostfxr-sys) - bindings for the hostfxr library.
143+
- [coreclr-hosting-shared](https://crates.io/crates/coreclr-hosting-shared) - shared bindings between [hostfxr-sys](https://crates.io/crates/hostfxr-sys) and [nethost-sys](https://crates.io/crates/nethost-sys).
144+
145+
## Additional Information
146+
- [Hosting layer APIs](https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/hosting-layer-apis.md)
147+
- [Native hosting](https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/native-hosting.md#runtime-properties)
148+
- [Write a custom .NET Core host to control the .NET runtime from your native code](https://docs.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting)
149+
150+
## License
151+
Licensed under the MIT license ([LICENSE](https://github.com/OpenByteDev/lnks/blob/master/LICENSE) or http://opensource.org/licenses/MIT)

src/buf_utils.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::Result;
2+
use std::path::PathBuf;
3+
use widestring::U16CString;
4+
use windows::Win32::Foundation::MAX_PATH;
5+
6+
const INITIAL_CAPACITY: usize = MAX_PATH as _;
7+
const MAX_CAPACITY: usize = 4 * 1024;
8+
9+
pub(crate) fn com_get_with_growth(
10+
mut f: impl FnMut(&mut [u16]) -> Result<()>,
11+
) -> Result<U16CString> {
12+
let mut buf = vec![0u16; INITIAL_CAPACITY];
13+
14+
loop {
15+
let res = f(&mut buf);
16+
17+
// COM APIs do not consitently return an error and just truncate the output
18+
// but still include the null terminator.
19+
// Thus we just check if the second to last element is null to guess whether
20+
// the output was truncated.
21+
if buf[0] != 0 && buf[buf.len() - 2] != 0 {
22+
let new_capacity = buf.capacity().saturating_mul(2);
23+
if new_capacity <= MAX_CAPACITY {
24+
buf.resize(new_capacity, 0);
25+
continue;
26+
}
27+
}
28+
29+
return res.map(|()| U16CString::from_vec_truncate(buf));
30+
}
31+
}
32+
33+
pub(crate) fn com_get_string(f: impl FnMut(&mut [u16]) -> Result<()>) -> Result<String> {
34+
com_get_with_growth(f)?
35+
.to_string()
36+
.map_err(crate::Error::Utf16)
37+
}
38+
39+
pub(crate) fn com_get_optional_string(
40+
f: impl FnMut(&mut [u16]) -> Result<()>,
41+
) -> Result<Option<String>> {
42+
let s = com_get_string(f)?;
43+
if s.is_empty() { Ok(None) } else { Ok(Some(s)) }
44+
}
45+
46+
#[allow(dead_code)]
47+
pub(crate) fn com_get_path(f: impl FnMut(&mut [u16]) -> Result<()>) -> Result<PathBuf> {
48+
let str = com_get_with_growth(f)?;
49+
Ok(PathBuf::from(str.to_os_string()))
50+
}
51+
52+
pub(crate) fn com_get_optional_path(
53+
f: impl FnMut(&mut [u16]) -> Result<()>,
54+
) -> Result<Option<PathBuf>> {
55+
let s = com_get_with_growth(f)?;
56+
if s.is_empty() {
57+
Ok(None)
58+
} else {
59+
Ok(Some(PathBuf::from(s.to_os_string())))
60+
}
61+
}

0 commit comments

Comments
 (0)