機能を模倣することは可能loadlibrary
ですか?最初に一時ファイルに書き込まずに BLOB フィールドからライブラリをロードしたいのですが、特定のバージョンの Delphi コンパイラや Windows に依存せず、ウイルス対策ソフトウェアをトリガーしないソリューションが必要です。
3 に答える
はい、できます。メモリからコードを実行する必要はありません。PAGE_EXECUTEフラグが設定されたVirtualAlloc関数をloadlibrary
使用してメモリを割り当てる必要があります。
更新: これは、32 ビット Delphi のメモリから実行されるコードの簡単で汚いデモです。動作することのみをテストしました。
type
TIncMe = procedure(var I: Integer);
var
IncMeProc: TIncMe;
procedure IncMe(var I: Integer);
begin
Inc(I);
end;
procedure CopyIncMe;
var
Size: LongWord;
Tmp: Pointer;
begin
Size:= LongWord(@CopyIncMe) - LongWord(@IncMe);
Tmp:= VirtualAlloc(nil, Size, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
Move(Pointer(@IncMe)^, Tmp^, Size);
IncMeProc:= Tmp;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
J: Integer;
begin
J:= 0;
CopyIncMe;
while J < 10 do begin
IncMeProc(J);
ShowMessage(IntToStr(J));
end;
VirtualFree(@IncMeProc, 0, MEM_RELEASE);
end;
dzlib には、リソースから dll をメモリに読み込み、ディスクに保存せずにそれを使用するための既製のオブジェクトが含まれています。
これがメインファイルです...
http://sourceforge.net/p/dzlib/code/147/tree/dzlib/trunk/src/u_dzResourceDllLoader.pas
..しかし、同じリポジトリからの他のファイルが必要です。
リソースから dll をロードする方法を示す記事がdelphi.about.comにあります。
最初にリソースをメモリにロードし、次にメモリ モジュールを使用してリソースから dll をロードします。
リソースの代わりに、データベースまたは dll をロードする任意のソースを使用できます。メモリ ストリームに入ると、次のコードを使用して dll 関数を読み込んで実行できます。これは、dll を呼び出す「通常の」コードと非常によく似ています。
var
btMM: PBTMemoryModule;
begin
btMM := BTMemoryLoadLibary(mp_DllData, m_DllDataSize);
try
if btMM = nil then Abort;
@m_TestCallstd := BTMemoryGetProcAddress(btMM, 'TestCallstd');
if @m_TestCallstd = nil then Abort;
m_TestCallstd('This is a Dll Memory call!');
except
Showmessage('An error occoured while loading the dll: ' + BTMemoryGetLastError);
end;
if Assigned(btMM) then BTMemoryFreeLibrary(btMM);
end;