The MASM Forum Archive 2004 to 2012

Specialised Projects => Compiler Based Assembler => Topic started by: Bieb on January 01, 2005, 05:13:26 PM

Title: Inline in VB
Post by: 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?
Title: Re: Inline in VB
Post by: Phoenix on January 01, 2005, 05:56:26 PM
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
Title: Re: Inline in VB
Post by: Bieb on January 01, 2005, 07:31:11 PM
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?
Title: Re: Inline in VB
Post by: Phoenix on January 01, 2005, 08:32:44 PM
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...

Title: Re: Inline in VB
Post by: Bieb on January 01, 2005, 08:51:36 PM
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.
Title: Re: Inline in VB
Post by: Bieb on January 01, 2005, 09:26:14 PM
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.
Title: Re: Inline in VB
Post by: Robert Collins on January 01, 2005, 09:33:28 PM
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.
Title: Re: Inline in VB
Post by: Phoenix on January 01, 2005, 10:16:24 PM
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]
Title: Re: Inline in VB
Post by: Phoenix on January 01, 2005, 10:21:02 PM
forgot this:

REAL inline-assembly as in C++ is NOT possible with VB6, as Robert Collins said.
Title: Re: Inline in VB
Post by: 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'?
Title: Re: Inline in VB
Post by: MichaelW on January 01, 2005, 11:24:17 PM
I got this link from "BP" on the old forum:

http://vbfibre.persistentrealities.com/index.php?category=0&item=5&t=asm

The file links here:

http://vbfibre.persistentrealities.com/index.php?category=0&item=50

Were not working when I tried them, so here are the direct links:

http://www.persistentrealities.com/files/vbftemplate.zip

http://www.persistentrealities.com/files/inlineasm.zip

http://www.persistentrealities.com/files/exampleasm.zip

http://www.persistentrealities.com/files/ASMPluginTest.zip
Title: Re: Inline in VB
Post by: Phoenix on January 01, 2005, 11:56:43 PM
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

Title: Re: Inline in VB
Post by: hutch-- on January 02, 2005, 12:34:10 AM
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.
Title: Re: Inline in VB
Post by: Maven on January 02, 2005, 05:52:03 PM
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
Title: Re: Inline in VB
Post by: Mr. Sade on February 19, 2005, 09:48:44 AM
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!
Title: Re: Inline in VB
Post by: Bieb on February 20, 2005, 10:26:23 PM
Wow.
Title: Re: Inline in VB
Post by: AeroASM on February 22, 2005, 09:44:29 PM
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=24616&lngWId=1

The above VB add-in does inline ASM.

It is not truly inline, though It works by interrupting the compile process and generating an assembly listing of the module. Then you can add your own code and assemble it yourself. Then, when the compile process is allowed to continue, VB will link the .obj you created into the exe.
Title: Re: Inline in VB
Post by: Xor Stance on March 06, 2005, 04:34:08 PM
The visual c preprocessor that has ml 6.15, I also had heard some guys inlining it with Masm32 and VC toolkit it's specialize for that too.

I have vb 6 enterprise version with sp5 and vcp5, now recently Installed.
Title: Re: Inline in VB
Post by: Robert Collins on March 06, 2005, 08:37:01 PM
Quote from: AeroASM on February 22, 2005, 09:44:29 PM
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=24616&lngWId=1

The above VB add-in does inline ASM.

It is not truly inline, though It works by interrupting the compile process and generating an assembly listing of the module. Then you can add your own code and assemble it yourself. Then, when the compile process is allowed to continue, VB will link the .obj you created into the exe.

Can you post a simple example of how one goes about doing this?
Title: Re: Inline in VB
Post by: AeroASM on March 06, 2005, 09:26:59 PM
It comes with an example, read the documentation.

I do not have an example because when I last tried the add-in I didn't know anything about asm. Sorry
Title: Re: Inline in VB
Post by: Xor Stance on March 10, 2005, 04:00:07 AM
Doesn't it with the preprocessor you get inline assembly? Or you can use the Visual C toolkit inline asm, to compile the source code. Visual C preprocessor has ml 6.15 but if you need for another, that I don't know.
Title: Re: Inline in VB
Post by: AeroASM on March 10, 2005, 07:03:24 AM
He is using VB, not VC.
Title: Re: Inline in VB
Post by: Xor Stance on March 10, 2005, 01:13:55 PM
Oh sorry, I wonder if there's going to be a release for vb.