Author Topic: Calling Assembler from Visual Basic for Excel  (Read 9016 times)

jj2007

  • Member
  • *****
  • Posts: 13651
  • Assembly is fun ;-)
    • MasmBasic
Calling Assembler from Visual Basic for Excel
« on: January 15, 2013, 11:58:32 PM »
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

  • Member
  • *****
  • Posts: 4150
  • Forgive your enemies, but never forget their names
Re: Calling Assembler from Visual Basic for Excel
« Reply #1 on: January 16, 2013, 08:24:56 AM »
Jochen,

thank you. Excellent work.  :t

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

Farabi

  • Member
  • ****
  • Posts: 968
  • Neuroscience Fans
Re: Calling Assembler from Visual Basic for Excel
« Reply #2 on: January 16, 2013, 09:20:07 AM »
 :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

  • Member
  • *****
  • Posts: 13651
  • Assembly is fun ;-)
    • MasmBasic
Re: Calling Assembler from Visual Basic for Excel
« Reply #3 on: January 16, 2013, 10:14:00 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

  • Member
  • ****
  • Posts: 968
  • Neuroscience Fans
Re: Calling Assembler from Visual Basic for Excel
« Reply #4 on: January 16, 2013, 10:54: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

  • Member
  • *****
  • Posts: 4150
  • Forgive your enemies, but never forget their names
Re: Calling Assembler from Visual Basic for Excel
« Reply #5 on: January 17, 2013, 05:36:18 AM »
Hi Jochen,

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.