News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Exiting a DLL from Visual Basic

Started by jj2007, March 09, 2011, 08:31:32 PM

Previous topic - Next topic

jj2007

Quote from: drizz on March 10, 2011, 12:08:22 PM
Quote from: jj2007 on March 10, 2011, 11:29:47 AMThe problem is ...., MasmBasic
Now we are getting somewhere...  :bg
Why don't you implement seh macros for MasmBasic and have "Open" routine raise an error(exception) instead of "MsgBox".

drizz,
Thanks for the SEH lesson, but the problem is not MasmBasic - Visual Basic is the problem. I want a "graceful crash" if there is a coding error, such as opening a non-existent file, or asking for an array element with a negative index. The solution attached above works perfectly - remember the return address in a global variable, and return to VB with a "wrong" edi value to trigger the runtime error. Before the VB error message, MasmBasic will show the real error, and clean up open handles etc.

Marko

I don't know if this could help you, but when I had to trap the result of a DLL function(wriiten in C ) in VB (via On error ...) I always used the structure HRESULT.

VB doesn't interpret HRESULT as a normal handle to a result but to a special automation type that says if a fucntion is successful or not.

Here there is a description of the structure :    http://www.maruf.ca/files/caadoc/CAASysQuickRefs/CAASysHRESULT.htm

After i declare an ODL file (automation description)  to create a type library. At this point you can declare the fucntions in Vb and errors are trappable with On error goto statement.

jj2007

Thanks, Marko. It seems that HRESULT is set by COM interfaces, pretty complex for my taste. A DLL in assember is typically a snippet meant to accelerate an innermost loop.

Anyway, the solution posted above works fine. What was really nasty was the sudden exit of MS Word. Now if there is a runtime error in the DLL, VB stops and displays a 'The DLL returned an error' message.

Sub ShowString()
    Dim JJ$, ErrDesc$
    WantError = 1
    On Error GoTo MyErr
    JJ$ = "Bad luck" + Space$(1000)  ' Loads \masm32\include\Windows.inc
    For n = 0 To 22271         ' with 22273 lines max, 7 secs/Mio
        GetString n, JJ$
    Next
    If WantError Then
        GetString2 123456, JJ$       ' Trigger an error
    Else
        GetString2 2, JJ$            ' Show line 2 (zero-based) of Windows.inc
    End If
    MsgBox JJ$, vbOK, "MasmBasic returned a string from Windows.inc:"
    Exit Sub
MyErr:
   ErrDesc$ = "Non-DLL error:"
   If Err.Number = 49 Then ErrDesc$ = "The DLL returned an error"
   MsgBox Err.Description, vbOK, ErrDesc$
End Sub