|
转载:
2 U$ q' T( I: M6 v0 v$ m来教你如何在vb里嵌入汇编!( Q' h( h2 a+ @7 c3 X- h
作者: wl3000wl $ L5 a* ?8 M' b' l. J" ^+ l
本贴绝对值得你珍藏.
3 D: \1 i6 d, u8 R6 ]下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.7 d3 N! q R) j) n8 ~
工程文件分为一个form1.frm 和一个模块module1.bas; S5 \5 @1 A0 m& a0 z
----------------------form1.frm的源文件---------------------
" v) q. D4 N/ b0 [% jVERSION 5.00! f9 D! T( N' w; r
Begin VB.Form Form1 1 S1 W: U- s5 G
Caption = "Form1"9 S# h$ f' |; r5 o6 x+ B+ N
ClientHeight = 1965
: ]' S( r" H* q4 j; Q# @ ClientLeft = 605 f0 y& t b7 n4 R3 n
ClientTop = 345' I' D# V. s6 l
ClientWidth = 3105* v# i: ^3 o5 r J) [
LinkTopic = "Form1" l2 g/ c% I( h1 d
ScaleHeight = 1965
5 s6 f1 b" @4 P: |5 I9 K8 U! c ScaleWidth = 31055 I9 ]* z u4 f/ i% ?+ W* j, `
StartUpPosition = 2 'Bildschirmmitte
. R) h$ R4 K# t, y6 ]+ ^1 C Begin VB.CommandButton Command1 * F" g6 H$ g" {( X' M8 d
Caption = "Get CPU Name"! o% b7 r O7 @. x# c. _) f
Height = 4951 d) Q% A4 U. y4 E) g( h4 l0 O2 t
Left = 840
( q, h" h- b7 K TabIndex = 01 L8 e/ u- r* a1 }3 H" `" ^5 K
Top = 315' P0 R4 m4 h* V+ S) t5 ?
Width = 1425
/ T+ t( J! ?& I# Z; y7 z End
2 O- }; C, d: Z& ? Begin VB.Label Label2
: d3 R; @9 {3 L: }& A& s2 i& O) S Alignment = 2 'Zentriert# a! ^/ A( w3 F( \4 U- z
AutoSize = -1 'True8 ~9 Q l A2 a* C7 Z/ |1 T
BeginProperty Font
2 f! Y* t. v+ @: y Name = "MS Sans Serif"
* o+ W q8 i8 r$ s O Size = 9.75* T" @) C+ s: H! @ p
Charset = 0
' i/ @) f: C. j! b; b" h$ h Weight = 400& ^( E; |! g9 n/ \
Underline = 0 'False
$ D- I4 |* U# |( W8 Q. s8 \ Italic = 0 'False& z( V/ K+ I$ B
Strikethrough = 0 'False! }+ B" f0 K; o
EndProperty
. ^! D9 f D4 h/ _ Height = 240
5 l# f7 v( \/ g4 q Left = 1515
! k" S) K- n! @ TabIndex = 2
1 T' h, ^$ h+ S Top = 1065
! l' o$ R s' l Width = 60
X p# q) l, [. n! { End3 s. D( H$ D3 p' T# w/ K
Begin VB.Label Label1 3 C) e# ]/ Y6 ?3 U5 }
Alignment = 2 'Zentriert
* p$ u: a2 H+ b- K9 T, y AutoSize = -1 'True+ m2 t0 z6 @" u$ J, `
BeginProperty Font
) E7 ]9 W& i; l0 z Name = "Arial"3 j- E6 j( v& C w$ S; X
Size = 12; ~+ Q( l. a3 l. T2 b- R0 x& M
Charset = 0; C) I: T, W' m- e4 Q# r0 K, G G( [: V! e
Weight = 700
/ R4 f% x' Z4 b$ ~. F4 G Underline = 0 'False
* K. D0 b8 B5 z0 \/ R8 q Italic = 0 'False
' N5 `+ u: [& h Strikethrough = 0 'False- x' I3 ~* x0 M4 k: L0 g! N
EndProperty2 P9 T$ m# g/ b+ Y. E
Height = 2857 J' q) x, V; S2 l3 }5 e( F
Left = 1515# a% l0 K7 N- ^" B' F" o' a& R. N
TabIndex = 1# D1 a8 G' g" H1 T
Top = 1350
9 L4 x: e- Q+ C/ x, C, _ Width = 758 s0 v+ ?) J/ T
End
& T1 z# t: N+ i9 |% bEnd l& J( l c& z' u& w
Attribute VB_Name = "Form1"
1 { i4 z8 T6 \6 J1 ]% `: z3 vAttribute VB_GlobalNameSpace = False$ Q* L: e0 r/ i4 L
Attribute VB_Creatable = False
6 l3 K" ^+ N, K ~! m. w3 `: tAttribute VB_PredeclaredId = True
% c( q* }& k6 R, m& c& C; T7 W! KAttribute VB_Exposed = False
4 ?* y, s( t: N. l: k) H, |Option Explicit& Y r! h: j% O4 O
' q) D: b1 O# L* V, F$ G2 iPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
+ Q% \( f4 ~/ G
' @, g& s2 J: A( Q' C+ s Label1 = "", Z1 V) f$ H; ]- `
Label2 = ""9 B$ q& x6 g: G H# v
8 R/ z% \) P8 q! w6 v% j8 C
End Sub( V& L$ u, a4 K2 T1 K8 F, q) ]
0 H2 P( O9 s1 [$ kPrivate Sub Command1_Click()
7 s, c- ?5 A5 a& K) J) o
! y J+ I3 G* n% j Label1 = GetCpuName() & " CPU"% y7 u9 {0 N7 Q3 g* Y$ i
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")/ w( v2 l+ e6 w
" C# Y; {& B7 }/ C2 }4 F
End Sub9 A4 n" d1 a( U
------------------------------end---------------------------------! f1 z$ N) L' G `$ H5 d- r0 h/ B
+ u; W3 T T* F
F: Q1 R$ ~( V9 z- `9 O. N% P( w) k
) \2 K* F5 P8 U5 ?+ g( ?7 g6 m7 u$ V8 K
$ d' k( A$ z( L* ~0 D8 _- o下面是modu1e.bas的源代码
8 A) f+ C! d1 z$ `6 T
+ l, m6 D" A1 }, }----------------------module1.bas的源文件--------------------------
/ M! ` i" D* m& x$ t. C. oOption Explicit
" m0 X: ~% u4 p x'
5 Q. a' ?. y' p, f" [/ O0 N& z'This shows how to incorporate machine code into VB# D* f1 h' y% c% L0 F* a
'''''''''''''''''''''''''''''''''''''''''''''''''''9 A7 e' z) R' b% z& o) y; M
'The example fills the array with a few machine instructions and then copies: d4 H. M9 s# H. X
'them to a procedure address. The modified procedure is then called thru6 M1 H$ F) w0 u" u4 l
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
* w2 @* g- b2 |, {& W'+ l- o9 E) w) k" X$ j! N
'##########################################################################
7 z8 A1 @- w4 X% @'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody5 S3 `% b7 R) @* w3 l
'can fix that please let me know... UMGEDV@AOL.COM; Q* q6 e' Z/ {, L. ]/ P* V3 B
'The Error is not present in the native compiled version; so I think it got5 A, `. v9 Y; j. n9 x: _
'something to do with the P-Code Calling Convention (strange though)...
. V$ b7 r) l$ K' L'##########################################################################
2 [& a$ G/ |6 k; m'
& S7 G7 k9 Q, G5 z; X+ [& b6 \'Sub Dummy serves to reserve some space to copy the machine instructions into.
* A3 b& A* [5 L'5 a9 A. j. ]3 \( [/ ]
'
/ c" O) j7 E% z'Tested on Intel and AMD CPU's (uncompiled and compiled)
3 B! d0 @ t1 \# B" y'
2 Y2 q1 b+ C/ X3 L p'
, M; Z' c! l5 a6 x1 }: X( {Private 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
) z8 V; g" s' Z& e% Y: zPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
! z- y! b2 l* BPrivate x As Long
- z) ]; Y9 n' {. m& P* F8 |5 I s
6 \) }/ a! u7 t( k. J) pPublic Function GetCpuName() As String
' T8 _9 y& M- o# m ) ~ E/ z) d4 z4 ?7 h% `& W
Dim MachineCode(0 To 35) As Byte
3 _5 }, f( q0 Y$ F+ q; N) S Dim VarAddr As Long; `, E6 T: I) [2 Z, \9 E' l' t
Dim FunctAddr As Long
' P8 x7 h/ J1 z: l! L" \1 a @ Dim EAX As Long
2 j7 {; I% N3 S L8 u: Q; N1 c Dim CPUName(1 To 12) As Byte, v& [ d* p- |6 U
* s' i# f. ]5 T1 [ 'set up machine code7 W* h4 r3 K) t$ q. [; G
: O$ ~5 E+ W$ Z6 X! C' t' [8 j
MachineCode(0) = &H55 'push ebp
5 \& C& d* t7 l( T' w
. }. E: ?: R O% s MachineCode(1) = &H8B 'move ebp,esp
; P% _, z0 ], b$ W, }7 I( i3 \ MachineCode(2) = &HEC5 U+ @2 i Z4 y% M: v
" ~+ r; u& \+ o# x' o0 {
MachineCode(3) = &H57 'push edi
% X7 K8 E$ {5 B) R: K+ O' T+ ` 1 N! ?8 G) w! q3 E; V% w
MachineCode(4) = &H52 'push edx
7 {$ t% h( ^' }4 c- Z# Z& W 2 g" o4 d6 C) [
MachineCode(5) = &H51 'push ecx
+ ^' c9 `4 S; K* @ $ D" I& w& v" f5 g2 M; {
MachineCode(6) = &H53 'push ebx
7 J; W7 S( y4 }! A
& t* x% {2 C' @" D" @ MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]
R$ P# _, D0 @9 p MachineCode(8) = &H454 h1 a. Z/ r3 a9 S
MachineCode(9) = &H81 O* d. x, t+ l% \
2 F2 s8 F( u9 h6 k8 \/ \2 o
MachineCode(10) = &HF 'cpuid a3 `) L6 o) K
MachineCode(11) = &HA2
/ P! z0 u/ w3 k7 w ' t$ S' j1 \, `
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]" {/ f$ W2 v6 `# I" Q; B
MachineCode(13) = &H7D
$ L5 @. l! }: e+ z MachineCode(14) = &HC2 x, r6 E+ U2 B
* P# o" n* g" D8 l5 S% @6 r& n0 M MachineCode(15) = &H89 'move dword ptr [edi],ebx8 O! V; o' s7 [& C
MachineCode(16) = &H1F
! U6 \6 O3 m# w6 B$ n/ `% F
Q; r: ^5 c8 t4 i6 o. i. U; j1 ]7 ~ MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
2 a7 T" ~* `% A MachineCode(18) = &H7D( N, j$ z# g8 f3 N. i; F
MachineCode(19) = &H10
; |% n" m1 @4 T ( d5 Q L6 {5 q, W. N5 @
MachineCode(20) = &H89 'move dword ptr [edi],ecx# F4 D! y+ [1 |' i" r; n0 b
MachineCode(21) = &HF% L/ S7 G3 _# A: ?; T# F
3 b( Q& P( ^2 Q- R$ j MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
4 q; w) S$ _1 O2 a8 ?1 l MachineCode(23) = &H7D
6 ~: u3 u% _1 A6 g4 n% N, B MachineCode(24) = &H14
2 ^& i" P5 V7 s
. I5 s1 _ X, Y$ l MachineCode(25) = &H89 'move dword ptr [edi],edx7 H; c4 s. }' v2 N
MachineCode(26) = &H17
, c- c! r! ^% h3 r
- o, E) L+ |6 Z: d R7 u MachineCode(27) = &H58 'pop ebx
8 R- }( [4 G; r) H( ?% h X5 A/ g
7 T! n2 ?0 g: ~) c MachineCode(28) = &H59 'pop ecx
. r3 j m" M" m1 H+ H0 O+ Q" O, a1 u6 j9 X8 k0 e, O5 j* k% Z% q; D
MachineCode(29) = &H5A 'pop edx6 \, A9 }' F3 E8 F ?
- i' u1 F) U0 e( S
MachineCode(30) = &H55 'pop edi
& l" U& h3 |6 d7 Y; D& T5 F
O" h. _4 B3 N MachineCode(31) = &HC9 'leave
2 U6 u0 q6 b" E- p0 `9 U, `
. j* v. B, P; `! n' ~: L MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
Y) R# H# \) C5 @& _ MachineCode(33) = &H10 ' but all produce the stack error2 q+ k. s5 i6 f% x7 A/ b
MachineCode(34) = &H0) w( q. X6 ?$ k9 v; _' d" H
7 |4 E+ A6 u9 h 'tell cpuid what we want
2 h' H! E3 Q3 Y. D6 K( B7 | EAX = 08 e4 s+ N+ R, _: U) M
/ O2 y- u& B+ O6 I h
'get address of Machine Code! V, t, t2 O% S* B( a6 O; `
VarAddr = VarPtr(MachineCode(0))
. Z% J& P9 x4 K) }' D u 0 q+ U; U! B& u# v8 X* A
'get address of Sub Dummy$ _: l& T S f
FunctAddr = GetAddress(AddressOf Dummy), h( s8 T: J: Y2 | \
/ O# C' T$ [& \, L. x; }! s% M 'copy the Machine Code to where it can be called
* |1 m3 M' w; s/ N) W# b- x CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
- @& w/ r# v3 r7 {" @: _ & W$ k h) F& z4 O0 {
'call it/ Y3 o# s" e9 u J, j$ J
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why0 F- D4 P& x/ @7 @: ~
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5)): f+ d; {' E: d* n9 x* e2 O
'Debug.Print Err; Err.Description
% ]! m- [ c {2 O9 U) H' E" [ 'MsgBox Err & Err.Description, k3 F4 U& [- `3 w' |2 C/ o/ A
On Error GoTo 0# h6 [# c) x/ k. e; R# C: K: J1 z
' j9 `& n/ x% h% G0 L& K GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
, a4 G' N5 s# W! a& f- @0 h 1 K0 G( K: n, i0 C
End Function. g P0 g% a4 u1 F5 x( a% Z
# t+ y7 L6 w8 \+ j, OPrivate Function GetAddress(Address As Long) As Long
0 h& ]8 \- X, A( s/ B) M- s# u( r, o3 o. }/ f
GetAddress = Address1 g: @+ a" k+ _( h! w
$ I& h; [$ ]) |8 NEnd Function4 T( a( \- X: u
; o3 h6 \: [9 m! o: VPrivate Sub Dummy()
: E) v* Z" a2 T0 \' n
8 L u% b& w5 E& w 'the code below just reserves some space to copy the machine code into
; r$ \; E0 w3 T+ w" n9 m; [1 X$ }& E 'it is never executed
/ |. f+ M" u/ A- m1 ^/ i- J) \& I5 ^0 @: w
x = 0
2 `" X! T# }% k+ x x = 1
1 w# T8 A% W& ^8 q1 M, L x = 2
+ L/ ?" f# h: N( d- n) H6 t- L$ i x = 3
" Z1 \6 U6 b7 O5 s5 h; Q+ O! I2 ~ x = 48 J! ]# X8 h X8 ~ Q( s3 N! _
x = 5
2 J% ]3 _+ M- R' K! F x = 6- x" S( ~$ J5 c7 F" @+ V; b
x = 7# D9 o1 d8 _( J8 w8 L. H g
x = 8
8 ?" ~- Q, k1 E5 Q x = 9/ m( }7 F2 G' ]0 Q
x = 10! ?+ T P5 ?) N+ D1 k! K9 U
x = 0' Z# k: L; O( x# n" j6 V) a+ m
x = 1
5 n. K3 A4 B( c* |5 s" @8 K x = 21 Q/ ^, }1 B" N: i0 ~2 l: G
x = 3
# P% u- S8 K: _& V9 w x = 47 s8 C+ l \0 L( p0 x
x = 5
' L6 V# S( b1 u x = 6
- H9 g7 x( u! k- m. w: q3 p* R- s x = 7
8 v" Q, Z! t( f: b: h1 x5 \ x = 8
) a$ X7 Z% c) G- d) p8 h* C9 m x = 9/ ?5 B1 H6 x0 L8 r% f3 f
x = 10
1 ]7 Y z8 I: r7 y! [8 s) G8 Z
# L( t! C. M5 }9 G5 G! Q/ VEnd Sub7 o# L# v9 Y& P/ `) `! _
------------------------------end--------------------------------------9 ]% a$ t d$ K) |5 F' q
: z1 Y7 W. K/ ~( }
# Z. I* u+ `! `& U+ l- x3 a
: U0 m( f4 X$ f: w1 ` l
|
|