- UID
- 2
注册时间2004-12-1
阅读权限255
最后登录1970-1-1
总坛主
TA的每日心情 | 开心 2024-12-1 11:04 |
---|
签到天数: 12 天 [LV.3]偶尔看看II
|
经过测试,非常稳定,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;
复制代码
BTMemoryModule.rar
(5.13 KB, 下载次数: 29)
|
|