|
转载:; B7 G# k8 Z* k" K# `6 Y. i$ C
来教你如何在vb里嵌入汇编!
, E- J+ `" Y* C0 Q, S' `作者: wl3000wl 2 N3 W3 g. S ^8 v5 k
本贴绝对值得你珍藏.
; L! o* I: d/ [& ^6 I. R! v下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.8 d0 L; ~3 g3 i- M3 b
工程文件分为一个form1.frm 和一个模块module1.bas. c; l2 x( c5 z3 I
----------------------form1.frm的源文件---------------------
% M, ~( B+ \. A" }" _VERSION 5.00
( D' N3 ~+ ]9 C+ FBegin VB.Form Form1
& e8 z! r+ _- z1 |* a: z Caption = "Form1"/ r" C X3 z3 m; y$ B9 @( X; _7 Q
ClientHeight = 19653 _2 i0 z2 H+ o5 A' \
ClientLeft = 600 \4 B$ u9 ~* q9 s' j- H8 x. @! p
ClientTop = 3454 Q; `2 T7 g! X9 u+ ?
ClientWidth = 3105/ \6 i* g! j" _, I, X
LinkTopic = "Form1"! l0 A2 l* t1 s6 w, z
ScaleHeight = 19653 P0 }1 j6 S, v1 V, {' D
ScaleWidth = 3105
- [( E/ A- x; \ StartUpPosition = 2 'Bildschirmmitte
# ?: d% J( Q1 p Begin VB.CommandButton Command1 : B: v, O& ?/ U) H: e0 N/ p
Caption = "Get CPU Name"
& n$ ~9 {7 m7 k4 |0 U0 {. o Height = 495! _( r3 D! @9 ]' j) G
Left = 840* a- o) D, s3 Q: G' W
TabIndex = 0( t4 V V7 j0 ` n, W
Top = 315
. }2 s6 ~( d9 A( @) G4 p Width = 1425
6 m3 t8 [/ S+ Z End
* d$ M" S) U6 c! |5 [" F9 \( J3 k Begin VB.Label Label2 1 V! V& x4 T) @ t5 _2 o6 A
Alignment = 2 'Zentriert
6 Y7 }3 Y: u1 e' ]9 x$ K AutoSize = -1 'True' n: Y* F' T) L: _% u4 \
BeginProperty Font
4 R4 u8 U' M& Y4 K- B6 i2 M! v. p Name = "MS Sans Serif"
& F' N% ]' u; |9 Y. C K3 S Size = 9.751 w: M: @- a% [; @* O" D- z
Charset = 0
b. ?2 X; z, J Weight = 400
) _/ T: s. b; ^: \ S6 P4 T# g5 Y Underline = 0 'False$ l6 |" b! C- N9 l
Italic = 0 'False; ]5 f, _% E8 M# _: x, L
Strikethrough = 0 'False
+ \ r$ J5 V1 X EndProperty/ _9 x* z% P5 P: U( W
Height = 2405 J1 t; J9 k e* [' k) @
Left = 1515! U, F3 F: X2 ]* B1 I6 \5 e
TabIndex = 2. Z/ h1 r- N, b; h! K/ i
Top = 1065
% g; M) Y) N4 l Width = 60
/ e7 w7 Z7 a1 [( O! P# d End
& L% w% r: U8 s5 P [% H Begin VB.Label Label1
1 n2 o1 l6 C# K) @2 a: q Alignment = 2 'Zentriert
2 F9 }7 F% d; k, C7 _ AutoSize = -1 'True) w/ G0 a' i, y" w7 Y4 m( W
BeginProperty Font 2 z, l3 V# ~( R8 D( k9 k; K- \
Name = "Arial"
4 T( S, L; i+ ~; P Size = 122 k7 l. H. F( s' f3 i- ~9 ?
Charset = 02 g2 O* I6 O+ I6 x
Weight = 7001 `4 F) }# `! W1 B# O' \9 b
Underline = 0 'False/ c; ?2 B& O5 y
Italic = 0 'False
! K% x9 ?4 s; X R' k5 F Strikethrough = 0 'False
2 X" `6 O, i6 n8 M/ M% t EndProperty
: G7 N' a$ i% T3 E Height = 285
. t( G9 b. z4 A. }7 i Left = 1515
* n L+ v) F! ]% c4 B( c TabIndex = 1
, j! X, W& H( Q( ^ Top = 13508 \' R$ j9 k) I6 U n4 e( `1 Q2 |
Width = 75
) O2 l# T u: C# D+ N End
3 v" u z5 \* z, hEnd
* }, ?2 L- ?& `- b7 {Attribute VB_Name = "Form1"
% m$ ?9 A0 f9 ]1 K5 u0 f. lAttribute VB_GlobalNameSpace = False
+ ^/ k" w+ w% c" G) F9 \8 ^% b$ BAttribute VB_Creatable = False
5 M8 F& d J: E/ m1 G5 |0 vAttribute VB_PredeclaredId = True
* B! k5 R0 x$ [- Q( _8 vAttribute VB_Exposed = False
2 }* ~9 e4 b: S% s- i9 F/ ?, h0 qOption Explicit
* e. B4 D2 |* P$ n/ j( f1 P/ V
# Y8 g2 i: L4 m5 v XPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
2 O6 U, i, M/ C( S$ Y5 t. z
8 Y2 I6 C+ X/ i R1 {& A/ F Label1 = ""2 ]4 F- F/ d- w& u3 m
Label2 = ""4 |9 D$ X( W8 @' U$ f
$ M; t- M+ d6 n6 M" M( u; AEnd Sub. H& X1 r. y, {/ e3 R( a
. b3 @' H2 G# I1 B7 G
Private Sub Command1_Click()- y& S- m' F+ A7 V& Y
% a) G) v. _/ C/ ]4 m' H Label1 = GetCpuName() & " CPU"/ T6 t/ X1 h0 @6 e' }9 a
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
0 [3 m5 k7 k$ V
l( `7 f# H0 f; N- ZEnd Sub
. N) _' u( v; q! P------------------------------end---------------------------------
1 l* t2 p. t0 A$ r8 N1 q& i9 W/ g- Q/ k* H/ @2 k
( R4 t$ Y U5 O
& |! F3 T) G+ t
N1 @3 D* Z% P$ e
3 S/ }3 e! H1 e1 m* c* T下面是modu1e.bas的源代码8 m4 {' p; I0 Y z: @# R% x) i
/ l8 c: H& b/ C8 B6 k
----------------------module1.bas的源文件--------------------------
5 L& P- F$ } SOption Explicit
1 @+ s2 Q6 b# q# C6 E$ ]'
/ o9 x0 e& v5 a1 F! D'This shows how to incorporate machine code into VB
. ?" f( n; w% g0 w$ ?- k$ k'''''''''''''''''''''''''''''''''''''''''''''''''''
$ C; T+ o K" \/ r5 G* [2 ~'The example fills the array with a few machine instructions and then copies/ a' ^' j/ @" c9 L3 H- n* B
'them to a procedure address. The modified procedure is then called thru
+ G) ]- N. p, S0 b: Q'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
) G5 \; ?) v$ j( G& P& i'
! j4 h/ g$ T5 a'##########################################################################
/ h' j$ }% Y$ {'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody- x" C$ k5 N; E# v* N1 {
'can fix that please let me know... UMGEDV@AOL.COM
. n( c0 [/ |# A, W'The Error is not present in the native compiled version; so I think it got$ M: f+ e/ J: R6 I) b1 ^
'something to do with the P-Code Calling Convention (strange though)...; M. V" R5 v5 ^1 v7 S" `6 D
'##########################################################################
! O4 _& k i% n( r'7 c, _* D; Y/ b0 G; c& j: G: B
'Sub Dummy serves to reserve some space to copy the machine instructions into.
# M" h1 r$ c9 c; l* q'
: ^, {: \5 h7 t7 c' @' F$ @1 n. |& p+ i6 r! k
'Tested on Intel and AMD CPU's (uncompiled and compiled)! N: v0 r, ^3 J8 \0 v0 g
'! {6 s) Q1 F H# w# e# s2 J- L
'
# I& {8 | t/ `7 v$ ]* oPrivate Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
& l" M; b! V4 l1 vPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)8 p! o0 E, j5 }' h1 Z
Private x As Long
. r+ ?' K) s' P# ?( k/ w5 o" d; T" V' X9 _. S
Public Function GetCpuName() As String0 r$ c' e0 V2 h3 @% p; [% L) m
. ~5 J% m+ B5 q$ n Dim MachineCode(0 To 35) As Byte. S2 S$ }( \8 O+ `- e
Dim VarAddr As Long% G5 v" x( E' U; r& |# w0 w
Dim FunctAddr As Long
" d, `9 `. F# D Dim EAX As Long7 Y; { R( o+ d; X9 c* e' ?
Dim CPUName(1 To 12) As Byte
3 m% x1 P% Y) u4 l3 l& @2 l; j* M* E / b+ m% G6 d0 L% k F% ^& P
'set up machine code& } v- `& N- ?! O1 u. V
8 M" a; [+ } R0 K MachineCode(0) = &H55 'push ebp
# F7 d+ _+ i' N* ?: Y( ]( ~
1 q+ j1 }; b* I4 N MachineCode(1) = &H8B 'move ebp,esp
0 P% |1 R' `9 h- l' a3 D MachineCode(2) = &HEC
; o1 `% N8 G3 W5 w% {# c s
2 k; `. W/ k* C' B MachineCode(3) = &H57 'push edi8 R7 D. [' Q* @% e1 ~
. |% m; v Z9 F- M1 \# \. J
MachineCode(4) = &H52 'push edx1 ~8 b5 }: T7 N+ E+ l% ~; h. E
5 h7 i) g, |; @- x
MachineCode(5) = &H51 'push ecx! P6 U& \& D) u3 C& D8 @
$ r; H; e9 U5 C/ |' N$ N
MachineCode(6) = &H53 'push ebx
1 \' u3 m9 @( ?3 U8 h% f( s: a X( l+ a 6 l5 N2 K( `7 J7 B- l2 G: ^
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
- m0 K+ Q$ }- @ MachineCode(8) = &H45
) X" U( v$ K- ^# z MachineCode(9) = &H8! a" G# p0 V; E+ D: ~
- Y9 u7 n! Z# K* c- O MachineCode(10) = &HF 'cpuid$ }: U* ]. a$ Z" {, Z0 I4 W
MachineCode(11) = &HA2
2 e; @; R5 [" a2 \# h% n3 k$ Z T: ~3 L0 J i* Q. @9 X
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
' ?( B9 w2 G- `- H& f s MachineCode(13) = &H7D4 g! `8 r2 y j0 }* M; V/ F. R
MachineCode(14) = &HC4 b8 A5 j# B: X" t, T, l# H4 b
2 _. E7 O t- i2 a% w1 L: O( ` MachineCode(15) = &H89 'move dword ptr [edi],ebx4 s% {3 }( t. h8 ` F
MachineCode(16) = &H1F
9 m$ G! K7 h0 u! o2 T0 s0 _ - i+ k3 \9 e3 J6 F* [$ S
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]2 J- I2 n8 { [9 G( a
MachineCode(18) = &H7D
, z% x8 S0 d& ?) m9 l g! ^- { MachineCode(19) = &H10
4 n6 z$ ]4 d \. f+ C ( ?& P5 p2 h, q6 @8 a' i R: Y
MachineCode(20) = &H89 'move dword ptr [edi],ecx
Y$ a- ?3 ~# r6 `+ B MachineCode(21) = &HF* r2 [" Y7 e4 x" M j( {$ z
& p7 H; b( c6 F5 x+ n
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
9 Z2 e- e% b$ a MachineCode(23) = &H7D
4 Z$ y: f4 ^; A MachineCode(24) = &H14
: Z9 d4 Q4 O1 ?2 j( @# Z3 ^3 o - y5 Y/ f. X" L% V5 V$ u+ w" O
MachineCode(25) = &H89 'move dword ptr [edi],edx
* I& P) ?# _; J3 R" {! Z5 o MachineCode(26) = &H17
# |% O9 l F9 c8 k6 ~; {
. v, r" p8 q9 g s2 q MachineCode(27) = &H58 'pop ebx- _$ z t, I3 c. x8 U* p/ m$ L
& }, u5 w1 B/ u8 e, M MachineCode(28) = &H59 'pop ecx
0 h P- ]( ]6 e- W5 y6 w' w
- [# @) y# ?- V" t# B9 ~- u- `; u MachineCode(29) = &H5A 'pop edx
) n7 ]- }* d! D7 ^, X! h+ e# m5 I+ r! h, R& r
MachineCode(30) = &H55 'pop edi8 a/ o+ I0 @* D, `1 j
t: M b, X3 A- l9 p% g
MachineCode(31) = &HC9 'leave/ m' `% a+ ]7 x4 T2 U% |
* O7 _' `* S& _- f! G
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
: V- l9 [) r8 o8 D* d: p0 ]9 E MachineCode(33) = &H10 ' but all produce the stack error# w* B) L# H" X8 e7 U% h
MachineCode(34) = &H0. V& n! R8 r; k" t% i
7 w5 D9 J$ d/ ?3 U$ {: e# V* Y x! M
'tell cpuid what we want
7 r, Q4 m6 U: v' N3 K4 X EAX = 0
! g, ^7 C3 P( t8 s; H: X6 k0 {
. _% y, n1 d a+ r% \' s 'get address of Machine Code8 X: f [! U6 o: p8 V" |- ^
VarAddr = VarPtr(MachineCode(0))
' S' s6 X7 _4 ?& k
1 ?) y% d; h8 u: j 'get address of Sub Dummy
( O) K$ F& {# u* l# k FunctAddr = GetAddress(AddressOf Dummy)
' [/ W" K$ q u6 I
4 @1 {/ A( x. V0 H 'copy the Machine Code to where it can be called0 z9 j# b1 u7 y/ f# W3 p% a9 ^
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code3 \$ M: E$ m& z9 ~+ a
& e* W# b5 g6 i
'call it
5 `5 S4 c. M5 T: V0 K3 R. O5 v2 S On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
0 k/ ~0 _3 I5 ^7 O CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
; l6 |6 K$ v" L0 N: h+ } 'Debug.Print Err; Err.Description
+ V. w1 k7 f% o: N- W 'MsgBox Err & Err.Description; F, m& Z) y! X, ]* D/ R) s8 D& i: q7 H
On Error GoTo 0! F. N4 `1 F, W2 D
9 h5 G5 }$ e9 O- w
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName* O( x# N4 W, Y) B3 g% a1 i
5 z9 N+ L& P' i0 S O: X" pEnd Function0 R* ~; H, {% F
) x+ C; N3 U& d$ G# M+ \( _; Y3 ]
Private Function GetAddress(Address As Long) As Long
! t) Y9 o3 B# P4 v/ m
0 j! x% K& p( o, u9 z GetAddress = Address
3 v. g, x+ x% [9 N
- g t$ W) P) L" z+ E5 l1 DEnd Function
& n7 j8 P3 y3 R2 F# x
1 Z$ y7 ~: |& |# A( i$ W$ lPrivate Sub Dummy()5 U9 v. H9 s5 _( o, {7 }' W
h1 A9 ~ Y6 [/ @' A
'the code below just reserves some space to copy the machine code into2 j- j+ G, k6 S7 p" ~2 f
'it is never executed
* }" l1 Y6 T, V, b# l) [+ r, _& K2 _( T2 [7 m
x = 04 l) N6 b; U+ W
x = 1- d7 y( q& _9 e" E* Q! c
x = 2- U+ ~/ l/ m7 b& I$ }
x = 3
8 ~4 e# r6 x/ f$ _- M x = 4
+ |- F" T% p ^6 ` E; ^, d x = 5" o" n% ]2 K6 g3 D5 A
x = 6# p% h% q. s3 L3 ?- j i: S
x = 77 x7 y E) M7 W8 _& K; G& W
x = 8$ y0 V N0 }2 E8 l5 \
x = 9
" [3 d0 u" q" D# d g x = 10
' L' v$ L2 P W5 X4 Z# J i1 S x = 0
- [' M" @; Y5 R7 l- v) }) {" l x = 15 {4 W% h% E; a3 a6 Y9 |1 L
x = 2
6 r% @3 M. f. G* r( ~ x = 3
: k' i# M* U- L. ?( O, [ x = 42 G5 R8 f' N j
x = 57 h: y) R) r: o: h8 M0 C: t9 [* R
x = 6
% a" ~" O: `6 b# l5 a* v6 R" [ x = 7 v& E' d) g' w
x = 8) W; H' ]) G: }/ s
x = 9
% r" i# S" X8 h) x x = 10
/ s/ S3 g2 m: A ?/ l- l7 P
- _* k% A5 ]! ]/ F! Q/ B2 bEnd Sub
+ f1 x' N& L* M5 j! z* q% h------------------------------end--------------------------------------
- p6 Q. ^ @- I
7 N5 C! m, H5 O% h8 ?
4 J; k/ x0 J' L1 |* @% H+ y% t& p& N6 q
|
|