Solved

VB Application Source Code: Win32 API equivalent source code for VB Application

Posted on 2004-09-08
18
1,267 Views
Last Modified: 2013-11-13
Experts:

Hi,

My observation: Vb is a higher level interface, So every call is acually implemented as a Win32 Api function call.

I am wondering to see the Win32 API equivalent surce code of my VB generated application. Please let me know any way out/tools to view Win32 API equivalent surce code of the application written in vb. How can I Import it in VC++ project.

:)

Regards,
msjammu



0
Comment
Question by:msjammu
  • 5
  • 4
  • 4
  • +3
18 Comments
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12008668
Sorry to disappoint you, but the correct answer to your question is: No, you can't do it.

I have not come across any tool till date that can translate VB code to equivalent Windows API code, or for that matter import a VB project and open it as a VC++ project.

Even there is no chance of decompilation. Even if you get hold of a decompiler, it will spit out Assembly code, which would be almost unreadable.
0
 
LVL 6

Author Comment

by:msjammu
ID: 12012981

Thanks for Comments,
but I am still Optimistic,

I don't want any decompilation but only I want is something between my VB source code and actual object code.

If this is not possible I wuld like to know the complete process of VB compiler  translating the VB proram to object code/executable code. That may help in this.

Thanks and Regards,

0
 
LVL 22

Assisted Solution

by:cookre
cookre earned 100 total points
ID: 12013196
One of the problems is that the code generated by VB doesn't go directly to the WIN APIs, rather it goes into the VB runtime library, which then callls APIs as needed.

A good example of this is field level I/O.
0
 
LVL 6

Author Comment

by:msjammu
ID: 12013203
cookre:

Can you plz elaborate more?

Many Thanks,
0
 
LVL 22

Expert Comment

by:cookre
ID: 12013298
We'll use the field level I/O (reading/writing a series of variously typed field).  There are no Win32 APIs that let you do this.  Your program code instead calls the VB runtime library that, in turn, uses the APIs to load chunks of the file in question, interprets what it read from the file, then passes back to you individual field values.

This is also one way to make it easier to move a program between versions of Windows.  If your code had explicit API references in it, it would fail if it referenced an API that was OS specific.  Since the VB runtime libraries are OS specific or OS aware, your generated code is less dependant on OS version.
0
 
LVL 6

Author Comment

by:msjammu
ID: 12013316
Thank You cookre,

At last

>>>If this is not possible I wuld like to know the *COMPLETE* process of VB compiler  translating the VB program to object code/executable code. That may help in this

You may help in this also

Any links will also help

Regards,
0
 
LVL 3

Expert Comment

by:Belthazor
ID: 12013883
Hi,
vb is not opensource, and i don't think microsoft will give you the code (or as you call it: "the complete process") of translating vbcode into w32 api calls.

Belthazor


0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12016532
>> If this is not possible I wuld like to know the complete process of VB compiler  translating the VB proram to object code/executable code. That may help in this.

Not sure how will that help you. Anyway, there is no such documentation available. Yes, experts may use thier personal experience to explain you a few things in brief, but certainly not the "complete process".

I'd suggest go thru some documentation on compiler design etc. Because, at the core all compilers are same. They all work on similar lines. May be that can help you.

If you want I can tell you a few things,, but at the moment you are expecting way too much.
0
 
LVL 22

Expert Comment

by:cookre
ID: 12019693
I just took a look at a VB6 executable.  I saw no direct calls to any Win32 APIs.  There were, however, plenty of calls to the VB runtime libraries.

That also explains why there is no static link option for VB.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Author Comment

by:msjammu
ID: 12023784
RanjeetRain:

>>>Yes, experts may use thier personal experience to explain you a few things in brief,

Plz, If you or anybody can

>>>I'd suggest go thru some documentation on compiler design etc. Because, at the core all compilers are same. They all work on similar lines. May be that can help you.

You are right. Architecturally all compilers work similarly. but I want only in terms of VB. What I need is not general compiler design process.

>>>If you want I can tell you a few things,, but at the moment you are expecting way too much.

I will appriciate any type of help specific to VB compile process as I know little about core of VB compiler.

cookre :

>>>I just took a look at a VB6 executable.  I saw no direct calls to any Win32 APIs.  There were, however, plenty of calls to the VB runtime libraries. That also explains why there is no static link option for VB.

Thanks for the Help.


Regards,
msjammu




