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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use alloc::{string::String, vec::Vec};
use core::{
    ffi::c_void,
    ops::{BitOr, Deref},
    ptr::{null, null_mut},
};

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

use crate::Invocation;
use crate::string::ComString;
use crate::variant::create_safe_args;
use crate::error::{ClrError, Result};
use crate::com::{_MethodInfo, _PropertyInfo};

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

impl _Type {
    /// Retrieves a method by its name from the type.
    #[inline]
    pub fn method(&self, name: &str) -> Result<_MethodInfo> {
        let method_name = name.to_bstr();
        self.GetMethod_6(method_name)
    }

    /// Finds a method by signature from the type.
    #[inline]
    pub fn method_signature(&self, name: &str) -> Result<_MethodInfo> {
        let methods = self.methods();
        if let Ok(methods) = methods {
            for (method_name, method_info) in methods {
                if method_name == name {
                    return Ok(method_info);
                }
            }
        }

        Err(ClrError::MethodNotFound)
    }

    /// Finds a property by signature from the type.
    #[inline]
    pub fn property_signature(&self, name: &str) -> Result<_PropertyInfo> {
        let properties = self.properties();
        if let Ok(properties) = properties {
            for (property_name, property_info) in properties {
                if property_name == name {
                    return Ok(property_info);
                }
            }
        }

        Err(ClrError::PropertyNotFound)
    }

    /// Retrieves a property by name from the type.
    #[inline]
    pub fn property(&self, name: &str) -> Result<_PropertyInfo> {
        unsafe {
            let binding_flags = BindingFlags::Public
                | BindingFlags::Instance
                | BindingFlags::Static
                | BindingFlags::FlattenHierarchy
                | BindingFlags::NonPublic;

            let property_name = name.to_bstr();
            let mut result = null_mut();
            let hr = (Interface::vtable(self).GetProperty)(
                Interface::as_raw(self),
                property_name,
                binding_flags,
                &mut result,
            );

            if hr == 0 && !result.is_null() {
                Ok(_PropertyInfo::from_raw(result)?)
            } else {
                Err(ClrError::ApiError("GetProperty", hr))
            }
        }
    }

    /// Invokes a method on the type.
    #[inline]
    pub fn invoke(
        &self,
        name: &str,
        instance: Option<VARIANT>,
        args: Option<Vec<VARIANT>>,
        invocation_type: Invocation,
    ) -> Result<VARIANT> {
        let flags = match invocation_type {
            Invocation::Static => {
                BindingFlags::NonPublic
                    | BindingFlags::Public
                    | BindingFlags::Static
                    | BindingFlags::InvokeMethod
            }
            Invocation::Instance => {
                BindingFlags::NonPublic
                    | BindingFlags::Public
                    | BindingFlags::Instance
                    | BindingFlags::InvokeMethod
            }
        };

        let method_name = name.to_bstr();
        let args = args
            .as_ref()
            .map_or_else(|| Ok(null_mut()), |args| create_safe_args(args.to_vec()))?;

        let instance = instance.unwrap_or(unsafe { core::mem::zeroed::<VARIANT>() });
        self.InvokeMember_3(method_name, flags, instance, args)
    }

