当前位置:  开发笔记 > 后端 > 正文

SQLite UDF-VBA回调

如何解决《SQLiteUDF-VBA回调》经验,为你挑选了1个好方法。

是否有人尝试将VBA(或VB6)函数(通过AddressOf?)传递给SQLite,以创建UDF函数(http://www.sqlite.org/c3ref/create_function.html)。

VBA如何处理产生的回调参数?

要调用的函数将具有以下签名...

无效(xFunc)(sqlite3_context,int,sqlite3_value **)



1> rpetrich..:

不幸的是,您不能直接将VB6 / VBA函数用作回调,因为VB6仅生成stdcall函数,而不是cdeclSQLite期望的函数。

您将需要编写一个C dll来代理来回调用,或者重新编译SQLite以支持您自己的自定义扩展。

重新编译dll并将函数导出为后stdcall,可以使用以下代码注册一个函数:

'Create Function
Public Declare Function sqlite3_create_function Lib "SQLiteVB.dll" (ByVal db As Long, ByVal zFunctionName As String, ByVal nArg As Long, ByVal eTextRep As Long, ByVal pApp As Long, ByVal xFunc As Long, ByVal xStep As Long, ByVal xFinal As Long) As Long

'Gets a value
Public Declare Function sqlite3_value_type Lib "SQLiteVB.dll" (ByVal arg As Long) As SQLiteDataTypes    'Gets the type
Public Declare Function sqlite3_value_text_bstr Lib "SQLiteVB.dll" (ByVal arg As Long) As String        'Gets as String
Public Declare Function sqlite3_value_int Lib "SQLiteVB.dll" (ByVal arg As Long) As Long                'Gets as Long

'Sets the Function Result
Public Declare Sub sqlite3_result_int Lib "SQLiteVB.dll" (ByVal context As Long, ByVal value As Long)
Public Declare Sub sqlite3_result_error_code Lib "SQLiteVB.dll" (ByVal context As Long, ByVal value As Long)

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)

Public Property Get ArgValue(ByVal argv As Long, ByVal index As Long) As Long
    CopyMemory ArgValue, ByVal (argv + index * 4), 4
End Property

Public Sub FirstCharCallback(ByVal context As Long, ByVal argc As Long, ByVal argv As Long)
    Dim arg1 As String
    If argc >= 1 Then
        arg1 = sqlite3_value_text_bstr(ArgValue(argv, 0))
        sqlite3_result_int context, AscW(arg1)
    Else
        sqlite3_result_error_code context, 666
    End If
End Sub

Public Sub RegisterFirstChar(ByVal db As Long)
    sqlite3_create_function db, "FirstChar", 1, 0, 0, AddressOf FirstCharCallback, 0, 0
    'Example query: SELECT FirstChar(field) FROM Table
End Sub

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有