0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 50 total points
ID: 12025631
>>My observation: Vb is a higher level interface, So every call is acually implemented as a Win32 Api function call
This is wrong. All the string, numberical and bit operations are NOT API calls, but memory operations that are translated by the compiler in 2 steps into machine code. All variable declaration are not API calls, but memory allocation calls, while procedure calls are actually "jumpers". The 2 steps are quite visible in VB when you compile larger projects: every module is compiled into 1 .obj file, and then linked into a executable file. C compiler variants to the same: 1 compilation of .obj files, then linking into the exectutable file (for most users, the overall process is simply called compilation).
This should explain why you won't get ANY source code getting translated into a pseudo or language-portable "program code" of only API calls.

Next issue is that VB 6 and lower is NOT really object-oriented as C++ is, thus working on VERY different logic behind the scenes. You can try to match many things of C++ in VB, but you will never achieve the same level. Just to start of, C++ has a much more complete list of data types than VB has, only mention the unsigned data types that VB simple does not have.
This might issue you to think that C++ maybe can match everything that is in VB, but that's not true neither. 1 thing to mention is the C-style of strings: in VB, strings are stored like this in memory (behind the scenes): <length><string>. This means, that the actual string value is prefixed with the length of the data. In C, it is like this <string><null char>. This means, that the string is delimited by a character of ascii code 0. This technique allows some nice hacking programming in C, which you can never do in VB. On the other side, C can not really use VB strings (while VB can simulate C strings and call C functions by appending a null char to the string !

Final note:
you cannot "export" VB code and "import" it to C++ or any other language (except VB.Net to a certain degree using the wizards...)

CHeers

0
 
LVL 16

Assisted Solution

by:JohnBPrice
JohnBPrice earned 250 total points
ID: 12030572
Also, compiled VB is not really a true compiler.  With C, for example, you can write code using the basic C statements, and it is compiled into the CPU instructions to effect it.  An "if" might be translated into some MOV, a CMP, and a BNE (branch not equal) that will execute on the CPU.  If you do not use any C/C++ libraries, the compiled code is ALL your program needs to run.  This is not true with VB.  VB statements are not translated into CPU code.  Every VB statement is a CPU language call into the VB runtime, with appropriate parameters.  The VB runtime is the code which actually calls the windows API and does whatever it has to to "run" your statement.

It used to be that VB (and other interpreted languages) didn't even make calls into the runtime, they were just "condensed" into a data format, and the runtime examined each statement.  The old VB exe's simply had one bit of CPU code, and that was to load and start the runtime.  Note you can still compile your CP app into this old form, called P-Code for VB.  You might find a P-Code dissassembler, or even write one.  I wrote one once for a different interpreted language, and it wasn't that hard.  Just create trivial programs, look at the result, and see how they encoded your source.  None the less, examining either the P-Code or compiled EXE created by VB won't help you do anything but recreate your source.  The implementation of each statement is not in the VB generated code, it's in the brains of the VB runtime, so a "SELECT CASE" statement compiles into a call to the runtime's implementation of SELECT CASE.

0
 
LVL 16

Accepted Solution

by:
JohnBPrice earned 250 total points
ID: 12030710
There is, however, an easy way to see waht code the VB compiler does create.  Compile your app with "Create Symbolic Debug Info", Open the resulting EXE in C++, and step into it.  The debug info includes the original VB statements as comments, this, for example, was generated by Msgbox "Hi There".  Note there is no API calls, just loading up some addresses with unnamed_var1 (presumably my string constant).  The message box actually appears in the line 0040165B  

call        dword ptr [__imp____vba@001BD43C (00401018)]

which sure looks like a call into the VBA runtime.


00401653   push        ecx
00401654   push        edx
00401655   lea         eax,[unnamed_var1]
00401658   push        0
0040165A   push        eax
0040165B   call        dword ptr [__imp____vba@001BD43C (00401018)]
00401661   lea         ecx,[unnamed_var1]
00401664   lea         edx,[unnamed_var1]
00401667   push        ecx
00401668   lea         eax,[unnamed_var1]
0040166B   push        edx
0040166C   lea         ecx,[unnamed_var1]
0040166F   push        eax
00401670   push        ecx
00401671   push        4
00401673   call        dword ptr [__imp____vbaFreeVarList (00401008)]
00401679   add         esp,14h
0040167C   push        offset $L47 (004016a0)
00401681   jmp         $L42 (0040169f)
5:
6:    End Sub
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12030722
oops, I missed a bit on my cut and paste, here is the complete code for the msgbox