    /// Retrieves all methods of the type.
    #[inline]
    pub fn methods(&self) -> Result<Vec<(String, _MethodInfo)>> {
        let binding_flags = BindingFlags::Public
            | BindingFlags::Instance
            | BindingFlags::Static
            | BindingFlags::FlattenHierarchy
            | BindingFlags::NonPublic;

        let sa_methods = self.GetMethods(binding_flags)?;
        if sa_methods.is_null() {
            return Err(ClrError::NullPointerError("GetMethods"));
        }

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

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

                let method = _MethodInfo::from_raw(p_method as *mut c_void)?;
                let method_name = method.ToString()?;
                methods.push((method_name, method));
            }
        }

        Ok(methods)
    }

    /// Retrieves all properties of the type.
    #[inline]
    pub fn properties(&self) -> Result<Vec<(String, _PropertyInfo)>> {
        let binding_flags = BindingFlags::Public
            | BindingFlags::Instance
            | BindingFlags::Static
            | BindingFlags::FlattenHierarchy
            | BindingFlags::NonPublic;

        let sa_properties = self.GetProperties(binding_flags)?;
        if sa_properties.is_null() {
            return Err(ClrError::NullPointerError("GetProperties"));
        }

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

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

                let property = _PropertyInfo::from_raw(p_property as *mut c_void)?;
                let name = property.ToString()?;
                properties.push((name, property));
            }
        }

        Ok(properties)
    }

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

    /// Retrieves the string representation of the type.
    #[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))
            }
        }
    }

    /// Retrieves all properties matching the specified `BindingFlags`.
    #[inline]
    pub fn GetProperties(&self, bindingAttr: BindingFlags) -> Result<*mut SAFEARRAY> {
        unsafe {
            let mut result = null_mut();
            let hr = (Interface::vtable(self).GetProperties)(
                Interface::as_raw(self),
                bindingAttr,
                &mut result,
            );

            if hr == 0 {
                Ok(result)
            } else {
                Err(ClrError::ApiError("GetProperties", hr))
            }
        }
    }

    /// Retrieves all methods matching the specified `BindingFlags`.
    #[inline]
    pub fn GetMethods(&self, bindingAttr: BindingFlags) -> Result<*mut SAFEARRAY> {
        unsafe {
            let mut result = null_mut();
            let hr = (Interface::vtable(self).GetMethods)(
                Interface::as_raw(self),
                bindingAttr,
                &mut result,
            );
            if hr == 0 {
                Ok(result)
            } else {
                Err(ClrError::ApiError("GetMethods", hr))
            }
        }
    }

    /// Retrieves a method by name.
    #[inline]
    pub fn GetMethod_6(&self, name: BSTR) -> Result<_MethodInfo> {
        unsafe {
            let mut result = core::mem::zeroed();
            let hr = (Interface::vtable(self).GetMethod_6)(Interface::as_raw(self), name, &mut result);
            if hr == 0 {
                _MethodInfo::from_raw(result as *mut c_void)
            } else {
                Err(ClrError::ApiError("GetMethod_6", hr))
            }
        }
    }

    /// Invokes a method (static or instance) by name on the specified type or object.
    #[inline]
    pub fn InvokeMember_3(
        &self,
        name: BSTR,
        invoke_attr: BindingFlags,
        instance: VARIANT,
        args: *mut SAFEARRAY,
    ) -> Result<VARIANT> {
        unsafe {
            let mut result = core::mem::zeroed();
            let hr = (Interface::vtable(self).InvokeMember_3)(
                Interface::as_raw(self),
                name,
                invoke_attr,
                null_mut(),
                instance,
                args,
                &mut result,
            );
            if hr == 0 {
                Ok(result)
            } else {
                Err(ClrError::ApiError("InvokeMember_3", hr))
            }
        }
    }
}

unsafe impl Interface for _Type {
    type Vtable = _Type_Vtbl;

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

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

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

/// Specifies flags that control binding and the way in which members are searched and invoked.
#[repr(C)]
pub enum BindingFlags {
    /// Default binding, no special options.
    Default = 0,

    /// Ignores case when looking up members.
    IgnoreCase = 1,

    /// Only members declared at the level of the supplied type's hierarchy should be considered.
    DeclaredOnly = 2,

    /// Specifies instance members.
    Instance = 4,

    /// Specifies static members.
    Static = 8,

    /// Specifies public members.
    Public = 16,

    /// Specifies non-public members.
    NonPublic = 32,

    /// Includes inherited members in the search.
    FlattenHierarchy = 64,

    /// Specifies that the member to invoke is a method.
    InvokeMethod = 256,

    /// Creates an instance of the object.
    CreateInstance = 512,

    /// Specifies that the member to retrieve is a field.
    GetField = 1024,

    /// Specifies that the member to set is a field.
    SetField = 2048,

    /// Specifies that the member to retrieve is a property.
    GetProperty = 4096,

    /// Specifies that the member to set is a property.
    SetProperty = 8192,

    /// Sets a COM object property.
    PutDispProperty = 16384,

    /// Sets a COM object reference property.
    PutRefDispProperty = 32768,

    /// Uses the most precise match during binding.
    ExactBinding = 65536,

    /// Suppresses coercion of argument types during method invocation.
    SuppressChangeType = 131072,

    /// Allows binding to optional parameters.
    OptionalParamBinding = 262144,

    /// Ignores the return value of a method.
    IgnoreReturn = 16777216,
}

impl BitOr for BindingFlags {
    type Output = Self;

