VisualFreeBasic/FreeBasic动态调用DLL示例
动态库导出函数为:
void __stdcall NL2GLW(wchar_t* p_lunar_hour,
wchar_t* p_lunar_day,
wchar_t* p_lunar_month,
wchar_t* p_lunar_year_era,
wchar_t* p_lunar_year_zodiac,
const wchar_t* p_calendar){
...代码略
}
VisualFreeBasic动态调用时这样操作:
'5.54版本以前的FF_TextBox_GetText接口获取文本会缺少一个字符,故此处重写
Function FF_TextBox_GetTextW (ByVal hWndControl As hWnd) As CWSTR
Dim nBufferSize As Long
Dim nBuffer() as UByte
'检查窗口句柄是否有效
If IsWindow(hWndControl) Then
' 得到的文本的长度
nBufferSize = GetWindowTextLength(hWndControl)
If nBufferSize = 0 Then return ""
' Add an extra character for the Nul terminator
ReDim nBuffer (nBufferSize*2 + 2)
' Retrieve the text
GetWindowTextW hWndControl, Cast(Wstring Ptr, @nBuffer(0)), nBufferSize+1
Return *CPtr(WString ptr,@nBuffer(0))
End If
Return ""
End Function
================================================================
Dim As Any Ptr nl2gl32 = DyLibLoad("NL2GL32")'无需带后缀名.DLL
If ( nl2gl32 = 0 ) Then
AfxMsg "加载DLL失败"
Return
End If
'声明要动态导入函数
Dim NL2GLW As Function( ByVal p_lunar_hour As Wstring Ptr, ByVal p_lunar_day As Wstring Ptr, ByVal p_lunar_month As Wstring Ptr, ByVal p_lunar_year_era As Wstring Ptr, ByVal p_lunar_year_zodiac As Wstring Ptr, ByVal p_calendar As Wstring Ptr ) As Sub
'寻址对应函数地址
NL2GLW = DyLibSymbol(nl2gl32 ,"NL2GLW")
If ( NL2GLW = 0 ) Then
AfxMsg "无法从开发DLL中检索NL2GLW()函数的地址"
Return
End If
Dim calendar As Wstring * 128 =>FF_TextBox_GetTextW(Text1.hWnd)'"" '"202104020001"
'GetWindowTextW(Text1.hWnd,calendar, 128)
AfxMsg calendar + "成功从开发DLL中检索NL2GLW()函数的地址"
Dim lunar_hour As Wstring * 5
Dim lunar_day As Wstring * 5
Dim lunar_month As Wstring * 9
Dim lunar_year_era As Wstring * 7
Dim lunar_year_zodiac As Wstring * 5
'调用导入函数
NL2GLW(lunar_hour ,lunar_day ,lunar_month ,lunar_year_era ,lunar_year_zodiac ,calendar)
FF_TextBox_SetText( Text2.hWnd,lunar_year_zodiac+lunar_year_era+lunar_month+lunar_day+lunar_hour)
DyLibFree( nl2gl32 )