Пример #1
0
        public NativeLibaryHandles LoadNativeLibariesIntoProcess(int processId)
        {
            var nativeHandles = new NativeLibaryHandles();

            // geting the handle of the process - with required privileges
            nativeHandles.ProcessHandle = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, false, processId);
            NativeHelper.ThrowIfRequired(nativeHandles.ProcessHandle);

            // searching for the address of LoadLibraryW and storing it in a pointer
            var kernel32Handle = GetModuleHandle("kernel32.dll");

            NativeHelper.ThrowIfRequired(kernel32Handle);

            var functionExecutor = RemoteFunction.Locate(nativeHandles.ProcessHandle, kernel32Handle, nameof(LoadLibraryA));

            var assemblyResolverPath = _nativeConfig.NativeLibDirectory + "\\" + _nativeConfig.AssemblyResolveHandle;

            nativeHandles.AssemblyResolveHandleLibHandle = new IntPtr(functionExecutor.Execute(assemblyResolverPath));
            if (nativeHandles.AssemblyResolveHandleLibHandle == IntPtr.Zero)
            {
                throw new NullReferenceException($"Cant load Library from {assemblyResolverPath}");
            }

            var ctosharp = _nativeConfig.NativeLibDirectory + "\\" + _nativeConfig.CtoSharpLib;

            nativeHandles.CtoSharpLibHandle = new IntPtr(functionExecutor.Execute(ctosharp));
            if (nativeHandles.CtoSharpLibHandle == IntPtr.Zero)
            {
                throw new NullReferenceException($"Cant load Library from {ctosharp}");
            }

            return(nativeHandles);
        }
Пример #2
0
        public static RemoteFunction Locate(IntPtr processHandle, IntPtr libaryReference, string functionName, Encoding encoding)
        {
            var functionReference = GetProcAddress(libaryReference, functionName);

            NativeHelper.ThrowIfRequired(functionReference);

            var remote = new RemoteFunction(processHandle, functionReference, encoding);

            remote.Name = functionName;
            return(remote);
        }