rustclr 0.3.4

Host CLR and run .NET binaries using Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use alloc::{string::String, vec::Vec};
use core::{
    ffi::c_void,
    ops::Deref,
    ptr::{null, null_mut},
};

use obfstr::obfstr as s;
use windows_core::{GUID, IUnknown, Interface};
use windows_sys::{
    core::{BSTR, HRESULT},
    Win32::{
        Foundation::VARIANT_BOOL,
        System::{
            Com::SAFEARRAY,
            Variant::VARIANT,
            Ole::{
                SafeArrayGetElement, 
                SafeArrayGetLBound, 
                SafeArrayGetUBound
            },
        },
    },
};

use super::{_MethodInfo, _Type};
use crate::string::ComString;
use crate::error::{ClrError, Result};

/// This struct represents the COM `_Assembly` interface.
#[repr(C)]
#[derive(Debug, Clone)]
pub struct _Assembly(windows_core::IUnknown);

impl _Assembly {
    /// Resolves a type by name within the assembly.
    #[inline]
    pub fn resolve_type(&self, name: &str) -> Result<_Type> {
        let type_name = name.to_bstr();
        self.GetType_2(type_name)
    }

    /// Executes the entry point of the assembly.
    ///
    /// The `run` method identifies the main entry point of the assembly and attempts
    /// to invoke it. It distinguishes between `Main()` and `Main(System.String[])` entry points,
    /// allowing optional arguments to be passed when the latter is detected.
    #[inline]
    pub fn run(&self, args: *mut SAFEARRAY) -> Result<VARIANT> {
        let entrypoint = self.get_EntryPoint()?;
        let str = entrypoint.ToString()?;
        match str.as_str() {
            str if str.ends_with(s!("Main()")) => entrypoint.invoke(None, None),
            str if str.ends_with(s!("Main(System.String[])")) => entrypoint.invoke(None, Some(args)),
            _ => Err(ClrError::MethodNotFound),
        }
    }

    /// Creates an instance of a type within the assembly.
    #[inline]
    pub fn create_instance(&self, name: &str) -> Result<VARIANT> {
        let type_name = name.to_bstr();
        self.CreateInstance(type_name)
    }

    /// Retrieves all types within the assembly.
    #[inline]
    pub fn types(&self) -> Result<Vec<String>> {
        let sa_types = self.GetTypes()?;
        if sa_types.is_null() {
            return Err(ClrError::NullPointerError("GetTypes"));
        }

        let mut types = Vec::new();
        let mut lbound = 0;
        let mut ubound = 0;
        unsafe {
            SafeArrayGetLBound(sa_types, 1, &mut lbound);
            SafeArrayGetUBound(sa_types, 1, &mut ubound);

            for i in lbound..=ubound {
                let mut p_type = null_mut::<_Type>();
                let hr = SafeArrayGetElement(sa_types, &i, &mut p_type as *mut _ as *mut _);
                if hr != 0 || p_type.is_null() {
                    return Err(ClrError::ApiError("SafeArrayGetElement", hr));
                }

                let _type = _Type::from_raw(p_type as *mut c_void)?;
                let type_name = _type.ToString()?;
                types.push(type_name);
            }
        }

        Ok(types)
    }

    /// Creates an `_Assembly` instance from a raw COM interface pointer.
    #[inline]
    pub fn from_raw(raw: *mut c_void) -> Result<_Assembly> {
        let iunknown = unsafe { IUnknown::from_raw(raw) };
        iunknown
            .cast::<_Assembly>()
            .map_err(|_| ClrError::CastingError("_Assembly"))
    }

