一、DLL的入口点
1.1 VFB的自带DLL模式入口
FB是把代码转成C(GCC编译)或者汇编(GAS编译)后编译的,本身就有一个main函数,所以在程序里其实不需要入口点,直接写就可以顺序执行,而有的小伙伴编译DLL时,想使用DLL入口点函数的参数。而VFB只提供了DLL加载和DLL卸载两种(使用的是Constructor和Destructor来实现加载和卸载功能的),其实现方式如下(可以直接使用VFB的DLL模式,IDE已经实现,不需要写代码):
'===================================================================================================================
'该方法没有线程启动和终止的参数
'使用Constructor和Destructor来判断加载和卸载调用,以此设置fdwReason参数,具体如下:
'获取当前模块句柄
Function GetCurrentModuleHandle() As HMODULE
dim info As MEMORY_BASIC_INFORMATION
VirtualQuery(cast(LPCVOID,@GetCurrModuleHandle),@info,SizeOf(info))
Return cast(HMODULE,info.AllocationBase)
End Function
'DLL入口函数
Function DllMain(ByVal hModule As HANDLE, ByVal fdwReason As Dword, ByVal lpReserved As LPVOID) As BOOL Export
Select Case fdwReason
Case DLL_PROCESS_ATTACH 'DLL被加载
MessageBox(0,"DLL_PROCESS_ATTACH","hModule=" & Hex(hModule),0)
Case DLL_PROCESS_DETACH 'DLL被卸载
MessageBox(0,"DLL_PROCESS_DETACH","hModule=" & Hex(hModule),0)
End Select
Function = TRUE
End Function
'DLL启动时调用
Sub LoadDllMain() Constructor
DllMain(GetCurrentModuleHandle(),DLL_PROCESS_ATTACH,NULL)
End Sub
'DLL卸载时调用
Sub UnloadDllMain() Destructor
DllMain(GetCurrentModuleHandle(),DLL_PROCESS_DETACH,NULL)
End Sub
1.2 Windows的标准入口
如果有需要线程启动和终止消息的,则需要使用这种模式的入口,需要自己复制代码实现,主要原理是通过参数命令-Wl -e增加一个入口点函数_DLLENTRY (在VFB附加编译选项中输入命令:-Wl -e_DLLENTRY,如果使用1.09.0的编译器,也可以使用 #cmdline来设置编译参数)。这里需要注意,32位和64位编译产生的函数名有区别(64位前面有下划线,而32位没有),代码如下:
'===================================================================================================================
'线程测试
Dim Shared pThread As HANDLE
Function TestThread(ByVal dat As Any Ptr) As DWORD
Print "hModule=";*Cast(HANDLE Ptr,dat)
Print "in thread"
Return 0
End Function
'线程测试
'===================================================================================================================
'===================================================================================================================
'该方法使用-Wl编译命令来设置入口点
'编译命令如下:fbc -s gui -dll -export -Wl -e_DLLENTRY
#cmdline "-Wl -e_DLLENTRY" '添加入口命令(fbc编译器支持的情况下)
#Ifdef __FB_64BIT__
Function DllMain Alias "_DLLENTRY" (ByVal hModule As HANDLE, ByVal fdwReason As DWORD, ByVal lpReserved As LPVOID) As BOOL Export
#Else
Function DllMain Alias "DLLENTRY" (ByVal hModule As HANDLE, ByVal fdwReason As DWORD, ByVal lpReserved As LPVOID) As BOOL Export
#EndIf
Select Case fdwReason
Case DLL_PROCESS_ATTACH 'DLL被加载
MessageBox(0,"DLL被加载","hModule=" & Hex(hModule),0)
'===================================================================================================================
'线程测试
Dim dwID As DWORD
pThread = CreateThread(NULL,0,Cast(LPTHREAD_START_ROUTINE,@TestThread),@hModule,0,@dwID) '开启线程
'线程测试
'===================================================================================================================
Case DLL_PROCESS_DETACH 'DLL被卸载
MessageBox(0,"DLL被卸载","hModule=" & Hex(hModule),0)
'===================================================================================================================
'线程测试
CloseHandle(pThread) '关闭线程
pThread = NULL
'线程测试
'===================================================================================================================
Case DLL_THREAD_ATTACH '单个线程启动
MessageBox(0,"单个线程启动","hModule=" & Hex(hModule),0)
Case DLL_THREAD_DETACH '单个线程终止
MessageBox(0,"单个线程终止","hModule=" & Hex(hModule),0)
Case Else
End Select
Function = TRUE
End Function
FBEdit测试工程如下:https://github.com/rainheart311/FBEdit-FullProject/tree/main/Enter%20Point%20%20FreeBASIC/DllMain
二、WinMain入口点
Function WinMain(ByVal hInst As HINSTANCE,ByVal hPrevInst As HINSTANCE,ByVal CmdLine As LPCTSTR,ByVal CmdShow As Integer) As LRESULT
'这里创建窗体
End Function
'{ Program start
WinMain(GetModuleHandle(NULL),NULL,GetCommandLine,SW_SHOWDEFAULT) '这里调用入口点函数
End
'}
'Program End
FBEdit测试工程如下:
https://github.com/rainheart311/FBEdit-FullProject/tree/main/Enter%20Point%20%20FreeBASIC/WinMain
三、Main入口点
Function Main(ByVal argc As Integer,ByVal argv As ZString Ptr Ptr ) As Integer
print "param";argc,**argv '显示参数值
print "end"
Sleep
Return True
End Function
End Main(__FB_ARGC__,__FB_ARGV__) '调用入口函数,__FB_ARGC__,__FB_ARGV__是控制台传入的参数
FBEdit测试工程如下:
FBEdit-FullProject/Enter Point FreeBASIC/Main at main · rainheart311/FBEdit-FullProject · GitHub