飘云 发表于 2014-2-5 00:34:42

内存加载模块 -- 飘云测试通过

经过测试,非常稳定,32/64bit通用   我封装了几个函数,大家可以直接使用!!
DLL:

#include <windows.h>
#include "MyDll.h"

int_stdcall AddFun(int a, int b)
{
      return a + b;
}

void_stdcall GotoPYG(char *url)
{
      ShellExecute(NULL,"open", url, "", "", SW_SHOW );
}

//MyDll.def
LIBRARY MyDll
EXPORTS
AddFun
GotoPYG

EXE:

// 函数原型
type
TAddFunc = function(a, b: Integer): Integer; stdcall;
TGotoPYGFunc = function(url: PChar): Boolean; stdcall;
implementation

uses
BTMemoryModule;

{$R *.dfm}

{$R MyDll.RES}

{-------------------------------------------------------------------------------
过程名:    GetMemoryDllPointer
作用:      获取内存DLL指针
作者:      piaoyun
日期:      2013.12.20
参数:      const ResName: string;[资源名] ResType: PChar[资源类型]
返回值:    Pointer[内存DLL指针]
-------------------------------------------------------------------------------}
function GetMemoryDllPointer(const ResName: string; ResType: PChar): Pointer;
var
MemoryStream: TResourceStream;
lpDllData: Pointer;// 若要保险起见,请把此指针放到全局 最后释放
dwDllDataSize: DWORD;
begin
MemoryStream := TResourceStream.Create(HInstance, ResName, ResType);
try
    dwDllDataSize := MemoryStream.Size;
    lpDllData := GetMemory(dwDllDataSize);
    MemoryStream.Read(lpDllData^, dwDllDataSize);
finally
    MemoryStream.Free;
end;
Result := BTMemoryLoadLibary(lpDllData, dwDllDataSize);
// 上面函数似乎已经完全拷贝了,可以释放
FreeMemory(lpDllData);
end;

{-------------------------------------------------------------------------------
过程名:    GetMemoryDllFunPointer
作用:      获取内存DLL某导出函数指针
作者:      piaoyun
日期:      2013.12.20
参数:      const ResName: string;[资源名] ResType: PChar[资源类型]
返回值:    Pointer[内存DLL导出函数指针]
-------------------------------------------------------------------------------}
function GetMemoryDllFunPointer(lpMemoryPointer: Pointer; FunName: PChar): Pointer;
begin
Result := BTMemoryGetProcAddress(PBTMemoryModule(lpMemoryPointer), FunName);
end;

{-------------------------------------------------------------------------------
过程名:    FreeMemoryDll
作用:      释放内存DLL
作者:      piaoyun
日期:      2013.12.20
参数:      lpMemoryPointer: Pointer
返回值:    无
-------------------------------------------------------------------------------}
procedure FreeMemoryDll(lpMemoryPointer: Pointer);
begin
if lpMemoryPointer <> nil then
    BTMemoryFreeLibrary(PBTMemoryModule(lpMemoryPointer));
end;

procedure Test();
var
lpMemoryPointer: Pointer;
AddFun: TAddFunc;
GotoPYGFun: TGotoPYGFunc;
begin
lpMemoryPointer := GetMemoryDllPointer('MYDLL', 'DLL');
AddFun := GetMemoryDllFunPointer(lpMemoryPointer, 'AddFun');
GotoPYGFun := GetMemoryDllFunPointer(lpMemoryPointer, 'GotoPYG');
if Assigned(AddFun) then
    ShowMessage(IntToStr(AddFun(10 ,20)));
if Assigned(GotoPYGFun) then
    GotoPYGFun('https://www.chinapyg.com');
   
FreeMemoryDll(lpMemoryPointer);
end;

procedure TForm1.btnTestClick(Sender: TObject);
begin
Test;
end;


xingbing 发表于 2014-2-7 10:42:15

学习一下,好资源。

hu251405204 发表于 2015-1-26 15:11:54

研究一下 感谢!感谢!

82207007 发表于 2016-6-27 11:16:46

支持一个,就是不知道能不呢公用啊!

ld0522 发表于 2016-7-1 17:42:02

学习一下,好资源。

cd37ycs 发表于 2024-2-13 11:31:41

学习一下,好资源。
页: [1]
查看完整版本: 内存加载模块 -- 飘云测试通过