News:

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

Inline in VB

Started by Bieb, January 01, 2005, 05:13:26 PM

Previous topic - Next topic

Bieb

Assuming I'm absolutely hellbent on doing it, and you're not going to be able to dissuade me or sell me an alternative, what's the easiest way to use inline assembly code in Visual Basic?

Phoenix

Bieb,

inline assembly in VB6 was my first step to asm, and there are several ways to realize it. One of them is to store the asm-PE-file as an array of longs within the code, and then to call this with CallWindowProc-API. But there are a lot of restrictions (NO .data-segment i.e.) that make this way not the best one. I have a good doc that explaines how to do it, but it is in german language. If you are interestet, mail me and i'll send it to you.

For me, i think it is much better and a lot more flexible to code the things you want in a DLL and use it like every other dll in VB.

Regards, Phoenix

Bieb

Well, I'm just starting to learn German, but I'll take a look at this document, as long as it uses normal VB code.  Thanks.  Now, if I create a Win32 executable and store it in memory as an array, can I just call the first part of the array, or will I have to target specific locations within the file?  Could there be some way to get VB's CALL statement to work with an array of longs?

Phoenix

Quote from: Bieb on January 01, 2005, 07:31:11 PM
Now, if I create a Win32 executable and store it in memory as an array, can I just call the first part of the array, or will I have to target specific locations within the file?  Could there be some way to get VB's CALL statement to work with an array of longs?

Bieb,

here is the link for the tutorial, on page 5 you can get this as word document (I'm sorry, cant find it on my machine because of new installation :red).

http://www.activevb.de/tutorials/tut_asm/asm1.html

Within this tutorial you will find all explanations how to use an array of longs with CallWindowProc-API. You just pass the array as first parameter:

CallWindowProc(asm(0), Var1, Var2, Var3, Var4)

where asm() is the name of the array that holds asm-code. I don't know, but should be possible to call several procedures within the code. But i think it is easier to make different arrays.

You also need a tool to convert PE to hex array, you will find this in the tutorial, too.

However, you should think of creating a dll, this is much easier...


Bieb

Well, the automated translation of that page wasn't a whole lot of help, but it included a link to an MSDN article that I think will give me what I need. Thanks.

Bieb

Alright, so suppose I have the following OpCodes


50
53
8BC3
5B
58
C3


How ekactly do I go about loading them into an array and calling them?
The above code, by the way, does nothing, purposely.  I just want to test out this whole inline thing.

Robert Collins

I got the impression from Bieb's question that he was asking for a way to include assembly code in-line within a Visual Basic program. Kind of like you can do when you include in-line assembly code within a C program. I have never seen anything that allows this per se. I remember long ago that in Basic (not Visual Basic) we could 'poke' assembly code (not the mnuemonics, but the absolute binary) into some memory location and then call the first byte of that location. Whether you can do that in VB I have no idea.

Phoenix

Bieb,

using vb inline this should be:

Option Explicit

' asmExample
'
Function asmExample() As Long
   
    Static asm(1) As Long
   
    If asm(0) = 0 Then
        asm(0) = &H50538BC3:  asm(1) = &H5B58C3
    End If
   
    ' ************************************************************
 
    asmExample = CallWindowProc(asm(0), 0, 0, 0, 0)

End Function



I'm not quite shure about the byte order because never did this by hand. I have attached a vb-module that extracts code from PE-files and copies them to clipboard i. e. Perhaps you find it useful.  ;)



[attachment deleted by admin]

Phoenix

forgot this:

REAL inline-assembly as in C++ is NOT possible with VB6, as Robert Collins said.

Robert Collins

OK you guys, now you got me interested in the VB/Assembly stuff. So, what is a 'PE-file'?


Phoenix

Quote from: Robert Collins on January 01, 2005, 10:29:37 PM
OK you guys, now you got me interested in the VB/Assembly stuff. So, what is a 'PE-file'?

PE EQU Win32 Portable Executable File Format  :wink

http://msdn.microsoft.com/msdnmag/issues/02/02/PE/default.aspx


hutch--

Somewhere along the line in the dim past I remember later versions of VB used CL.EXE or one of its DLLs to generate compiled code and I think it was here that someone found a trick to plug in an object module.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Maven

Quote from: Bieb on January 01, 2005, 05:13:26 PM
Assuming I'm absolutely hellbent on doing it, and you're not going to be able to dissuade me or sell me an alternative, what's the easiest way to use inline assembly code in Visual Basic?

I would avoid using inline asm (It's not even really inline) with visual basic like that. It's a very hacky way of doing things and I think it would be best to just write a dll. But to answer you're question, you just write a small modual in asm and then generate the machine code listing for it. Store that in a variable and call it using the windows API with a pointer to the variable.

You would put it in the variable like so: 50538BC35B58C3

Anyway that is so hacky lol

Mr. Sade

You can use an add-in for vb6 called TweakVB, I have never use it though.
it says it can:

Statically link Assembler or C modules directly into your compiled Visual Basic executables.
Add Inline-Assembly right into your Visual Basic project for the ultimate in performance.
Generate Assembly listings of your Visual Basic code.
Use custom optimization settings on a per-module basis.
Easily call Function Pointers using just Visual Basic code. No type libraries, no DLL's or any external files.
Export functions from your Visual Basic DLLs, just like a normal DLL, with no external "shadow dlls" or the like.
Build Control Panel applets and more using only native Visual Basic code.
Increase or decrease your program's stack size.
Easily generate Map files to assist in debugging.
Much, much more!