aboutsummaryrefslogtreecommitdiff
path: root/src/dtf/WixToolset.Dtf.WindowsInstaller/RemotableNativeMethods.cs
blob: 960fd15f7444ac96c495c802695bcb4459e4f7be (plain)
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Dtf.WindowsInstaller
{
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Assigns ID numbers to the MSI APIs that are remotable.
    /// </summary>
    /// <remarks><p>
    /// This enumeration MUST stay in sync with the
    /// unmanaged equivalent in RemoteMsiSession.h!
    /// </p></remarks>
    internal enum RemoteMsiFunctionId
    {
        EndSession = 0,
        MsiCloseHandle,
        MsiCreateRecord,
        MsiDatabaseGetPrimaryKeys,
        MsiDatabaseIsTablePersistent,
        MsiDatabaseOpenView,
        MsiDoAction,
        MsiEnumComponentCosts,
        MsiEvaluateCondition,
        MsiFormatRecord,
        MsiGetActiveDatabase,
        MsiGetComponentState,
        MsiGetFeatureCost,
        MsiGetFeatureState,
        MsiGetFeatureValidStates,
        MsiGetLanguage,
        MsiGetLastErrorRecord,
        MsiGetMode,
        MsiGetProperty,
        MsiGetSourcePath,
        MsiGetSummaryInformation,
        MsiGetTargetPath,
        MsiProcessMessage,
        MsiRecordClearData,
        MsiRecordDataSize,
        MsiRecordGetFieldCount,
        MsiRecordGetInteger,
        MsiRecordGetString,
        MsiRecordIsNull,
        MsiRecordReadStream,
        MsiRecordSetInteger,
        MsiRecordSetStream,
        MsiRecordSetString,
        MsiSequence,
        MsiSetComponentState,
        MsiSetFeatureAttributes,
        MsiSetFeatureState,
        MsiSetInstallLevel,
        MsiSetMode,
        MsiSetProperty,
        MsiSetTargetPath,
        MsiSummaryInfoGetProperty,
        MsiVerifyDiskSpace,
        MsiViewExecute,
        MsiViewFetch,
        MsiViewGetError,
        MsiViewGetColumnInfo,
        MsiViewModify,
    }

    /// <summary>
    /// Defines the signature of the native function
    /// in SfxCA.dll that implements the remoting call.
    /// </summary>
    internal delegate void MsiRemoteInvoke(
        RemoteMsiFunctionId id,
        [MarshalAs(UnmanagedType.SysInt)]
        IntPtr request,
        [MarshalAs(UnmanagedType.SysInt)]
        out IntPtr response);

    /// <summary>
    /// Redirects native API calls to either the normal NativeMethods class
    /// or to out-of-proc calls via the remoting channel.
    /// </summary>
    internal static class RemotableNativeMethods
    {
        private const int MAX_REQUEST_FIELDS = 4;
        private static int requestFieldDataOffset;
        private static int requestFieldSize;
        
        [SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
        private static IntPtr requestBuf;

        private static MsiRemoteInvoke remotingDelegate;

        /// <summary>
        /// Checks if the current process is using remoting to access the
        /// MSI session and database APIs.
        /// </summary>
        internal static bool RemotingEnabled
        {
            get
            {
                return RemotableNativeMethods.remotingDelegate != null;
            }
        }

        /// <summary>
        /// Sets a delegate that is used to make remote API calls.
        /// </summary>
        /// <remarks><p>
        /// The implementation of this delegate is provided by the
        /// custom action host DLL.
        /// </p></remarks>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal static MsiRemoteInvoke RemotingDelegate
        {
            set
            {
                RemotableNativeMethods.remotingDelegate = value;

                if (value != null && requestBuf == IntPtr.Zero)
                {
                    requestFieldDataOffset = Marshal.SizeOf(typeof(IntPtr));
                    requestFieldSize = 2 * Marshal.SizeOf(typeof(IntPtr));
                    RemotableNativeMethods.requestBuf = Marshal.AllocHGlobal(
                        requestFieldSize * MAX_REQUEST_FIELDS);
                }
            }
        }

        internal static bool IsRemoteHandle(int handle)
        {
            return (handle & Int32.MinValue) != 0;
        }

        internal static int MakeRemoteHandle(int handle)
        {
            if (handle == 0)
            {
                return handle;
            }

            if (RemotableNativeMethods.IsRemoteHandle(handle))
            {
                throw new InvalidOperationException("Handle already has the remote bit set.");
            }

            return handle ^ Int32.MinValue;
        }

        internal static int GetRemoteHandle(int handle)
        {
            if (handle == 0)
            {
                return handle;
            }
            
            if (!RemotableNativeMethods.IsRemoteHandle(handle))
            {
                throw new InvalidOperationException("Handle does not have the remote bit set.");
            }

            return handle ^ Int32.MinValue;
        }

        private static void ClearData(IntPtr buf)
        {
            for (int i = 0; i < MAX_REQUEST_FIELDS; i++)
            {
                Marshal.WriteInt32(buf, (i * requestFieldSize), (int) VarEnum.VT_NULL);
                Marshal.WriteIntPtr(buf, (i * requestFieldSize) + requestFieldDataOffset, IntPtr.Zero);
            }
        }

        private static void WriteInt(IntPtr buf, int field, int value)
        {
            Marshal.WriteInt32(buf, (field * requestFieldSize), (int) VarEnum.VT_I4);
            Marshal.WriteInt32(buf, (field * requestFieldSize) + requestFieldDataOffset, value);
        }

        private static void WriteString(IntPtr buf, int field, string value)
        {
            if (value == null)
            {
                Marshal.WriteInt32(buf, (field * requestFieldSize), (int) VarEnum.VT_NULL);
                Marshal.WriteIntPtr(buf, (field * requestFieldSize) + requestFieldDataOffset, IntPtr.Zero);
            }
            else
            {
                IntPtr stringPtr = Marshal.StringToHGlobalUni(value);
                Marshal.WriteInt32(buf, (field * requestFieldSize), (int) VarEnum.VT_LPWSTR);
                Marshal.WriteIntPtr(buf, (field * requestFieldSize) + requestFieldDataOffset, stringPtr);
            }
        }

        private static int ReadInt(IntPtr buf, int field)
        {
            VarEnum vt = (VarEnum) Marshal.ReadInt32(buf, (field * requestFieldSize));
            if (vt == VarEnum.VT_EMPTY)
            {
                return 0;
            }
            else if (vt != VarEnum.VT_I4 && vt != VarEnum.VT_UI4)
            {
                throw new InstallerException("Invalid data received from remote MSI function invocation.");
            }
            return Marshal.ReadInt32(buf, (field * requestFieldSize) + requestFieldDataOffset);
        }

        private static void ReadString(IntPtr buf, int field, StringBuilder szBuf, ref uint cchBuf)
        {
            VarEnum vt = (VarEnum) Marshal.ReadInt32(buf, (field * requestFieldSize));
            if (vt == VarEnum.VT_NULL)
            {
                szBuf.Remove(0, szBuf.Length);
                return;
            }
            else if (vt != VarEnum.VT_LPWSTR)
            {
                throw new InstallerException("Invalid data received from remote MSI function invocation.");
            }

            szBuf.Remove(0, szBuf.Length);
            IntPtr strPtr = Marshal.ReadIntPtr(buf, (field * requestFieldSize) + requestFieldDataOffset);
            string str = Marshal.PtrToStringUni(strPtr);
            if (str != null)
            {
                szBuf.Append(str);
            }
            cchBuf = (uint) szBuf.Length;
        }

        private static void FreeString(IntPtr buf, int field)
        {
            IntPtr stringPtr = Marshal.ReadIntPtr(buf, (field * requestFieldSize) + requestFieldDataOffset);
            if (stringPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(stringPtr);
            }
        }

        private static void ReadStream(IntPtr buf, int field, byte[] sBuf, int count)
        {
            VarEnum vt = (VarEnum) Marshal.ReadInt32(buf, (field * requestFieldSize));
            if (vt != VarEnum.VT_STREAM)
            {
                throw new InstallerException("Invalid data received from remote MSI function invocation.");
            }

            IntPtr sPtr = Marshal.ReadIntPtr(buf, (field * requestFieldSize) + requestFieldDataOffset);
            Marshal.Copy(sPtr, sBuf, 0, count);
        }

        private static uint MsiFunc_III(RemoteMsiFunctionId id, int in1, int in2, int in3)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteInt(requestBuf, 1, in2);
                WriteInt(requestBuf, 2, in3);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                return unchecked ((uint) ReadInt(resp, 0));
            }
        }

        private static uint MsiFunc_IIS(RemoteMsiFunctionId id, int in1, int in2, string in3)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteInt(requestBuf, 1, in2);
                WriteString(requestBuf, 2, in3);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 2);
                return unchecked ((uint) ReadInt(resp, 0));
            }
        }

        private static uint MsiFunc_ISI(RemoteMsiFunctionId id, int in1, string in2, int in3)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                WriteInt(requestBuf, 2, in3);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 2);
                return unchecked ((uint) ReadInt(resp, 0));
            }
        }

        private static uint MsiFunc_ISS(RemoteMsiFunctionId id, int in1, string in2, string in3)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                WriteString(requestBuf, 2, in3);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 1);
                FreeString(requestBuf, 2);
                return unchecked ((uint) ReadInt(resp, 0));
            }
        }

        private static uint MsiFunc_II_I(RemoteMsiFunctionId id, int in1, int in2, out int out1)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteInt(requestBuf, 1, in2);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                out1 = ReadInt(resp, 1);
                return ret;
            }
        }

        private static uint MsiFunc_ISII_I(RemoteMsiFunctionId id, int in1, string in2, int in3, int in4, out int out1)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                WriteInt(requestBuf, 2, in3);
                WriteInt(requestBuf, 3, in4);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 1);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                out1 = ReadInt(resp, 1);
                return ret;
            }
        }

        private static uint MsiFunc_IS_II(RemoteMsiFunctionId id, int in1, string in2, out int out1, out int out2)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 1);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                out1 = ReadInt(resp, 1);
                out2 = ReadInt(resp, 2);
                return ret;
            }
        }

        private static uint MsiFunc_II_S(RemoteMsiFunctionId id, int in1, int in2, StringBuilder out1, ref uint cchOut1)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteInt(requestBuf, 1, in2);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                if (ret == 0) ReadString(resp, 1, out1, ref cchOut1);
                return ret;
            }
        }

        private static uint MsiFunc_IS_S(RemoteMsiFunctionId id, int in1, string in2, StringBuilder out1, ref uint cchOut1)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 1);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                if (ret == 0) ReadString(resp, 1, out1, ref cchOut1);
                return ret;
            }
        }

        private static uint MsiFunc_ISII_SII(RemoteMsiFunctionId id, int in1, string in2, int in3, int in4, StringBuilder out1, ref uint cchOut1, out int out2, out int out3)
        {
            lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, in1);
                WriteString(requestBuf, 1, in2);
                WriteInt(requestBuf, 2, in3);
                WriteInt(requestBuf, 3, in4);
                IntPtr resp;
                remotingDelegate(id, requestBuf, out resp);
                FreeString(requestBuf, 1);
                uint ret = unchecked ((uint) ReadInt(resp, 0));
                if (ret == 0) ReadString(resp, 1, out1, ref cchOut1);
                out2 = ReadInt(resp, 2);
                out3 = ReadInt(resp, 3);
                return ret;
            }
        }

        internal static int MsiProcessMessage(int hInstall, uint eMessageType, int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
            {
                return NativeMethods.MsiProcessMessage(hInstall, eMessageType, hRecord);
            }
            else lock (remotingDelegate)
            {
                // I don't understand why, but this particular function doesn't work
                // when using the static requestBuf -- some data doesn't make it through.
                // But it works when a fresh buffer is allocated here every call.
                IntPtr buf = Marshal.AllocHGlobal(
                    requestFieldSize * MAX_REQUEST_FIELDS);
                ClearData(buf);
                WriteInt(buf, 0, RemotableNativeMethods.GetRemoteHandle(hInstall));
                WriteInt(buf, 1, unchecked ((int) eMessageType));
                WriteInt(buf, 2, RemotableNativeMethods.GetRemoteHandle(hRecord));
                IntPtr resp;
                remotingDelegate(RemoteMsiFunctionId.MsiProcessMessage, buf, out resp);
                Marshal.FreeHGlobal(buf);
                return ReadInt(resp, 0);
            }
        }

        internal static uint MsiCloseHandle(int hAny)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hAny))
                return NativeMethods.MsiCloseHandle(hAny);
            else
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiCloseHandle, RemotableNativeMethods.GetRemoteHandle(hAny), 0, 0);
        }

        internal static uint MsiGetProperty(int hInstall, string szName, StringBuilder szValueBuf, ref uint cchValueBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetProperty(hInstall, szName, szValueBuf, ref cchValueBuf);
            else
            {
                return RemotableNativeMethods.MsiFunc_IS_S(
                    RemoteMsiFunctionId.MsiGetProperty,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szName,
                    szValueBuf,
                    ref cchValueBuf);
            }
        }

        internal static uint MsiSetProperty(int hInstall, string szName, string szValue)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetProperty(hInstall, szName, szValue);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISS(
                    RemoteMsiFunctionId.MsiSetProperty,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szName,
                    szValue);
            }
        }

        internal static int MsiCreateRecord(uint cParams, int hAny)
        {
            // When remoting is enabled, we might need to create either a local or
            // remote record, depending on the handle it is to have an affinity with.
            // If no affinity handle is specified, create a remote record (the 99% case).
            if (!RemotingEnabled ||
                (hAny != 0 && !RemotableNativeMethods.IsRemoteHandle(hAny)))
            {
                return NativeMethods.MsiCreateRecord(cParams);
            }
            else
            {
                int hRecord = unchecked((int)RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiCreateRecord, (int) cParams, 0, 0));
                return RemotableNativeMethods.MakeRemoteHandle(hRecord);
            }
        }

        internal static uint MsiRecordGetFieldCount(int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordGetFieldCount(hRecord);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordGetFieldCount,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    0,
                    0);
            }
        }

        internal static int MsiRecordGetInteger(int hRecord, uint iField)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordGetInteger(hRecord, iField);
            else
            {
                return unchecked ((int) RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordGetInteger,
                    RemotableNativeMethods.GetRemoteHandle(hRecord), 
                    (int) iField, 
                    0));
            }
        }

        internal static uint MsiRecordGetString(int hRecord, uint iField, StringBuilder szValueBuf, ref uint cchValueBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
            {
                return NativeMethods.MsiRecordGetString(hRecord, iField, szValueBuf, ref cchValueBuf);
            }
            else
            {
                return RemotableNativeMethods.MsiFunc_II_S(
                    RemoteMsiFunctionId.MsiRecordGetString,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField,
                    szValueBuf,
                    ref cchValueBuf);
            }
        }

        internal static uint MsiRecordSetInteger(int hRecord, uint iField, int iValue)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordSetInteger(hRecord, iField, iValue);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordSetInteger,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField,
                    iValue);
            }
        }

        internal static uint MsiRecordSetString(int hRecord, uint iField, string szValue)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordSetString(hRecord, iField, szValue);
            else
            {
                return RemotableNativeMethods.MsiFunc_IIS(
                    RemoteMsiFunctionId.MsiRecordSetString,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField,
                    szValue);
            }
        }

        internal static int MsiGetActiveDatabase(int hInstall)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetActiveDatabase(hInstall);
            else
            {
                int hDatabase = (int)RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiGetActiveDatabase,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    0,
                    0);
                return RemotableNativeMethods.MakeRemoteHandle(hDatabase);
            }
        }

        internal static uint MsiDatabaseOpenView(int hDatabase, string szQuery, out int hView)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hDatabase))
                return NativeMethods.MsiDatabaseOpenView(hDatabase, szQuery, out hView);
            else
            {
                uint err = RemotableNativeMethods.MsiFunc_ISII_I(
                    RemoteMsiFunctionId.MsiDatabaseOpenView,
                    RemotableNativeMethods.GetRemoteHandle(hDatabase),
                    szQuery,
                    0,
                    0,
                    out hView);
                hView = RemotableNativeMethods.MakeRemoteHandle(hView);
                return err;
            }
        }

        internal static uint MsiViewExecute(int hView, int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hView))
                return NativeMethods.MsiViewExecute(hView, hRecord);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiViewExecute,
                    RemotableNativeMethods.GetRemoteHandle(hView),
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    0);
            }
        }

        internal static uint MsiViewFetch(int hView, out int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hView))
                return NativeMethods.MsiViewFetch(hView, out hRecord);
            else
            {
                uint err = RemotableNativeMethods.MsiFunc_II_I(
                    RemoteMsiFunctionId.MsiViewFetch,
                    RemotableNativeMethods.GetRemoteHandle(hView),
                    0,
                    out hRecord);
                hRecord = RemotableNativeMethods.MakeRemoteHandle(hRecord);
                return err;
            }
        }

        internal static uint MsiViewModify(int hView, int iModifyMode, int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hView))
                return NativeMethods.MsiViewModify(hView, iModifyMode, hRecord);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiViewModify,
                    RemotableNativeMethods.GetRemoteHandle(hView),
                    iModifyMode,
                    RemotableNativeMethods.GetRemoteHandle(hRecord));
            }
        }

        internal static int MsiViewGetError(int hView, StringBuilder szColumnNameBuffer, ref uint cchBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hView))
                return NativeMethods.MsiViewGetError(hView, szColumnNameBuffer, ref cchBuf);
            else
            {
                return unchecked ((int) RemotableNativeMethods.MsiFunc_II_S(
                    RemoteMsiFunctionId.MsiViewGetError,
                    RemotableNativeMethods.GetRemoteHandle(hView),
                    0,
                    szColumnNameBuffer,
                    ref cchBuf));
            }
        }

        internal static uint MsiViewGetColumnInfo(int hView, uint eColumnInfo, out int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hView))
                return NativeMethods.MsiViewGetColumnInfo(hView, eColumnInfo, out hRecord);
            else
            {
                uint err = RemotableNativeMethods.MsiFunc_II_I(
                    RemoteMsiFunctionId.MsiViewGetColumnInfo,
                    RemotableNativeMethods.GetRemoteHandle(hView),
                    (int) eColumnInfo,
                    out hRecord);
                hRecord = RemotableNativeMethods.MakeRemoteHandle(hRecord);
                return err;
            }
        }

        internal static uint MsiFormatRecord(int hInstall, int hRecord, StringBuilder szResultBuf, ref uint cchResultBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiFormatRecord(hInstall, hRecord, szResultBuf, ref cchResultBuf);
            else
            {
                return RemotableNativeMethods.MsiFunc_II_S(
                    RemoteMsiFunctionId.MsiFormatRecord,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    szResultBuf,
                    ref cchResultBuf);
            }
        }

        internal static uint MsiRecordClearData(int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordClearData(hRecord);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordClearData,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    0,
                    0);
            }
        }

        internal static bool MsiRecordIsNull(int hRecord, uint iField)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordIsNull(hRecord, iField);
            else
            {
                return 0 != RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordIsNull,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField,
                    0);
            }
        }

        internal static uint MsiDatabaseGetPrimaryKeys(int hDatabase, string szTableName, out int hRecord)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hDatabase))
                return NativeMethods.MsiDatabaseGetPrimaryKeys(hDatabase, szTableName, out hRecord);
            else
            {
                uint err = RemotableNativeMethods.MsiFunc_ISII_I(
                    RemoteMsiFunctionId.MsiDatabaseGetPrimaryKeys,
                    RemotableNativeMethods.GetRemoteHandle(hDatabase),
                    szTableName,
                    0,
                    0,
                    out hRecord);
                hRecord = RemotableNativeMethods.MakeRemoteHandle(hRecord);
                return err;
            }
        }

        internal static uint MsiDatabaseIsTablePersistent(int hDatabase, string szTableName)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hDatabase))
                return NativeMethods.MsiDatabaseIsTablePersistent(hDatabase, szTableName);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiDatabaseIsTablePersistent,
                    RemotableNativeMethods.GetRemoteHandle(hDatabase),
                    szTableName,
                    0);
            }
        }

        internal static uint MsiDoAction(int hInstall, string szAction)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiDoAction(hInstall, szAction);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiDoAction,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szAction,
                    0);
            }
        }

        internal static uint MsiEnumComponentCosts(int hInstall, string szComponent, uint dwIndex, int iState, StringBuilder lpDriveBuf, ref uint cchDriveBuf, out int iCost, out int iTempCost)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiEnumComponentCosts(hInstall, szComponent, dwIndex, iState, lpDriveBuf, ref cchDriveBuf, out iCost, out iTempCost);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISII_SII(
                    RemoteMsiFunctionId.MsiEvaluateCondition,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szComponent, (int) dwIndex, iState, lpDriveBuf, ref cchDriveBuf, out iCost, out iTempCost);
            }
        }

        internal static uint MsiEvaluateCondition(int hInstall, string szCondition)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiEvaluateCondition(hInstall, szCondition);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiEvaluateCondition,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szCondition,
                    0);
            }
        }

        internal static uint MsiGetComponentState(int hInstall, string szComponent, out int iInstalled, out int iAction)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetComponentState(hInstall, szComponent, out iInstalled, out iAction);
            else
            {
                return RemotableNativeMethods.MsiFunc_IS_II(
                    RemoteMsiFunctionId.MsiGetComponentState,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szComponent,
                    out iInstalled,
                    out iAction);
            }
        }

        internal static uint MsiGetFeatureCost(int hInstall, string szFeature, int iCostTree, int iState, out int iCost)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetFeatureCost(hInstall, szFeature, iCostTree, iState, out iCost);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISII_I(
                    RemoteMsiFunctionId.MsiGetFeatureCost,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFeature,
                    iCostTree,
                    iState,
                    out iCost);
            }
        }

        internal static uint MsiGetFeatureState(int hInstall, string szFeature, out int iInstalled, out int iAction)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetFeatureState(hInstall, szFeature, out iInstalled, out iAction);
            else
            {
                return RemotableNativeMethods.MsiFunc_IS_II(
                    RemoteMsiFunctionId.MsiGetFeatureState,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFeature,
                    out iInstalled,
                    out iAction);
            }
        }

        internal static uint MsiGetFeatureValidStates(int hInstall, string szFeature, out uint dwInstalledState)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetFeatureValidStates(hInstall, szFeature, out dwInstalledState);
            else
            {
                int iTemp;
                uint ret = RemotableNativeMethods.MsiFunc_ISII_I(
                    RemoteMsiFunctionId.MsiGetFeatureValidStates,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFeature,
                    0,
                    0,
                    out iTemp);
                dwInstalledState = (uint) iTemp;
                return ret;
            }
        }

        internal static int MsiGetLanguage(int hInstall)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetLanguage(hInstall);
            else
            {
                return unchecked((int)RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiGetLanguage,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    0,
                    0));
            }
        }

        internal static int MsiGetLastErrorRecord(int hAny)
        {
            // When remoting is enabled, we might need to create either a local or
            // remote record, depending on the handle it is to have an affinity with.
            // If no affinity handle is specified, create a remote record (the 99% case).
            if (!RemotingEnabled ||
                (hAny != 0 && !RemotableNativeMethods.IsRemoteHandle(hAny)))
            {
                return NativeMethods.MsiGetLastErrorRecord();
            }
            else
            {
                int hRecord = unchecked((int) RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiGetLastErrorRecord, 0, 0, 0));
                return RemotableNativeMethods.MakeRemoteHandle(hRecord);
            }
        }

        internal static bool MsiGetMode(int hInstall, uint iRunMode)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetMode(hInstall, iRunMode);
            else
            {
                return 0 != RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiGetMode,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    (int) iRunMode,
                    0);
            }
        }

        internal static uint MsiGetSourcePath(int hInstall, string szFolder, StringBuilder szPathBuf, ref uint cchPathBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetSourcePath(hInstall, szFolder, szPathBuf, ref cchPathBuf);
            else
            {
                return RemotableNativeMethods.MsiFunc_IS_S(
                    RemoteMsiFunctionId.MsiGetSourcePath,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFolder,
                    szPathBuf,
                    ref cchPathBuf);
            }
        }

        internal static uint MsiGetSummaryInformation(int hDatabase, string szDatabasePath, uint uiUpdateCount, out int hSummaryInfo)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hDatabase))
                return NativeMethods.MsiGetSummaryInformation(hDatabase, szDatabasePath, uiUpdateCount, out hSummaryInfo);
            else
            {
                uint err = RemotableNativeMethods.MsiFunc_ISII_I(
                    RemoteMsiFunctionId.MsiGetSummaryInformation,
                    RemotableNativeMethods.GetRemoteHandle(hDatabase),
                    szDatabasePath,
                    (int)uiUpdateCount,
                    0,
                    out hSummaryInfo);
                hSummaryInfo = RemotableNativeMethods.MakeRemoteHandle(hSummaryInfo);
                return err;
            }
        }

        internal static uint MsiGetTargetPath(int hInstall, string szFolder, StringBuilder szPathBuf, ref uint cchPathBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiGetTargetPath(hInstall, szFolder, szPathBuf, ref cchPathBuf);
            else
            {
                return RemotableNativeMethods.MsiFunc_IS_S(
                    RemoteMsiFunctionId.MsiGetTargetPath,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFolder,
                    szPathBuf,
                    ref cchPathBuf);
            }
        }

        internal static uint MsiRecordDataSize(int hRecord, uint iField)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordDataSize(hRecord, iField);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiRecordDataSize,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField, 0);
                }
        }

        internal static uint MsiRecordReadStream(int hRecord, uint iField, byte[] szDataBuf, ref uint cbDataBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
            {
                return NativeMethods.MsiRecordReadStream(hRecord, iField, szDataBuf, ref cbDataBuf);
            }
            else lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                unchecked
                {
                    WriteInt(requestBuf, 0, RemotableNativeMethods.GetRemoteHandle(hRecord));
                    WriteInt(requestBuf, 1, (int) iField);
                    WriteInt(requestBuf, 2, (int) cbDataBuf);
                    IntPtr resp;
                    remotingDelegate(RemoteMsiFunctionId.MsiRecordReadStream, requestBuf, out resp);
                    uint ret = (uint) ReadInt(resp, 0);
                    if (ret == 0)
                    {
                        cbDataBuf = (uint) ReadInt(resp, 2);
                        if (cbDataBuf > 0)
                        {
                            RemotableNativeMethods.ReadStream(resp, 1, szDataBuf, (int) cbDataBuf);
                        }
                    }
                    return ret;
                }
            }
        }

        internal static uint MsiRecordSetStream(int hRecord, uint iField, string szFilePath)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hRecord))
                return NativeMethods.MsiRecordSetStream(hRecord, iField, szFilePath);
            else
            {
                return RemotableNativeMethods.MsiFunc_IIS(
                    RemoteMsiFunctionId.MsiRecordSetStream,
                    RemotableNativeMethods.GetRemoteHandle(hRecord),
                    (int) iField,
                    szFilePath);
            }
        }

        internal static uint MsiSequence(int hInstall, string szTable, int iSequenceMode)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSequence(hInstall, szTable, iSequenceMode);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiSequence,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szTable,
                    iSequenceMode);
            }
        }

        internal static uint MsiSetComponentState(int hInstall, string szComponent, int iState)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetComponentState(hInstall, szComponent, iState);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiSetComponentState,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szComponent,
                    iState);
            }
        }

        internal static uint MsiSetFeatureAttributes(int hInstall, string szFeature, uint dwAttributes)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetFeatureAttributes(hInstall, szFeature, dwAttributes);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiSetFeatureAttributes,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFeature,
                    (int) dwAttributes);
            }
        }

        internal static uint MsiSetFeatureState(int hInstall, string szFeature, int iState)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetFeatureState(hInstall, szFeature, iState);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISI(
                    RemoteMsiFunctionId.MsiSetFeatureState,
                    RemotableNativeMethods.GetRemoteHandle(hInstall), szFeature, iState);
            }
        }

        internal static uint MsiSetInstallLevel(int hInstall, int iInstallLevel)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetInstallLevel(hInstall, iInstallLevel);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiSetInstallLevel,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    iInstallLevel,
                    0);
            }
        }

        internal static uint MsiSetMode(int hInstall, uint iRunMode, bool fState)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetMode(hInstall, iRunMode, fState);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiSetMode,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    (int) iRunMode,
                    fState ? 1 : 0);
            }
        }

        internal static uint MsiSetTargetPath(int hInstall, string szFolder, string szFolderPath)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiSetTargetPath(hInstall, szFolder, szFolderPath);
            else
            {
                return RemotableNativeMethods.MsiFunc_ISS(
                    RemoteMsiFunctionId.MsiSetTargetPath,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    szFolder,
                    szFolderPath);
            }
        }

        internal static uint MsiSummaryInfoGetProperty(int hSummaryInfo, uint uiProperty, out uint uiDataType, out int iValue, ref long ftValue, StringBuilder szValueBuf, ref uint cchValueBuf)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hSummaryInfo))
            {
                return NativeMethods.MsiSummaryInfoGetProperty(hSummaryInfo, uiProperty, out uiDataType, out iValue, ref ftValue, szValueBuf, ref cchValueBuf);
            }
            else lock (RemotableNativeMethods.remotingDelegate)
            {
                ClearData(requestBuf);
                WriteInt(requestBuf, 0, RemotableNativeMethods.GetRemoteHandle(hSummaryInfo));
                WriteInt(requestBuf, 1, (int) uiProperty);
                IntPtr resp;
                remotingDelegate(RemoteMsiFunctionId.MsiSummaryInfoGetProperty, requestBuf, out resp);
                unchecked
                {
                    uint ret = (uint) ReadInt(resp, 0);
                    if (ret == 0)
                    {
                        uiDataType = (uint) ReadInt(resp, 1);
                        switch ((VarEnum) uiDataType)
                        {
                            case VarEnum.VT_I2:
                            case VarEnum.VT_I4:
                                iValue = ReadInt(resp, 2);
                                break;

                            case VarEnum.VT_FILETIME:
                                uint ftHigh = (uint) ReadInt(resp, 2);
                                uint ftLow = (uint) ReadInt(resp, 3);
                                ftValue = ((long) ftHigh) << 32 | ((long) ftLow);
                                iValue = 0;
                                break;

                            case VarEnum.VT_LPSTR:
                                ReadString(resp, 2, szValueBuf, ref cchValueBuf);
                                iValue = 0;
                                break;

                            default:
                                iValue = 0;
                                break;
                        }
                    }
                    else
                    {
                        uiDataType = 0;
                        iValue = 0;
                    }
                    return ret;
                }
            }
        }

        internal static uint MsiVerifyDiskSpace(int hInstall)
        {
            if (!RemotingEnabled || !RemotableNativeMethods.IsRemoteHandle(hInstall))
                return NativeMethods.MsiVerifyDiskSpace(hInstall);
            else
            {
                return RemotableNativeMethods.MsiFunc_III(
                    RemoteMsiFunctionId.MsiVerifyDiskSpace,
                    RemotableNativeMethods.GetRemoteHandle(hInstall),
                    0,
                    0);
            }
        }
    }
}