    /// Enables combining multiple `BindingFlags` using bitwise OR.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let flags = BindingFlags::Public | BindingFlags::Instance;
    /// ```
    fn bitor(self, rhs: Self) -> Self::Output {
        unsafe { core::mem::transmute::<u32, BindingFlags>(self as u32 | rhs as u32) }
    }
}

/// Raw COM vtable for the `_Type` interface.
#[repr(C)]
pub struct _Type_Vtbl {
    pub 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: *const c_void,
    GetType: *const c_void,
    get_MemberType: *const c_void,
    get_name: *const c_void,
    get_DeclaringType: *const c_void,
    get_ReflectedType: *const c_void,
    GetCustomAttributes: *const c_void,
    GetCustomAttributes_2: *const c_void,
    IsDefined: *const c_void,
    get_Guid: *const c_void,
    get_Module: *const c_void,
    get_Assembly: *const c_void,
    get_TypeHandle: *const c_void,
    get_FullName: *const c_void,
    get_Namespace: *const c_void,
    get_AssemblyQualifiedName: *const c_void,
    GetArrayRank: *const c_void,
    get_BaseType: *const c_void,
    GetConstructors: *const c_void,
    GetInterface: *const c_void,
    GetInterfaces: *const c_void,
    FindInterfaces: *const c_void,
    GetEvent: *const c_void,
    GetEvents: *const c_void,
    GetEvents_2: *const c_void,
    GetNestedTypes: *const c_void,
    GetNestedType: *const c_void,
    GetMember: *const c_void,
    GetDefaultMembers: *const c_void,
    FindMembers: *const c_void,
    GetElementType: *const c_void,
    IsSubclassOf: *const c_void,
    IsInstanceOfType: *const c_void,
    IsAssignableFrom: *const c_void,
    GetInterfaceMap: *const c_void,
    GetMethod: *const c_void,
    GetMethod_2: *const c_void,
    GetMethods: unsafe extern "system" fn(
        this: *mut c_void,
        bindingAttr: BindingFlags,
        pRetVal: *mut *mut SAFEARRAY,
    ) -> HRESULT,
    GetField: *const c_void,
    GetFields: *const c_void,
    pub GetProperty: unsafe extern "system" fn(
        this: *mut c_void,
        name: BSTR,
        bindingAttr: BindingFlags,
        result: *mut *mut c_void,
    ) -> HRESULT,
    GetProperty_2: *const c_void,
    GetProperties: unsafe extern "system" fn(
        this: *mut c_void,
        bindingAttr: BindingFlags,
        pRetVal: *mut *mut SAFEARRAY,
    ) -> HRESULT,
    GetMember_2: *const c_void,
    GetMembers: *const c_void,
    InvokeMember: *const c_void,
    get_UnderlyingSystemType: *const c_void,
    InvokeMember_2: *const c_void,
    InvokeMember_3: unsafe extern "system" fn(
        this: *mut c_void,
        name: BSTR,
        invokeAttr: BindingFlags,
        Binder: *mut c_void,
        Target: VARIANT,
        args: *mut SAFEARRAY,
        pRetVal: *mut VARIANT,
    ) -> HRESULT,
    GetConstructor: *const c_void,
    GetConstructor_2: *const c_void,
    GetConstructor_3: *const c_void,
    GetConstructors_2: *const c_void,
    get_TypeInitializer: *const c_void,
    GetMethod_3: *const c_void,
    GetMethod_4: *const c_void,
    GetMethod_5: *const c_void,
    GetMethod_6: unsafe extern "system" fn(
        this: *mut c_void,
        name: BSTR,
        pRetVal: *mut *mut _MethodInfo,
    ) -> HRESULT,
    GetMethods_2: *const c_void,
    GetField_2: *const c_void,
    GetFields_2: *const c_void,
    GetInterface_2: *const c_void,
    GetEvent_2: *const c_void,
    GetProperty_3: *const c_void,
    GetProperty_4: *const c_void,
    GetProperty_5: *const c_void,
    GetProperty_6: *const c_void,
    GetProperty_7: *const c_void,
    GetProperties_2: *const c_void,
    GetNestedTypes_2: *const c_void,
    GetNestedType_2: *const c_void,
    GetMember_3: *const c_void,
    GetMembers_2: *const c_void,
    get_Attributes: *const c_void,
    get_IsNotPublic: *const c_void,
    get_IsPublic: *const c_void,
    get_IsNestedPublic: *const c_void,
    get_IsNestedPrivate: *const c_void,
    get_IsNestedFamily: *const c_void,
    get_IsNestedAssembly: *const c_void,
    get_IsNestedFamANDAssem: *const c_void,
    get_IsNestedFamORAssem: *const c_void,
    get_IsAutoLayout: *const c_void,
    get_IsLayoutSequential: *const c_void,
    get_IsExplicitLayout: *const c_void,
    get_IsClass: *const c_void,
    get_IsInterface: *const c_void,
    get_IsValueType: *const c_void,
    get_IsAbstract: *const c_void,
    get_IsSealed: *const c_void,
    get_IsEnum: *const c_void,
    get_IsSpecialName: *const c_void,
    get_IsImport: *const c_void,
    get_IsSerializable: *const c_void,
    get_IsAnsiClass: *const c_void,
    get_IsUnicodeClass: *const c_void,
    get_IsArray: *const c_void,
    get_IsByRef: *const c_void,
    get_IsPointer: *const c_void,
    get_IsPrimitive: *const c_void,
    get_IsCOMObject: *const c_void,
    get_HasElementType: *const c_void,
    get_IsContextful: *const c_void,
    get_IsMarshalByRef: *const c_void,
    Equals_2: *const c_void,
}