Qbasicnews.com

Full Version: How to call functions in external DLL?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I donno much about windows program and just want to study how to call functions from DLL files.
I find an example http://www.newlisp.org/code/VB6.zip and modify it to
Code:
Option Explicit

Declare Function  dllNewlispEval CDECL Lib "newlisp" Alias "newlispEvalStr" (ByVal LExpr As String) As Long
Declare Function LoadLibraryA Lib "kernel32" (ByVal s As String) As Long
Declare Sub FreeLibrary Lib "kernel32" (ByVal h As Long)
Declare Function lstrLen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Declare Function lstrCpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long

Dim Shared NewlispHandle As Long

Sub LoadNewLISP()
    Dim mylib As String
    Dim mypath As String
    Dim hinst As Long
    Print "in load"
    mylib = "newlisp.dll"
    NewlispHandle = LoadLibraryA(mylib)
    Print "NewlispHandle=",NewlispHandle
    If NewlispHandle = 0 Then
        Print "can not find newlisp.dll"
        End
    End If
    Print
End Sub

Sub UnloadNewLISP()
    Print "in unload"
    FreeLibrary NewlispHandle
    Print "NewlispHandle=",NewlispHandle
End Sub

Function newlispEval(LispExpression As String) As String
    Dim Result As String
    Dim ResHandle As Long
    Dim ResultCode As Long
    
    Print "in Eval"
    Print "LispExpression$=",LispExpression+chr$(0)
    Print "dllNewlispEval=",dllNewlispEval("1")
    ResHandle = dllNewlispEval(LispExpression)
    Print "ResHandle=",ResHandle
    Result = Space$(lstrLen(ByVal ResHandle))
    Print "Result=",Result
    lstrCpy ByVal Result, ByVal ResHandle
    newlispEval = Result
    Print "Result=",Result
    Print
End Function

Dim LispExpression$
LispExpression$="(+ 1 2)"

LoadNewLISP()
Print newlispEval$(LispExpression$)
UnloadNewLISP()
But the the display message is
Quote:in load
NewlispHandle= 1809055744

in Eval
LispExpression$=(+ 1 2)
dllNewlispEval= 3458168ResHandle= 3458168
Result=
Result=


in unload
NewlispHandle= 1809055744
what is wrong with the source? Thank you.