Example #1
0
        public static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: PInvokeCompiler Input.dll Output.dll");
                return;
            }

            var inputFile  = args[0];
            var outputFile = args[1];

            using (var host = new PeReader.DefaultHost())
            {
                var unit     = host.LoadUnitFrom(inputFile);
                var assembly = unit as IAssembly;

                if (assembly == null)
                {
                    Console.WriteLine("Error: Input file not loadable as assembly");
                    return;
                }

                Assembly mutable = new MetadataDeepCopier(host).Copy(assembly);

                var interopHelperReference = new InteropHelperReferences(host, mutable);

                var pinvokeMethodMetadataTraverser = new PInvokeMethodMetadataTraverser(interopHelperReference.PInvokeHelpers);
                pinvokeMethodMetadataTraverser.TraverseChildren(mutable);

                var methodTransformationMetadataRewriter = new MethodTransformationMetadataRewriter(interopHelperReference.LoadLibrary, interopHelperReference.GetProcAddress, interopHelperReference.IsLibraryInitialized, host, pinvokeMethodMetadataTraverser);
                methodTransformationMetadataRewriter.RewriteChildren(mutable);

                var pinvokeMethodMetadataRewriter = new PInvokeMethodMetadataRewriter(interopHelperReference, host, methodTransformationMetadataRewriter);
                pinvokeMethodMetadataRewriter.RewriteChildren(mutable);

                using (var stream = File.Create(outputFile))
                {
                    PeWriter.WritePeToStream(mutable, host, stream);
                }
            }
        }
        public PInvokeMethodMetadataRewriter(InteropHelperReferences interopHelperReferences, IMetadataHost host, IMethodTransformationMetadataProvider metadataProvider)
            : base(host)
        {
            this.metadataProvider = metadataProvider;
            var platformType = host.PlatformType;
            var nameTable    = host.NameTable;

            this.intPtrZero = new FieldReference
            {
                Name           = nameTable.GetNameFor("Zero"),
                ContainingType = platformType.SystemIntPtr,
                Type           = platformType.SystemIntPtr
            };

            this.stringToAnsiArray = interopHelperReferences.StringToAnsiByteArray;

            this.getOffSetToStringData = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = nameTable.GetNameFor("get_OffsetToStringData"),
                ContainingType = interopHelperReferences.SystemRuntimeCompilerServicesRuntimeHelpers,
                Type           = platformType.SystemInt32
            };

            this.intPtrOpEquality = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = nameTable.OpEquality,
                ContainingType = platformType.SystemIntPtr,
                Type           = platformType.SystemBoolean,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = platformType.SystemIntPtr
                    }, new ParameterDefinition {
                        Index = 1, Type = platformType.SystemIntPtr
                    }
                }
            };

            this.getFunctionPointerForDelegate = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = nameTable.GetNameFor("GetFunctionPointerForDelegate"),
                ContainingType = interopHelperReferences.SystemRuntimeInteropServicesMarshal,
                Type           = platformType.SystemIntPtr,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = platformType.SystemDelegate
                    }
                }
            };

            this.getFunctionPointerForDelegate = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = nameTable.GetNameFor("GetFunctionPointerForDelegate"),
                ContainingType = interopHelperReferences.SystemRuntimeInteropServicesMarshal,
                Type           = platformType.SystemIntPtr,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = platformType.SystemDelegate
                    }
                }
            };

            this.ptrToStringAnsi = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = host.NameTable.GetNameFor("PtrToStringAnsi"),
                ContainingType = interopHelperReferences.SystemRuntimeInteropServicesMarshal,
                Type           = host.PlatformType.SystemString,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = host.PlatformType.SystemIntPtr
                    }
                }
            };

            this.ptrToStringUnicode = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = host.NameTable.GetNameFor("PtrToStringUni"),
                ContainingType = interopHelperReferences.SystemRuntimeInteropServicesMarshal,
                Type           = host.PlatformType.SystemString,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = host.PlatformType.SystemIntPtr
                    }
                }
            };

            this.getTypeFromHandle = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = host.NameTable.GetNameFor("GetTypeFromHandle"),
                ContainingType = host.PlatformType.SystemType,
                Type           = host.PlatformType.SystemType,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = host.PlatformType.SystemRuntimeTypeHandle
                    }
                }
            };

            this.getDelegateForFunctionPointer = new Microsoft.Cci.MutableCodeModel.MethodReference
            {
                Name           = host.NameTable.GetNameFor("GetDelegateForFunctionPointer"),
                ContainingType = interopHelperReferences.SystemRuntimeInteropServicesMarshal,
                Type           = host.PlatformType.SystemDelegate,
                Parameters     = new List <IParameterTypeInformation> {
                    new ParameterDefinition {
                        Index = 0, Type = host.PlatformType.SystemIntPtr
                    }, new ParameterDefinition {
                        Index = 1, Type = host.PlatformType.SystemType
                    }
                }
            };

            this.stringArrayAnsiMarshallingProlog    = interopHelperReferences.StringArrayAnsiMarshallingProlog;
            this.stringArrayUnicodeMarshallingProlog = interopHelperReferences.StringArrayUnicodeMarshallingProlog;
            this.stringArrayMarshallingEpilog        = interopHelperReferences.StringArrayMarshallingEpilog;
            this.skipTypeReference = interopHelperReferences.PInvokeHelpers;
        }