    /// Retrieves the string representation of the assembly.
    #[inline]
    pub fn ToString(&self) -> Result<String> {
        unsafe {
            let mut result = null::<u16>();
            let hr = (Interface::vtable(self).get_ToString)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                let mut len = 0;
                while *result.add(len) != 0 {
                    len += 1;
                }

                let slice = core::slice::from_raw_parts(result, len);
                Ok(String::from_utf16_lossy(slice))
            } else {
                Err(ClrError::ApiError("ToString", hr))
            }
        }
    }

    /// Calls the `GetHashCode` method from the vtable of the `_Assembly` interface.
    #[inline]
    pub fn GetHashCode(&self) -> Result<u32> {
        let mut result = 0;
        let hr =
            unsafe { (Interface::vtable(self).GetHashCode)(Interface::as_raw(self), &mut result) };
        if hr == 0 {
            Ok(result)
        } else {
            Err(ClrError::ApiError("GetHashCode", hr))
        }
    }

    /// Retrieves the entry point method of the assembly.
    #[inline]
    pub fn get_EntryPoint(&self) -> Result<_MethodInfo> {
        let mut result = null_mut();
        let hr = unsafe {
            (Interface::vtable(self).get_EntryPoint)(Interface::as_raw(self), &mut result)
        };
        if hr == 0 {
            _MethodInfo::from_raw(result as *mut c_void)
        } else {
            Err(ClrError::ApiError("get_EntryPoint", hr))
        }
    }

    /// Resolves a specific type by name within the assembly.
    #[inline]
    pub fn GetType_2(&self, name: BSTR) -> Result<_Type> {
        let mut result = null_mut();
        let hr = unsafe {
            (Interface::vtable(self).GetType_2)(Interface::as_raw(self), name, &mut result)
        };
        if hr == 0 {
            _Type::from_raw(result as *mut c_void)
        } else {
            Err(ClrError::ApiError("GetType_2", hr))
        }
    }

    /// Retrieves all types defined within the assembly as a `SAFEARRAY`.
    #[inline]
    pub fn GetTypes(&self) -> Result<*mut SAFEARRAY> {
        let mut result = null_mut();
        let hr =
            unsafe { (Interface::vtable(self).GetTypes)(Interface::as_raw(self), &mut result) };
        if hr == 0 {
            Ok(result)
        } else {
            Err(ClrError::ApiError("GetTypes", hr))
        }
    }

    /// Creates an instance of a type using its name as a `BSTR`.
    #[inline]
    pub fn CreateInstance(&self, typeName: BSTR) -> Result<VARIANT> {
        let mut result = unsafe { core::mem::zeroed::<VARIANT>() };
        let hr = unsafe {
            (Interface::vtable(self).CreateInstance)(Interface::as_raw(self), typeName, &mut result)
        };
        if hr == 0 {
            Ok(result)
        } else {
            Err(ClrError::ApiError("CreateInstance", hr))
        }
    }

    /// Retrieves the main type associated with the assembly.
    #[inline]
    pub fn GetType(&self) -> Result<_Type> {
        let mut result = null_mut();
        let hr = unsafe { (Interface::vtable(self).GetType)(Interface::as_raw(self), &mut result) };
        if hr == 0 {
            _Type::from_raw(result as *mut c_void)
        } else {
            Err(ClrError::ApiError("GetType", hr))
        }
    }

    /// Retrieves the assembly's codebase as a URI.
    #[inline]
    pub fn get_CodeBase(&self) -> Result<String> {
        unsafe {
            let mut result = null::<u16>();
            let hr = (Interface::vtable(self).get_CodeBase)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                let mut len = 0;
                while *result.add(len) != 0 {
                    len += 1;
                }

                let slice = core::slice::from_raw_parts(result, len);
                Ok(String::from_utf16_lossy(slice))
            } else {
                Err(ClrError::ApiError("get_CodeBase", hr))
            }
        }
    }

    /// Retrieves the escaped codebase of the assembly as a URI.
    #[inline]
    pub fn get_EscapedCodeBase(&self) -> Result<String> {
        unsafe {
            let mut result = null::<u16>();
            let hr =
                (Interface::vtable(self).get_EscapedCodeBase)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                let mut len = 0;
                while *result.add(len) != 0 {
                    len += 1;
                }

                let slice = core::slice::from_raw_parts(result, len);
                Ok(String::from_utf16_lossy(slice))
            } else {
                Err(ClrError::ApiError("get_EscapedCodeBase", hr))
            }
        }
    }

    /// Retrieves the name of the assembly.
    #[inline]
    pub fn GetName(&self) -> Result<*mut c_void> {
        unsafe {
            let mut result = null_mut();
            let hr = (Interface::vtable(self).GetName)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                Ok(result)
            } else {
                Err(ClrError::ApiError("GetName", hr))
            }
        }
    }

    /// Retrieves the name of the assembly, with an option to copy the name.
    #[inline]
    pub fn GetName_2(&self, copiedName: VARIANT_BOOL) -> Result<*mut c_void> {
        unsafe {
            let mut result = null_mut();
            let hr = (Interface::vtable(self).GetName_2)(
                Interface::as_raw(self),
                copiedName,
                &mut result,
            );
            if hr == 0 {
                Ok(result)
            } else {
                Err(ClrError::ApiError("GetName_2", hr))
            }
        }
    }

    /// Retrieves the full name of the assembly.
    #[inline]
    pub fn get_FullName(&self) -> Result<String> {
        unsafe {
            let mut result = null::<u16>();
            let hr = (Interface::vtable(self).get_FullName)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                let mut len = 0;
                while *result.add(len) != 0 {
                    len += 1;
                }

                let slice = core::slice::from_raw_parts(result, len);
                Ok(String::from_utf16_lossy(slice))
            } else {
                Err(ClrError::ApiError("get_FullName", hr))
            }
        }
    }

    /// Retrieves the file location of the assembly.
    #[inline]
    pub fn get_Location(&self) -> Result<String> {
        unsafe {
            let mut result = null::<u16>();
            let hr = (Interface::vtable(self).get_Location)(Interface::as_raw(self), &mut result);
            if hr == 0 {
                let mut len = 0;
                while *result.add(len) != 0 {
                    len += 1;
                }

                let slice = core::slice::from_raw_parts(result, len);
                Ok(String::from_utf16_lossy(slice))
            } else {
                Err(ClrError::ApiError("get_Location", hr))
            }
        }
    }
}

