内存加载模块 -- 飘云测试通过
经过测试,非常稳定,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;
学习一下,好资源。 研究一下 感谢!感谢! 支持一个,就是不知道能不呢公用啊! 学习一下,好资源。 学习一下,好资源。
页:
[1]