|
发表于 2004-3-19 17:50:00
|
显示全部楼层
转载:9 z \! i2 p0 n1 b8 `% `* E
来教你如何在vb里嵌入汇编!
5 B1 f% I2 g' L- V- X) l% ^作者: wl3000wl 7 w: X9 W1 n; H3 E5 P6 w7 `
本贴绝对值得你珍藏.
7 n$ _( z2 P2 v; r1 j& Y$ f下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
+ e! g U% \5 |) N/ {& O' ^工程文件分为一个form1.frm 和一个模块module1.bas
. H0 z; Z1 n6 v+ u) a----------------------form1.frm的源文件---------------------
7 R0 R- Y8 F" g9 `0 N- V5 mVERSION 5.008 _3 |2 y, m- ]! F+ p
Begin VB.Form Form1
; X) t4 w5 ?* {6 m6 m1 f( ~ Caption = "Form1"6 i: z, K2 i( Q$ u1 q, [, ]8 K
ClientHeight = 1965
4 ]0 _1 K4 b3 I ClientLeft = 60
- G, I3 X! }# x8 {* _ ClientTop = 345
$ X) s' K9 P- e, H6 k ClientWidth = 3105
, Q. w/ p2 m2 Q* k1 ^/ c6 A0 b+ c) [ LinkTopic = "Form1"- y$ X& B1 Z* z$ I6 U0 {
ScaleHeight = 1965- U) G& n' V: t. h2 L' B1 }' _4 C8 K
ScaleWidth = 3105
" c2 O0 p4 D8 {- Z$ N/ t! d StartUpPosition = 2 'Bildschirmmitte3 c% C( e1 C+ E4 Z; W% i& |
Begin VB.CommandButton Command1
% V4 ~- m3 q |3 ]/ L Caption = "Get CPU Name"( P. }7 v( P( d- T, c" E) s2 ~
Height = 495
! D9 c3 X0 {6 i J Left = 840
7 t( c k. ~9 e3 p1 E4 A' { TabIndex = 0
0 w5 m1 z/ o$ n5 ]" S+ K Top = 315
; Q. z D% v( {' T3 D t4 f2 M Width = 1425" u* Y2 N- W& D# d y
End
7 l- u; y6 @% o/ M& A Begin VB.Label Label2
T8 \1 q4 J1 j Alignment = 2 'Zentriert5 ] g$ l+ n/ k) m
AutoSize = -1 'True" }9 l; ~% }! T! B$ q+ I
BeginProperty Font " z8 W5 K5 i) R
Name = "MS Sans Serif"
) ^' D6 A2 k8 x3 s; w" ?* b Size = 9.75
: W# m% I L3 B4 D) k Charset = 0
! G( H! q( S4 l9 E9 {( T: A Weight = 400
1 G! z9 I0 W# y3 ?) J8 ` Underline = 0 'False
' N6 X- J) p6 K& ^ l8 p+ X; u Italic = 0 'False
j0 \9 L1 W( y' l) B+ S( V Strikethrough = 0 'False
4 E! U$ m* ]1 O) e0 b8 q8 e EndProperty
6 X5 B* v1 ~) Q- u' L) H5 k Height = 2404 B- k& C" e' k0 B# `
Left = 1515" D, [) D9 ]' z6 w
TabIndex = 2" j0 L* q6 c! X5 K6 Q& \
Top = 1065# Z0 f) w, h9 y7 }
Width = 60
) M ~' }& A5 k% E$ ^ End
! {3 V' j0 ~7 w. q* I$ Q* z Begin VB.Label Label1 1 h, { L- {6 d' P2 J' n% Q
Alignment = 2 'Zentriert
3 Q) G/ f' r1 \& u$ ` AutoSize = -1 'True* r/ K0 }* b# H( B% _$ ~# _- t
BeginProperty Font 5 F. E0 n9 }/ }, W
Name = "Arial"$ j* q6 E% ^/ e* ~* x
Size = 12
& \, J3 n+ W5 [6 P5 f# E4 K Charset = 0( x3 V) T4 q% @! K( N; y9 T
Weight = 700
6 v! I& i' v* p% z5 T Underline = 0 'False
8 O6 @) n2 ?. J. r; Z Italic = 0 'False# Y: I! N4 L8 f2 B
Strikethrough = 0 'False* I; j2 E- {, h d+ z1 y
EndProperty
: m ~6 v2 g4 ^' P Height = 285( Y% r+ }6 D0 h0 k
Left = 1515" u0 ]3 [$ q6 Z; @' J/ c- v' ]
TabIndex = 11 i$ O Z# A5 r" b7 A9 m
Top = 1350
4 [$ y" G& j' m Width = 75
$ b0 B$ n# S$ X+ w" i: [) @ End' g3 p9 {! q( l5 v) d
End
! U7 {- b9 |) t' ? y8 aAttribute VB_Name = "Form1"
* W) J3 d0 V. S! {6 lAttribute VB_GlobalNameSpace = False- T8 o# \4 B) o' E
Attribute VB_Creatable = False; M n, P. F+ H. `3 \; L% B
Attribute VB_PredeclaredId = True
" q7 c q" ]6 Y8 V; M3 U5 I8 L6 A3 KAttribute VB_Exposed = False
0 ?$ D5 a! _* V b% r7 l4 T; WOption Explicit
, K( c* S( r( w; H+ U( Q# f: [8 c
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
2 h. l, O4 J8 k; ^1 G; p
6 Q; Y- g1 O' j" P# i% b' X0 N Label1 = ""; A' g" o" p* r1 e5 \% w; ?
Label2 = """ L* k2 J. T6 n+ W3 O
1 v, `& n8 }7 a$ D' B
End Sub
6 B3 o0 r8 k( U# O2 Z- U3 [( W( }4 v
Private Sub Command1_Click()
! P" M, W) c U; W; x& v
% k9 k+ B! V9 ^7 k- ]+ v; R Label1 = GetCpuName() & " CPU"- g7 m$ s- l. ~4 Y9 r
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")5 ^; z/ `) @! X3 ~
- l9 n$ j H# h# j8 HEnd Sub
% i/ W5 M* l, a# n* C9 f$ L7 D------------------------------end---------------------------------
( G5 \3 m4 o( |( B9 ~0 z# Q% ^" _/ W7 _9 l/ Q
5 d1 A' f2 e9 L: \/ D
! n$ ^, h( G4 Y" `5 S" u# q# G# E7 a
; c# [$ e% ]+ A
下面是modu1e.bas的源代码
9 A2 O3 s3 S, b8 p
0 R5 A& ~% H( b p0 C----------------------module1.bas的源文件--------------------------
) P% D! i/ @7 X K" dOption Explicit0 w1 v, J2 X0 Y1 ]) K' D
'
) {9 u; d( x: D8 j5 b6 R( @'This shows how to incorporate machine code into VB
0 U2 Y0 i! t3 c8 k7 a'''''''''''''''''''''''''''''''''''''''''''''''''''1 f2 E/ s: \, ]& u4 A9 b
'The example fills the array with a few machine instructions and then copies
6 W; K m: z6 h3 N: ^1 v2 ~" E* E8 l'them to a procedure address. The modified procedure is then called thru
* Y8 d: M2 d% \4 J% N6 k'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
, U( Z, F) }) u0 Q: a- j, v! G'
8 O w/ v5 r0 u" I1 ~'##########################################################################
! Y2 U( |# E8 U; a'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
4 \1 p$ H% \9 Z# P8 h'can fix that please let me know... UMGEDV@AOL.COM! h5 y4 c: d, J( f
'The Error is not present in the native compiled version; so I think it got( [6 B% b+ x# b3 D. @+ n( k2 t( K3 L" p
'something to do with the P-Code Calling Convention (strange though)..., z2 U1 c8 \. @. {2 A' l
'##########################################################################
: ?: ~2 T0 A9 g6 J'
- F a6 |& L8 f. o: M3 a0 s'Sub Dummy serves to reserve some space to copy the machine instructions into.
2 w, k. W& V1 a3 I( d'! P g% E ~& [2 j4 |+ A
'! R6 w& ~9 f6 r9 d8 ~. y9 l
'Tested on Intel and AMD CPU's (uncompiled and compiled)5 Y( Q, T: `6 B. z; `& L. r
'
9 Y8 { a# g/ x# V' L'
! |6 W; D; ]* I7 j0 J5 o3 L8 gPrivate 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
" b* y3 U A& B9 {$ ?5 g- aPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)4 h3 ~( j/ G+ C4 J/ s
Private x As Long
/ ]& z. P: Y0 x% c4 O# |
$ c& L% Q' c1 }' _- L4 jPublic Function GetCpuName() As String/ j6 V, r' l/ S2 P, Q
6 w5 T. p" {9 Z* J5 J3 p Dim MachineCode(0 To 35) As Byte$ T# l. f! {' q8 t! m
Dim VarAddr As Long4 @" h& b( w& P
Dim FunctAddr As Long
5 T5 v3 |: e7 G Dim EAX As Long( G5 H* x. x. P ]3 }, J* j2 T' \: k
Dim CPUName(1 To 12) As Byte
& Y! E6 F) T& q# T5 g 5 {; `3 g; m* j! L
'set up machine code
( O) V9 w& b) t6 a# ^ 2 e& A( f: W% d- ~9 v5 P( F# N
MachineCode(0) = &H55 'push ebp
4 Z) M, I. i4 t/ }' i8 a0 R" V ' p) q" |3 i7 d
MachineCode(1) = &H8B 'move ebp,esp9 e' i& E# s( h, l5 @& ^1 I& N2 f
MachineCode(2) = &HEC
4 c7 h$ \. k, w1 R5 @' R 7 c( ?0 B, p9 d7 A2 y9 P1 n
MachineCode(3) = &H57 'push edi
( r4 B" }' U( r6 s# } - C: D! j1 Z6 o! F; q8 s
MachineCode(4) = &H52 'push edx
9 U: c e& ^5 ]# t7 [7 G w
3 } p" U+ T, } MachineCode(5) = &H51 'push ecx
) X) U! ^" a( N- `$ M: l ( o1 j, ^' `, W% @1 M6 \" ~9 k( ]1 A
MachineCode(6) = &H53 'push ebx' o2 k2 r7 I0 E2 S9 R( r
) Z$ I" Q2 X# O* V5 A
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]" j3 w" v5 R* O, x) U
MachineCode(8) = &H45
+ M6 U0 R4 C6 w, | MachineCode(9) = &H8$ Q' T7 A% q3 Y$ P
# }5 T) y4 h1 q$ P MachineCode(10) = &HF 'cpuid
' @5 ~! s9 a2 F MachineCode(11) = &HA2" a) Z% C2 g. {3 }) ], M
% L1 \ t6 v: J9 I2 a( }1 q MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
8 W2 f* q# E/ @ MachineCode(13) = &H7D
" W9 s: Z4 a; V MachineCode(14) = &HC
0 H! }6 `3 c, p; h) z$ j , Z |+ B5 V, o, Z f5 a
MachineCode(15) = &H89 'move dword ptr [edi],ebx2 E3 m% G z& \3 t C6 `0 h/ u+ S% I
MachineCode(16) = &H1F3 _! c6 ^1 Q% S% o: i% P& P7 \
: @" a0 t* Y3 N# }& o MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]# F- i4 X) ?& C) h/ ]' N" W
MachineCode(18) = &H7D
/ z. }) L! m# {5 I MachineCode(19) = &H10, x( @- o# }- ^; Y# ^
6 [' z" H3 t0 Y3 u1 y+ P" ~( t
MachineCode(20) = &H89 'move dword ptr [edi],ecx+ k6 {3 X% D/ G' [5 p
MachineCode(21) = &HF
8 v, } b: m5 S* E" ?" m
) N* A8 p$ n o7 h3 E MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]! e1 Q1 p* ~. D5 r/ o- H5 g
MachineCode(23) = &H7D
5 h* Q5 x. u6 U- o$ ] MachineCode(24) = &H14: O5 l' @+ `5 d
6 H. A j9 j. ]" d MachineCode(25) = &H89 'move dword ptr [edi],edx0 X8 ~ o5 s5 p# K
MachineCode(26) = &H17
" T/ J5 e% Y! g/ W 0 }6 _8 e& j2 e3 a: M r
MachineCode(27) = &H58 'pop ebx
8 `) S) s- e7 g; i) _
) M5 T2 N& @2 u' Z MachineCode(28) = &H59 'pop ecx0 @# I' B R3 U4 s! A
/ l, }$ b8 Q- Z1 ]; P8 ?" A3 H! l MachineCode(29) = &H5A 'pop edx- h! a& ~6 A3 r7 t& p5 O
2 X* R& s2 S/ ~/ z, R5 f! c
MachineCode(30) = &H55 'pop edi
- Z/ o5 Q ^3 D
, Z, s* Y4 s, y/ b MachineCode(31) = &HC9 'leave5 s( X0 |7 R. G6 h/ ~: D( v
* `( b! D+ N3 G: j' ~3 g
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
1 F6 R& ?( L+ H: O/ A' W5 l; Q, ? MachineCode(33) = &H10 ' but all produce the stack error
! g( j0 d5 y Q8 U; L$ z MachineCode(34) = &H0
6 \3 r ?$ L) c6 w" }% h, S " w; r! U/ e; R2 T, O
'tell cpuid what we want
! t: j3 f: T' }' O* S9 J1 g EAX = 0
8 V+ F, t7 M# Y8 B1 V
( h" P1 }. D4 Z h1 w5 Q" S 'get address of Machine Code
. A) }% A/ U9 F2 }1 N VarAddr = VarPtr(MachineCode(0))
p2 X" e% R+ i V( g % g V5 ^* `5 ]4 v( q: d6 |
'get address of Sub Dummy
: R9 y( p e: \2 @ FunctAddr = GetAddress(AddressOf Dummy); p9 ?' N* Z* D3 ^
` N8 Y, q8 P% O; D 'copy the Machine Code to where it can be called
* h3 V/ X+ F& K' R: X% C2 B" B& j CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
2 O; m4 t0 H+ M2 K2 `" F, A) K
1 y' Y8 o7 ^' u4 y+ J& ^$ o 'call it
9 E* o4 m4 E6 o7 V- C& I. l a On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
% u; b i# h: _8 b! t$ E CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))& t% W6 V' g7 @+ Q
'Debug.Print Err; Err.Description
( U# E. F5 S8 p 'MsgBox Err & Err.Description1 J1 l: n k# ^2 B$ q8 ?, m6 N
On Error GoTo 0
+ @7 y" H! A( F4 i 6 P: {+ k f6 z
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName* q; U* u' e" d! m2 h
% K' e# L& ?4 ]) u% G+ o* H$ c7 Y5 GEnd Function
* Y& g! T9 S3 v; [2 h3 r6 J
$ ?! g6 Z. I" P9 l9 A! ^3 CPrivate Function GetAddress(Address As Long) As Long6 `. Y. P& z7 v4 B
4 k' G `4 h o
GetAddress = Address
4 t+ x/ d% B. Z: H u" |& O% V ~) [# B; Y; ]4 x s
End Function
2 Z. y- R8 s6 ~* f' m9 D( j0 Q! |! H# o, @2 O: u
Private Sub Dummy()' @, q) \8 O& ]8 v' x
% a# C+ I# l8 D4 b2 I
'the code below just reserves some space to copy the machine code into( r7 \; v& W8 l P
'it is never executed$ B( m3 `" h% J% M: M1 k3 @
) V0 T5 ?9 Z+ v+ B: L6 c x = 0
3 R- L; ]( j& ~ x = 1) d$ `, h# w5 l3 _3 h1 a% z
x = 2
0 T2 P8 `& s7 b( ^3 [' F1 D x = 3
( ?$ f& O1 V% z x = 4
. v& d0 c. d0 \. ? x = 5
% r( j+ J/ B2 ?5 d0 x x = 61 |1 ?2 a- A9 J8 G
x = 75 W+ j6 O9 J: h0 F0 S; k7 \9 V
x = 8
& Z7 M7 Y% c' m9 U, U5 ? x = 9
8 f0 ~7 k7 k: q# g7 W* X. | x = 10' I( D$ T% a* |3 ^0 X, ~
x = 0
. q& C& h. \% @. c x = 1+ ~1 S: Y/ p% Y! M* h
x = 22 A0 ?0 `) Y; N' A0 K. {
x = 3
% Q# `5 R0 f! A$ z* {: i; ^- w$ b3 Y x = 43 }0 B: N) L W7 V
x = 5: @" a8 t3 Z, N4 y1 k
x = 6
/ w" E3 J! q1 J. Z: V x = 7
1 @& m9 g l. ]% t: Q! E4 z x = 85 j* W1 p0 q" L) L
x = 9
$ N$ m" |/ H! L% D+ E8 R& r x = 10
" M' O3 s; v8 F u5 H & u; i( I. {8 @0 H9 w: B! ]
End Sub
6 G) o& g: V. L1 a; F; y------------------------------end--------------------------------------
7 j/ F; [2 D! N `0 y. N% j) ^$ o5 _# {3 o# q. F2 K
: ]( F0 j- I+ t; ~( X' i# |& ]2 a5 N0 J: j- y3 s: s6 M- v
|
|