4:    MsgBox "Hi There"
0040160C   mov         ecx,80020004h
00401611   mov         eax,0Ah
00401616   mov         dword ptr [ebp-48h],ecx
00401619   mov         dword ptr [ebp-38h],ecx
0040161C   mov         dword ptr [ebp-28h],ecx
0040161F   lea         edx,[unnamed_var1]
00401622   lea         ecx,[unnamed_var1]
00401625   mov         dword ptr [unnamed_var1],0
0040162C   mov         dword ptr [unnamed_var1],eax
0040162F   mov         dword ptr [unnamed_var1],eax
00401632   mov         dword ptr [unnamed_var1],eax
00401635   mov         dword ptr [ebp-58h],offset ___vba@001BD378 (0040131c)
0040163C   mov         dword ptr [unnamed_var1],8
00401643   call        dword ptr [__imp____vbaVarDup (0040105c)]
00401649   lea         eax,[unnamed_var1]
0040164C   lea         ecx,[unnamed_var1]
0040164F   push        eax
00401650   lea         edx,[unnamed_var1]
00401653   push        ecx
00401654   push        edx
00401655   lea         eax,[unnamed_var1]
00401658   push        0
0040165A   push        eax
0040165B   call        dword ptr [__imp____vba@001BD43C (00401018)]
00401661   lea         ecx,[unnamed_var1]
00401664   lea         edx,[unnamed_var1]
00401667   push        ecx
00401668   lea         eax,[unnamed_var1]
0040166B   push        edx
0040166C   lea         ecx,[unnamed_var1]
0040166F   push        eax
00401670   push        ecx
00401671   push        4
00401673   call        dword ptr [__imp____vbaFreeVarList (00401008)]
00401679   add         esp,14h
0040167C   push        offset $L47 (004016a0)
00401681   jmp         $L42 (0040169f)
5:
6:    End Sub
0
 
LVL 19

Assisted Solution

by:RanjeetRain
RanjeetRain earned 100 total points
ID: 12030768
Oops! This is such a vast subject. I have no clue what we all are heading towards. Without thinking much, let me add some more to the heap.

Lets see what does copiling a VB program result in:


-> It compiles to a 32 bit application that cannot be run in DOS mode.
-> It links a static library that resides at X:\Program Files\Microsoft Visual Studio\VB98\VB6.OLB
-> It adds a refrence to MSVBVM60.DLL
-> It has many symbols like _CIcos    _adj_fptan    _adj_fdiv_m64   _adj_fprem1   _adj_fdiv_m32   _adj_fdiv_m16i    _adj_fdivr_m16i   _CIsin    __vbaChkstk   EVENT_SINK_AddRef   _adj_fpatan   EVENT_SINK_Release    _CIsqrt   EVENT_SINK_QueryInterface   __vbaExceptHandler    _adj_fprem    _adj_fdivr_m64    __vbaFPException    _CIlog    _adj_fdiv_m32i    _adj_fdivr_m32i   _adj_fdivr_m32    _adj_fdiv_r   _CIatan   _allmul   _CItan    _CIexp
-> It has usual code and data segment and blah blah blah



Regardless of how it compiles or how it links, it produces an executable which is statically linked to some runtime libraries. There may be a catch here. By modifying MSVBVM60.DLL you may expect to change the behaviour of a VB executable.
0
 
LVL 19

Expert Comment

by:RanjeetRain
ID: 12030838
Being a Assembly programmer can help. No, understanding Assembly wont help a lot. Not atleast upto an extent where you can import it or debug it.

For your learning Windows API purpose, you should give up on idea of importing or seeing the equivalent API code translated from VB. For that my recommendation would be to read an Windows API book.

THere are many sites on Net that have excellent reference material. YOu can see several implemntation of VB's built-in controls and other poular controls. Most of them have been implemented using C/C++ and Windows API. I guess that would be your best bet.
0
 
LVL 16

Expert Comment

by:JohnBPrice
ID: 12030900
Now, if we tweak your original statement a bit, you are right on.

"My observation: Vb is a higher level interface, So every STATEMENT is acually implemented as a VB RUNTIME function call."

And about sums up the "complete process of the [VB] compiler".
0
 
LVL 6

Author Comment

by:msjammu
ID: 12032577
JohnBPrice:

"My observation: Vb is a higher level interface, So every STATEMENT is acually implemented as a VB RUNTIME function call." - Approved

:)

This is a nice discussion.
I am Extremely Thankful to all the experts for their contributions.

Regards,
Manpreet S Jammu
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now