unsafe impl Interface for _Assembly {
    type Vtable = _Assembly_Vtbl;

    /// The interface identifier (IID) for the `_Assembly` COM interface.
    ///
    /// This GUID is used to identify the `_Assembly` interface when calling
    /// COM methods like `QueryInterface`. It is defined based on the standard
    /// .NET CLR IID for the `_Assembly` interface.
    const IID: GUID = GUID::from_u128(0x17156360_2f1a_384a_bc52_fde93c215c5b);
}

impl Deref for _Assembly {
    type Target = windows_core::IUnknown;

    /// Provides a reference to the underlying `IUnknown` interface.
    ///
    /// This implementation allows `_Assembly` to be used as an `IUnknown`
    /// pointer, enabling access to basic COM methods like `AddRef`, `Release`,
    /// and `QueryInterface`.
    fn deref(&self) -> &Self::Target {
        unsafe { core::mem::transmute(self) }
    }
}

/// Raw COM vtable for the `_Assembly` interface.
#[repr(C)]
pub struct _Assembly_Vtbl {
    base__: windows_core::IUnknown_Vtbl,

    // IDispatch methods
    GetTypeInfoCount: *const c_void,
    GetTypeInfo: *const c_void,
    GetIDsOfNames: *const c_void,
    Invoke: *const c_void,

    // Methods specific to the COM interface
    get_ToString: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut BSTR) -> HRESULT,
    Equals: *const c_void,
    GetHashCode: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut u32) -> HRESULT,
    GetType: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut *mut _Type) -> HRESULT,
    get_CodeBase: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut BSTR) -> HRESULT,
    get_EscapedCodeBase: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut BSTR) -> HRESULT,
    GetName: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut *mut c_void) -> HRESULT,
    GetName_2: unsafe extern "system" fn(
        this: *mut c_void,
        copiedName: VARIANT_BOOL,
        pRetVal: *mut *mut c_void,
    ) -> HRESULT,
    get_FullName: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut BSTR) -> HRESULT,
    get_EntryPoint: unsafe extern "system" fn(
        this: *mut c_void, 
        pRetVal: *mut *mut _MethodInfo
    ) -> HRESULT,
    GetType_2: unsafe extern "system" fn(
        this: *mut c_void,
        name: BSTR,
        pRetVal: *mut *mut _Type,
    ) -> HRESULT,
    GetType_3: *const c_void,
    GetExportedTypes: *const c_void,
    GetTypes: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut *mut SAFEARRAY) -> HRESULT,
    GetManifestResourceStream: *const c_void,
    GetManifestResourceStream_2: *const c_void,
    GetFile: *const c_void,
    GetFiles: *const c_void,
    GetFiles_2: *const c_void,
    GetManifestResourceNames: *const c_void,
    GetManifestResourceInfo: *const c_void,
    get_Location: unsafe extern "system" fn(this: *mut c_void, pRetVal: *mut BSTR) -> HRESULT,
    get_Evidence: *const c_void,
    GetCustomAttributes: *const c_void,
    GetCustomAttributes_2: *const c_void,
    IsDefined: *const c_void,
    GetObjectData: *const c_void,
    add_ModuleResolve: *const c_void,
    remove_ModuleResolve: *const c_void,
    GetType_4: *const c_void,
    GetSatelliteAssembly: *const c_void,
    GetSatelliteAssembly_2: *const c_void,
    LoadModule: *const c_void,
    LoadModule_2: *const c_void,
    CreateInstance: unsafe extern "system" fn(
        this: *mut c_void,
        typeName: BSTR,
        pRetVal: *mut VARIANT,
    ) -> HRESULT,
    CreateInstance_2: *const c_void,
    CreateInstance_3: *const c_void,
    GetLoadedModules: *const c_void,
    GetLoadedModules_2: *const c_void,
    GetModules: *const c_void,
    GetModules_2: *const c_void,
    GetModule: *const c_void,
    GetReferencedAssemblies: *const c_void,
    get_GlobalAssemblyCache: *const c_void,
}