News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Calling Assembler from Visual Basic for Excel

Started by jj2007, January 15, 2013, 11:58:32 PM

Previous topic - Next topic

jj2007

Attached a demo showing how to all a DLL written in assembler (using the MasmBasic library).

When called from Excel, the user can choose to
- generate an exception
- see debug info in a console
- cancel

Here is the Excel code:

Private Declare Function RetHello Lib "\masm32\MasmBasic\Mb2VB\MbDllX.dll" _
(ByVal StrPtr As String, ByVal Ret$) As Long

Sub MbRetHello()
    Dim rv As Long
    Dim Ret As String
    Ret = "This is the Ret$ - we use it to return text from the DLL to Excel" + Space$(100)

    rv = RetHello("Click 'Yes' for a crash, 'No' for debug info:", Ret)
   
    If rv = vbYes Then
        MsgBox "You clicked yes - but you will never see this box!"
    ElseIf rv = vbNo Then
        MsgBox "You clicked NO"
    ElseIf rv = vbCancel Then
        MsgBox "You cancelled"
    Else
        MsgBox Ret, vbCritical, "You had a fat crash:"
    End If
End Sub


This is the function exported by the DLL:

RetHello proc MyText, Ret$        ; $export        Called from VBA as Declare Function ... As Long
  VbDll                        ; serves to provide the SEH with the right return address
  Try
  invoke MessageBox, 0, MyText, Chr$("Do you want a nice exception"), MB_YESNOCANCEL or MB_ICONQUESTION
  .if eax==IDYES
        xor eax, eax        ; set eax to zero
        inc dword ptr [eax]           ; that's pretty illegal...
  .elseif eax==IDNO
        pushad
        invoke AllocConsole
        fldpi        ; set ST(0), just for fun
        deb 4, "State of your application right now:", eax, ecx, edx, ebx, esi, edi, ebp, xmm0, xmm1, xmm2, ST(0), ST(1), ST(2), $Ret$:50, $MyText
        Inkey "Hit any key to exit"
        invoke FreeConsole
        popad
  .endif
  Catch only
        invoke MbCopy, Ret$, Cat$("Exception in DLL:"+CrLf$+"Address"+Tb$+Hex$(LastEx(addr))+CrLf$+"Code"+Tb$+Hex$(LastEx(code))), -1
  Finally
  ret
RetHello endp

Unzip the attachment with "use folder names" to your Masm32 drive, then launch Excel (tested with 2003, Excel 2010 doesn't like the SEH and crashes for 'Yes', but debugging works). In the VB editor window, import the *.bas file and run the macro. The library source (\Masm32\MasmBasic\MB2VB\MbDllX.asc) is set up for use with RichMasm.

Gunther

Jochen,

thank you. Excellent work.  :t

Gunther
You have to know the facts before you can distort them.

Farabi

 :t Thanks

My current project is handling excell VBA. Why each time I had a project there always post about it, just coincidence?
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

jj2007

Quote from: Farabi on January 16, 2013, 09:20:07 AM
Why each time I had a project there always post about it, just coincidence?

No, I can read your thoughts ::)

Jokes apart: What are you planning with VBA?

Farabi

Quote from: jj2007 on January 16, 2013, 10:14:00 AM
Quote from: Farabi on January 16, 2013, 09:20:07 AM
Why each time I had a project there always post about it, just coincidence?

No, I can read your thoughts ::)

Jokes apart: What are you planning with VBA?

I had an order to create an academic system. A simple one.
http://farabidatacenter.url.ph/MySoftware/
My 3D Game Engine Demo.

Contact me at Whatsapp: 6283818314165

Gunther

Hi Jochen,

Quote from: jj2007 on January 16, 2013, 10:14:00 AM
No, I can read your thoughts ::)

that's very impressive. You're a mind spook.  :lol: :lol:

Gunther
You have to know the facts before you can distort them.