Exemple #1
0
        public static ModuleImplementation Import(string path, CompilerOptions options)
        {
            Dictionary <string, FunctionImplementation> functions = new Dictionary <string, FunctionImplementation>();

            // Load the module in to the current process
            IntPtr hModule = NativeMethods.LoadLibrary(path);

            if (hModule == IntPtr.Zero)
            {
                throw new AzureKinectStubGeneratorException("Unable to load implementation module");
            }


            IntPtr Stub_SetErrorFunction = NativeMethods.GetProcAddress(hModule, "Stub_SetErrorFunction");

            if (Stub_SetErrorFunction == IntPtr.Zero)
            {
                throw new AzureKinectStubGeneratorException("No Error function");
            }

            NativeMethods.Stub_SetErrorFunction setError = System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer <NativeMethods.Stub_SetErrorFunction>(Stub_SetErrorFunction);

            setError(new NativeMethods.RaiseError(ModuleError));

            try
            {
                ModuleImplementation module = new ModuleImplementation(path, hModule, functions);

                // Inspect the module for its exports and get the function addresses
                ModuleInfo moduleInfo = ModuleInfo.Analyze(path, options);
                foreach (string export in moduleInfo.Exports)
                {
                    IntPtr functionAddress   = NativeMethods.GetProcAddress(hModule, export);
                    FunctionImplementation f = new FunctionImplementation(export, module, functionAddress);
                    functions.Add(export, f);
                }

                return(new ModuleImplementation(path, hModule, functions));
            }
            catch
            {
                NativeMethods.FreeLibrary(hModule);
                throw;
            }
        }
        public void SetImplementation(CodeString code)
        {
            Hash hash = Hash.GetHash(code) + Hash.GetHash(CompilerOptions);

            if (!implementedModules.ContainsKey(hash))
            {
                implementedModules.Add(hash, ModuleImplementation.Compile(code, CompilerOptions));
            }

            ModuleImplementation i = implementedModules[hash];

            if (i.Functions.Where((x) => { return(!x.Value.Name.StartsWith("Stub_")); }).Count() == 0)
            {
                throw new AzureKinectStubGeneratorException("No exported functions found in implementation");
            }
            foreach (var function in i.Functions)
            {
                SetImplementation(function.Key, function.Value);
            }
        }
 internal FunctionImplementation(string name, ModuleImplementation module, IntPtr address)
 {
     this.Name    = name;
     this.Module  = module;
     this.Address = address;
 }