virtualbox_rs 0.4.2

A Rust library for interacting with VirtualBox, providing a safe and idiomatic interface to the VirtualBox API.
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
use crate::enums::{
    AdditionsFacilityStatus, AdditionsFacilityType, AdditionsRunLevelType, AdditionsUpdateFlag,
    GuestShutdownFlag,
};
use crate::utility::macros::macros::{
    get_function_result_bool, get_function_result_number, get_function_result_pointer,
    get_function_result_str, get_function_result_unit,
};
use crate::utility::string_to_c_u64_str;
use crate::{EventSource, Guest, GuestInternalGetStatistics, GuestSession, Progress, VboxError};
use vbox_raw::sys_lib::{IEventSource, IGuestSession, IProgress, PRUint32};

impl Guest {
    /// Identifier of the Guest OS type as reported by the Guest Additions.
    ///
    /// # Returns
    ///
    /// Returns &str on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let os_type_id = guest.get_os_type_id().unwrap();
    pub fn get_os_type_id(&self) -> Result<&'static str, VboxError> {
        get_function_result_str!(self.object, GetOSTypeId)
    }

    /// Current run level of the installed Guest Additions.
    ///
    /// # Returns
    ///
    /// Returns a [`AdditionsRunLevelType`] on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let additions_run_level = guest.get_additions_run_level().unwrap();
    pub fn get_additions_run_level(&self) -> Result<AdditionsRunLevelType, VboxError> {
        let additions_run_level =
            get_function_result_number!(self.object, GetAdditionsRunLevel, u32)?;
        Ok(AdditionsRunLevelType::from(additions_run_level))
    }

    /// Version of the installed Guest Additions in the same format as IVirtualBox::version.
    ///
    /// # Returns
    ///
    /// Returns &str on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let additions_version = guest.get_additions_version().unwrap();
    pub fn get_additions_version(&self) -> Result<&'static str, VboxError> {
        get_function_result_str!(self.object, GetAdditionsVersion)
    }

    /// The internal build revision number of the installed Guest Additions.
    ///
    /// # Returns
    ///
    /// Returns u32 on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let additions_revision = guest.get_additions_revision().unwrap();
    pub fn get_additions_revision(&self) -> Result<u32, VboxError> {
        get_function_result_number!(self.object, GetAdditionsRevision, u32)
    }
    // TODO GetDnDTarget

    /// Event source for guest events.
    ///
    /// # Returns
    ///
    /// Returns [`EventSource`] class on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let event_source = guest.get_event_source().unwrap();
    pub fn get_event_source(&self) -> Result<EventSource, VboxError> {
        let event_source =
            get_function_result_pointer!(self.object, GetEventSource, *mut IEventSource)?;
        Ok(EventSource::new(event_source))
    }

    /// Returns a collection of all opened guest sessions.
    ///
    /// # Returns
    ///
    /// Returns [`Vec<GuestSession>`]  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let guest_sessions = guest.get_sessions().unwrap();
    pub fn get_sessions(&self) -> Result<Vec<GuestSession>, VboxError> {
        let mut count = 0;
        let sessions_ptr = get_function_result_pointer!(
            self.object,
            GetSessions,
            *mut *mut IGuestSession,
            &mut count
        )?;
        let sessions_ptr_vec =
            unsafe { Vec::from_raw_parts(sessions_ptr, count as usize, count as usize) };

        let mut sessions = Vec::new();
        for session_ptr in sessions_ptr_vec {
            sessions.push(GuestSession::new(session_ptr));
        }
        Ok(sessions)
    }

    // TODO GetFacilities
    // TODO GetDnDSource

    /// Guest system memory balloon size in megabytes (transient property).
    ///
    /// # Returns
    ///
    /// Returns u32  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let memory_balloon_size = guest.get_memory_balloon_size().unwrap();
    pub fn get_memory_balloon_size(&self) -> Result<u32, VboxError> {
        get_function_result_number!(self.object, GetMemoryBalloonSize, u32)
    }

    /// Guest system memory balloon size in megabytes (transient property).
    ///
    /// # Arguments
    ///
    /// * `memory_balloon_size` - u32
    ///
    /// # Returns
    ///
    /// Returns ()  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// guest.set_memory_balloon_size(16).unwrap();
    pub fn set_memory_balloon_size(&self, memory_balloon_size: u32) -> Result<(), VboxError> {
        get_function_result_unit!(self.object, SetMemoryBalloonSize, memory_balloon_size)
    }

    /// Interval to update guest statistics in seconds.
    ///
    /// # Returns
    ///
    /// Returns u32  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let statistics_update_interval = guest.get_statistics_update_interval().unwrap();
    pub fn get_statistics_update_interval(&self) -> Result<u32, VboxError> {
        get_function_result_number!(self.object, GetStatisticsUpdateInterval, u32)
    }

    /// Interval to update guest statistics in seconds.
    ///
    /// # Arguments
    ///
    /// * `statistics_update_interval` - u32
    ///
    /// # Returns
    ///
    /// Returns ()  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// guest.set_statistics_update_interval(1).unwrap();
    pub fn set_statistics_update_interval(
        &self,
        statistics_update_interval: u32,
    ) -> Result<(), VboxError> {
        get_function_result_unit!(
            self.object,
            SetStatisticsUpdateInterval,
            statistics_update_interval
        )
    }

    /// <div class="warning">
    ///     Internal method; do not use as it might change at any time.
    /// </div>
    ///
    /// # Returns
    ///
    /// Returns [`GuestInternalGetStatistics`]  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let internal_get_statistics = guest.internal_get_statistics().unwrap();
    pub fn internal_get_statistics(&self) -> Result<GuestInternalGetStatistics, VboxError> {
        let mut cpu_user: u32 = 0;
        let mut cpu_kernel: u32 = 0;
        let mut cpu_idle: u32 = 0;
        let mut mem_total: u32 = 0;
        let mut mem_free: u32 = 0;
        let mut mem_balloon: u32 = 0;
        let mut mem_shared: u32 = 0;
        let mut mem_cache: u32 = 0;
        let mut paged_total: u32 = 0;
        let mut mem_alloc_total: u32 = 0;
        let mut mem_free_total: u32 = 0;
        let mut mem_balloon_total: u32 = 0;
        let mut mem_shared_total: u32 = 0;
        get_function_result_unit!(
            self.object,
            InternalGetStatistics,
            &mut cpu_user,
            &mut cpu_kernel,
            &mut cpu_idle,
            &mut mem_total,
            &mut mem_free,
            &mut mem_balloon,
            &mut mem_shared,
            &mut mem_cache,
            &mut paged_total,
            &mut mem_alloc_total,
            &mut mem_free_total,
            &mut mem_balloon_total,
            &mut mem_shared_total
        )?;
        Ok(GuestInternalGetStatistics {
            cpu_user,
            cpu_kernel,
            cpu_idle,
            mem_total,
            mem_free,
            mem_balloon,
            mem_shared,
            mem_cache,
            paged_total,
            mem_alloc_total,
            mem_free_total,
            mem_balloon_total,
            mem_shared_total,
        })
    }

    /// Get the current status of a Guest Additions facility.
    ///
    ///
    /// # Arguments
    ///
    /// * `facility` - AdditionsFacilityType. Facility to check status for.
    ///
    /// # Returns
    ///
    /// Returns (i64, [`AdditionsFacilityStatus`]) ([Timestamp (in ms) of last status update seen by the host.], [The current (latest) facility status.]) on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::{AdditionsFacilityType, SessionType};
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let facility_status = guest.get_facility_status(AdditionsFacilityType::All).unwrap();
    pub fn get_facility_status(
        &self,
        facility: AdditionsFacilityType,
    ) -> Result<(i64, AdditionsFacilityStatus), VboxError> {
        let mut timestamp: i64 = 0;
        let mut status: u32 = 0;
        let facility = facility.into();
        get_function_result_unit!(
            self.object,
            GetFacilityStatus,
            facility,
            &mut timestamp,
            &mut status
        )?;
        let facility_status = AdditionsFacilityStatus::from(facility);
        Ok((timestamp, facility_status))
    }

    /// Retrieve the current status of a certain Guest Additions run level.
    ///
    ///
    /// # Arguments
    ///
    /// * `level` - AdditionsRunLevelType. Status level to check
    ///
    /// # Returns
    ///
    /// Returns bool  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::{AdditionsRunLevelType, SessionType};
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let additions_status = guest.get_additions_status(AdditionsRunLevelType::Desktop).unwrap();

    pub fn get_additions_status(&self, level: AdditionsRunLevelType) -> Result<bool, VboxError> {
        let level = level.into();
        get_function_result_bool!(self.object, GetAdditionsStatus, level)
    }

    /// Store login credentials that can be queried by guest operating systems with Additions installed.
    ///
    /// The credentials are transient to the session and the guest may also choose to erase them. Note that the caller cannot determine whether the guest operating system has queried or made use of the credentials.    ///
    /// # Arguments
    ///
    /// * `user_name` -  &str. User name string, can be empty.
    /// * `password` -  &str. Password string, can be empty
    /// * `domain` -  &str. Domain name (guest logon scheme specific), can be empty
    /// * `allow_interactive_logon` - bool. Flag whether the guest should alternatively allow the user to interactively specify different credentials. This flag might not be supported by all versions of the Additions.
    ///
    /// # Returns
    ///
    /// Returns ()  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// guest.set_credentials(
    ///     "user",
    ///     "pass",
    ///     "domain",
    ///     true).unwrap();
    pub fn set_credentials(
        &self,
        user_name: &str,
        password: &str,
        domain: &str,
        allow_interactive_logon: bool,
    ) -> Result<(), VboxError> {
        let user_name_ptr = string_to_c_u64_str(user_name)?;
        let password_ptr = string_to_c_u64_str(password)?;
        let domain_ptr = string_to_c_u64_str(domain)?;
        let allow_interactive_logon = if allow_interactive_logon { 1 } else { 0 };
        get_function_result_unit!(
            self.object,
            SetCredentials,
            user_name_ptr,
            password_ptr,
            domain_ptr,
            allow_interactive_logon
        )
    }

    /// Creates a new guest session for controlling the guest.
    ///
    /// The new session will be started asynchronously, meaning on return of this function it is not guaranteed that the guest session is in a started and/or usable state. To wait for successful startup, use the [`GuestSession::wait_for`] call.
    ///
    /// A guest session represents one impersonated user account in the guest, so every operation will use the same credentials specified when creating the session object via [`Guest::create_session`]. Anonymous sessions, that is, sessions without specifying a valid user account in the guest are not allowed reasons of security.
    ///
    /// There can be a maximum of 32 sessions at once per VM. An error will be returned if this has been reached.
    ///
    /// For more information please consult [`GuestSession`]
    ///
    /// # Arguments
    ///
    /// * `user_name` -  &str. User name this session will be using to control the guest; has to exist and have the appropriate rights to execute programs in the VM. Must not be empty.
    /// * `password` -  &str. Password of the user account to be used. Empty passwords are allowed.
    /// * `domain` -  &str. Domain name of the user account to be used if the guest is part of a domain. Optional. This feature is not implemented yet.
    /// * `session_name` - &str. The session's friendly name. Optional, can be empty.
    ///
    /// # Returns
    ///
    /// Returns GuestSession  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let session = guest.create_session(
    ///     "user",
    ///     "pass",
    ///     "domain",
    ///     "s1").unwrap();
    pub fn create_session(
        &self,
        user_name: &str,
        password: &str,
        domain: &str,
        session_name: &str,
    ) -> Result<GuestSession, VboxError> {
        let user_name_ptr = string_to_c_u64_str(user_name)?;
        let password_ptr = string_to_c_u64_str(password)?;
        let domain_ptr = string_to_c_u64_str(domain)?;
        let session_name_ptr = string_to_c_u64_str(session_name)?;

        let session = get_function_result_pointer!(
            self.object,
            CreateSession,
            *mut IGuestSession,
            user_name_ptr,
            password_ptr,
            domain_ptr,
            session_name_ptr
        )?;
        Ok(GuestSession::new(session))
    }
    /// Finds guest sessions by their friendly name and returns an interface array with all found guest sessions.
    ///
    /// # Arguments
    ///
    /// * `session_name` -  &str. The session's friendly name to find. Wildcards like ? and * are allowed.
    ///
    /// # Returns
    ///
    /// Returns [`Vec<GuestSession>`]  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let session = guest.create_session(
    ///     "user",
    ///     "pass",
    ///     "domain",
    ///     "s1").unwrap();
    pub fn find_session(&self, session_name: &str) -> Result<Vec<GuestSession>, VboxError> {
        let session_name_ptr = string_to_c_u64_str(session_name)?;
        let mut count = 0;
        let sessions_ptr = get_function_result_pointer!(
            self.object,
            FindSession,
            *mut *mut IGuestSession,
            session_name_ptr,
            &mut count
        )?;
        let sessions_ptr_vec =
            unsafe { Vec::from_raw_parts(sessions_ptr, count as usize, count as usize) };

        let mut sessions = Vec::new();
        for session_ptr in sessions_ptr_vec {
            sessions.push(GuestSession::new(session_ptr));
        }
        Ok(sessions)
    }

    #[cfg(not(is_v_6_1))]
    /// Shuts down (and optionally halts and/or reboots) the guest.
    ///
    /// Needs supported Guest Additions installed.
    ///
    /// # Returns
    ///
    /// Returns bool  on success, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    /// use std::vec;
    /// use virtualbox_rs::{ Session, VirtualBox};
    /// use virtualbox_rs::enums::{GuestShutdownFlag, SessionType};
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// guest.shutdown(vec![GuestShutdownFlag::PowerOff]).unwrap();
    pub fn shutdown(&self, flags: Vec<GuestShutdownFlag>) -> Result<(), VboxError> {
        let mut flags: Vec<u32> = flags.iter().map(|flag| (*flag).into()).collect();
        let flags_ptr = flags.as_mut_ptr();
        let flags_size = flags.len() as PRUint32;
        get_function_result_unit!(self.object, Shutdown, flags_size, flags_ptr)
    }

    ///Automatically updates already installed Guest Additions in a VM.
    ///
    /// At the moment only Windows and Linux guests are supported.
    ///
    /// Because the VirtualBox Guest Additions drivers are not WHQL-certified yet there might be warning dialogs during the actual Guest Additions update. These need to be confirmed manually in order to continue the installation process. This applies to Windows 2000 and Windows XP guests and therefore these guests can't be updated in a fully automated fashion without user interaction. However, to start a Guest Additions update for the mentioned Windows versions anyway, the flag [`AdditionsUpdateFlag::WaitForUpdateStartOnly`] can be specified. See [`AdditionsUpdateFlag`] for more information.
    ///
    /// The guest needs to be restarted in order to make use of the updated Guest Additions.
    ///
    ///
    /// # Arguments
    ///
    /// * `source` -  &str.
    /// * `arguments` -  Vec<&str>.
    /// * `flags` -  [`Vec<AdditionsUpdateFlag>`]
    ///
    /// # Returns
    ///
    /// Progress object to track the operation completion, or a [`VboxError`] on failure.
    ///
    ///  # Example
    ///
    /// ```no_run
    ///
    ///
    /// use virtualbox_rs::{Session, VirtualBox};
    /// use virtualbox_rs::enums::SessionType;
    ///
    /// let vbox = VirtualBox::init().unwrap();
    /// let mut session = Session::init().unwrap();
    /// let machine = vbox.
    ///         find_machines("Freebsd_14").unwrap();
    /// machine.lock_machine(&mut session, SessionType::Shared).unwrap();
    ///
    /// let console = session.get_console().unwrap();
    ///
    /// let guest = console.get_guest().unwrap();
    /// let progress = guest.update_guest_additions(
    ///     "https://example.com",
    ///     Vec::new(),
    ///     Vec::new()
    /// ).unwrap();

    pub fn update_guest_additions(
        &self,
        source: &str,
        arguments: Vec<&str>,
        flags: Vec<AdditionsUpdateFlag>,
    ) -> Result<Progress, VboxError> {
        let source_ptr = string_to_c_u64_str(source)?;

        let mut arguments_ptr_vec = Vec::new();
        for argument in &arguments {
            arguments_ptr_vec.push(string_to_c_u64_str(argument)?);
        }
        let arguments_ptr = arguments_ptr_vec.as_mut_ptr();
        let arguments_size = arguments.len() as PRUint32;

        let mut flags: Vec<u32> = flags.iter().map(|flag| (*flag).into()).collect();
        let flags_ptr = flags.as_mut_ptr();
        let flags_size = flags.len() as PRUint32;

        let progress = get_function_result_pointer!(
            self.object,
            UpdateGuestAdditions,
            *mut IProgress,
            source_ptr,
            arguments_size,
            arguments_ptr,
            flags_size,
            flags_ptr
        )?;
        Ok(Progress::new(progress))
    }
}

#[cfg(is_v_6_1)]
impl Guest {
    /// Placeholder Method
    ///
    /// This method serves as a placeholder for versions of the API where the actual method is not available.
    /// Attempting to call this method will result in an `UnsupportedInCurrentApiVersion` error.
    /// This method is intended to ensure that the codebase can be compiled against multiple API versions
    /// without modification.
    ///
    /// Supported from API version: v7_0
    pub fn shutdown(&self, _flags: Vec<GuestShutdownFlag>) -> Result<(), VboxError> {
        Err(VboxError::unsupported_in_current_api_version(
            "Guest::shutdown",
            "v7_0",
        ))
    }
}