Ejemplo n.º 1
0
        protected virtual void Dispose()
        {
            if (_dataHandle.IsAllocated)
            {
                _dataHandle.Free();
            }

            if (_initialized)
            {
                _dllEntry?.Invoke(_codeBase, DllReason.DLL_PROCESS_DETACH, null);
                _initialized = false;
            }

            if (_modules.Count > 0)
            {
                foreach (HCoustomMudule module in _modules)
                {
                    if (!InvalidHandle(module)) // INVALID_HANDLE
                    {
                        Win.FreeLibrary(module);
                    }
                }
            }

            if (_codeBase != null)
            {
                Win.VirtualFree(_codeBase, 0, AllocationType.RELEASE);
            }

            Disposed = true;
        }
Ejemplo n.º 2
0
        private void BuildImportTable()
        {
            IMAGE_DATA_DIRECTORY *directory = &_headers->OptionalHeader.ImportTable;

            if (directory->Size == 0)
            {
                throw new NativeDllLoadException("Invalid import table.");
            }


            IMAGE_IMPORT_DESCRIPTOR *importDesc = (IMAGE_IMPORT_DESCRIPTOR *)(_codeBase + directory->VirtualAddress);

            for (; !Win.IsBadReadPtr((importDesc), (UIntPtrT)Marshal.SizeOf(typeof(IMAGE_IMPORT_DESCRIPTOR))) && importDesc->Name > 0; importDesc++)
            {
                UIntPtrT *thunkRef;
                Farproc * funcRef;

                HCoustomMudule handle = Win.LoadLibrary(_codeBase + importDesc->Name);

                if (InvalidHandle(handle))
                {
                    if (_modules.Any())
                    {
                        _modules.ForEach(m => Win.FreeLibrary(m));
                    }

                    throw new NativeDllLoadException("Can't load libary " + Marshal.PtrToStringAnsi(new IntPtr(_codeBase + importDesc->Name)));
                }

                _modules.Add(handle);
                if (importDesc->OriginalFirstThunk > 0)
                {
                    thunkRef = (UIntPtrT *)(_codeBase + importDesc->OriginalFirstThunk);
                    funcRef  = (Farproc *)(_codeBase + importDesc->FirstThunk);
                }
                else
                {
                    // no hint table
                    thunkRef = (UIntPtrT *)(_codeBase + importDesc->FirstThunk);
                    funcRef  = (Farproc *)(_codeBase + importDesc->FirstThunk);
                }
                for (; *thunkRef > 0; thunkRef++, funcRef++)
                {
                    string procName;
                    if (Win.IMAGE_SNAP_BY_ORDINAL(*thunkRef))
                    {
                        procName = Marshal.PtrToStringAnsi(new IntPtr());
                        *funcRef = (Farproc)Win.GetProcAddress(handle, (byte *)Win.IMAGE_ORDINAL(*thunkRef));
                    }
                    else
                    {
                        IMAGE_IMPORT_BY_NAME *thunkData = (IMAGE_IMPORT_BY_NAME *)(_codeBase + (*thunkRef));
                        *funcRef = (Farproc)Win.GetProcAddress(handle, thunkData->Name);
                    }
                    if (*funcRef == 0)
                    {
                        throw new NativeDllLoadException("Can't get adress for imported function.");
                    }
                }
            }
        }