|
转载:
& L3 y4 R+ a2 S/ a8 E6 Z+ r来教你如何在vb里嵌入汇编!
* R) a) X5 s) h" c* C作者: wl3000wl
6 a9 Q' [+ {' d* M% M本贴绝对值得你珍藏.1 V5 N( g/ e! V( R
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
# M; n/ ~- L! a. P工程文件分为一个form1.frm 和一个模块module1.bas$ y; m) c; o% L: s
----------------------form1.frm的源文件---------------------
* |; ~9 v0 `8 n+ KVERSION 5.00+ M% |/ }9 F) \0 o; D- C
Begin VB.Form Form1
, H' g; j1 t! A- W. Q0 Z Caption = "Form1"5 P8 F/ L7 r3 T# A( N
ClientHeight = 1965
1 b+ h% {( M; @ V3 b+ B2 `: U/ ] ClientLeft = 60% n1 a: z$ o, I- E5 e; e
ClientTop = 3454 y5 \9 q' J, x3 R6 U7 {/ Z8 i* N
ClientWidth = 3105
" X) N A7 F3 n% w LinkTopic = "Form1"3 |% B. R5 h8 a( s9 } r+ R9 V/ G
ScaleHeight = 1965
+ J. |7 L/ x' [% i @' o- Q ScaleWidth = 3105
5 I$ Y" l0 [+ n- r0 {) N+ } StartUpPosition = 2 'Bildschirmmitte( o* L4 c, H) G3 F4 S
Begin VB.CommandButton Command1
+ Y, ]9 s% X9 h8 c. y( K Caption = "Get CPU Name"5 N U4 x- J% b" \: y
Height = 495- w; s( A7 [1 j! R% L9 j- _* p9 h
Left = 840
6 I6 k+ X8 N- D* u- J TabIndex = 0
, l) d' H3 a: k$ T4 a; W Top = 315
5 R! N. A( |3 {# ]% z Width = 1425
/ }! Z g9 z. b End; @, z9 Z, ]1 R& ~' i* t
Begin VB.Label Label2
: a, A. ~7 | o- q1 G6 O Alignment = 2 'Zentriert4 h/ m* X+ z* O6 y
AutoSize = -1 'True8 k3 f v+ [% b
BeginProperty Font * @" b" s1 c- }; C5 F
Name = "MS Sans Serif"6 \' J; ~0 s6 p
Size = 9.75
+ N: t8 B. a* G( x Charset = 0* q- U- [9 R( L# a% j2 s0 {
Weight = 400
- ?. f d' H* ]8 B2 x$ Q Underline = 0 'False5 v5 }5 r! q3 ], M5 h7 H( A
Italic = 0 'False$ y% n) R8 S9 P3 h, Y
Strikethrough = 0 'False- _* V9 r4 @( ?" w* E; G/ F" D$ `
EndProperty
! `7 l' }# D- j1 L) J: N9 s Height = 2409 i7 I% a2 v# ^' d' ^
Left = 1515
9 ?# n& g) b% M( W. c TabIndex = 2
5 L! g" v# Z; ]0 Y4 k# l" \0 d& D Top = 1065
+ \+ ~! O) b7 Q3 H Width = 60* z9 ?+ C1 {! E' j
End$ K4 s" ?; v. n
Begin VB.Label Label1 4 q5 J9 M6 J# @$ P
Alignment = 2 'Zentriert
$ v' \& h" e" j, p( g AutoSize = -1 'True. q* Q# O# V# S8 r; U
BeginProperty Font : n7 z/ U9 ~4 t; C6 j' e+ @
Name = "Arial"
5 a P/ Y O8 ]% _: I8 ^, u8 a Size = 12
0 ~" P$ b' ?1 k% m/ T' i8 A Charset = 0
6 _$ I2 `" @. C e Weight = 7005 P0 N8 V, {4 ]7 V
Underline = 0 'False
8 {8 s9 y4 n" N! c: o" N1 X Italic = 0 'False4 }' F) g: _$ y8 ~( |
Strikethrough = 0 'False- Q( r7 d2 ?2 m+ e0 ~1 M
EndProperty: a0 S# q& N0 p0 ]$ f9 m( G
Height = 285. a. K% H% T/ B) a% A: d
Left = 1515
* z0 g( E( A9 o8 Y X TabIndex = 1" x$ t" D: Z# ]/ Y5 ]7 m
Top = 1350, H+ Z- `! O7 r7 `+ }
Width = 759 ?2 E+ M) D4 ?: f+ j& l
End
" x( r, v% S: G3 EEnd
" Z: Q$ W0 h4 L5 w7 I% c/ \Attribute VB_Name = "Form1"
4 V$ u$ E$ k+ |: p5 w+ bAttribute VB_GlobalNameSpace = False
; J8 z! S" G7 C3 Y& o$ B! DAttribute VB_Creatable = False! d3 J Z) [2 l- }
Attribute VB_PredeclaredId = True2 q( l0 Z- Q% n5 V
Attribute VB_Exposed = False# [7 h# N3 `: ]9 m9 T
Option Explicit; Z7 I0 Y% g% J6 Y( m
/ E8 q7 K) L% A9 s7 D9 X+ F
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
! X3 f) }: h* G/ J O5 e# @* {( L3 O2 j H* s
Label1 = ""
# Z& j, ^+ K3 _/ P0 {, E- W Label2 = ""6 w ~# P( H" m2 ^% k/ \1 m! a
9 O- L/ q. u7 ]9 _
End Sub& G9 O% ]: m3 l0 V
* r; S( h: o3 N& vPrivate Sub Command1_Click()8 a+ T; P5 I+ o; y9 i
! N* @) e1 a9 o0 l3 L
Label1 = GetCpuName() & " CPU"8 f# M8 X0 X. M& t" x! r, T
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", ""), z3 G3 W$ B: ?% l+ Z) S
- b" _. f* y5 h c) ?: ?1 CEnd Sub
2 p2 @/ x x+ e: D9 S------------------------------end---------------------------------3 }0 v' I `% C2 B/ W1 S
* i# B. E( g0 \, H$ k# c" E
6 |! l3 U" a& S
0 O @0 k/ W2 X" \( m* D* T( V6 Z( ^9 x# ?$ J* C
, T" j( A5 G5 {7 f) e* o
下面是modu1e.bas的源代码6 b% z$ l' v3 n2 u% b1 x
# \, z% T+ `# w- d: B+ Y. k
----------------------module1.bas的源文件--------------------------8 `: ]6 j( Y- o. c: A& K7 b
Option Explicit( h5 a9 g6 A: _6 B
'
( C$ d8 M& Y2 k) L1 G' o6 B" R'This shows how to incorporate machine code into VB
6 R7 R7 M3 l& h'''''''''''''''''''''''''''''''''''''''''''''''''''
9 A6 u9 C# n$ x5 H( O'The example fills the array with a few machine instructions and then copies! [5 `7 j6 u+ C L7 e, P( P6 L; b
'them to a procedure address. The modified procedure is then called thru3 l7 y- n" \/ a3 g4 O7 B" o
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name." b# P( O: C$ g o
'
2 m2 E1 R( Q: Z% B" ^4 X'##########################################################################- G. p& f% g" ^+ R' f
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody7 A p+ I, L$ C1 _& H
'can fix that please let me know... UMGEDV@AOL.COM5 t7 H; d& v# h% C2 |
'The Error is not present in the native compiled version; so I think it got
7 w( Z' v1 y p. ?" X'something to do with the P-Code Calling Convention (strange though)...' u. M3 U' C' ]* ~+ ~
'##########################################################################+ k; `7 Y* j& U" P! a: x
'
4 R. Q' {% f8 {2 E5 M8 ]9 }'Sub Dummy serves to reserve some space to copy the machine instructions into.
0 \8 _4 X. n/ M6 ~# K'
* I9 R T" e8 n; l: {. M4 K0 ~'
$ Y( R7 ^. L& X% L6 P! K'Tested on Intel and AMD CPU's (uncompiled and compiled)
( Z1 F M6 h0 S8 u'
/ Q9 g4 h% {, ~1 A( k: `; V M'
2 P( n3 X/ T! lPrivate 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
9 M, B$ @- g! s: y6 r- D( xPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
4 O4 D4 U" L, t0 \5 JPrivate x As Long
4 T, |' B' X. O
, G; \: z. W0 k( _Public Function GetCpuName() As String
1 a$ E @0 W, C2 v: l8 r2 P 7 j2 G4 I' a: D5 b
Dim MachineCode(0 To 35) As Byte4 A2 ^* H/ U8 D: Y0 T
Dim VarAddr As Long7 z4 @1 x" r# |' Q# R
Dim FunctAddr As Long2 H4 d# H" `+ l3 u1 c' h9 N; e
Dim EAX As Long7 z$ a/ c$ c( u; D$ t
Dim CPUName(1 To 12) As Byte- a+ s% e6 E }* O2 p+ H4 y' G
; }3 {5 ?( \0 w- T 'set up machine code
+ e# y' K# O% ~7 T7 i( ]& O " Y; w; p- }) A" J$ O
MachineCode(0) = &H55 'push ebp( }9 ^- X6 [, N }, Y8 G; I: F
+ Q1 z5 S' S$ [4 Y' }% i
MachineCode(1) = &H8B 'move ebp,esp
( b5 X+ j( i* [! X6 M/ Z X9 F MachineCode(2) = &HEC l' j+ e/ u7 d" [3 j* m; p
* J% E& `3 |# \) s/ v* a0 O! E" m MachineCode(3) = &H57 'push edi+ J1 D% V# X+ C, {3 ~. I
8 y) m7 @9 i! d ]3 E/ e+ _+ w MachineCode(4) = &H52 'push edx* {# e$ W1 G3 p, y8 [
/ {! p/ F& l+ r; A
MachineCode(5) = &H51 'push ecx
h4 P1 I( j# P ( H9 S) K% D4 h/ S, _
MachineCode(6) = &H53 'push ebx
: `2 x$ W7 s/ q" ]2 F $ n/ p1 \& [( c1 t. r+ v0 ~0 O- i
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]" P! x; d- d" Z/ E
MachineCode(8) = &H45- `% L6 Q- Q0 O; O
MachineCode(9) = &H8. n$ o8 ^4 i2 x4 I
3 v$ l* H; Q! z; \8 x- p4 A, J
MachineCode(10) = &HF 'cpuid$ H( Z; l+ e P4 b' y! d" ?
MachineCode(11) = &HA27 m. \+ s0 r8 C* ]: G* A
$ ]& O# C: D% n& @* a
MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
e/ n. w0 @/ p MachineCode(13) = &H7D3 `7 ^+ v2 a7 F! ]2 g
MachineCode(14) = &HC3 p, ?7 o! M4 T( c& t& {8 g
. N, q4 Z4 ~1 c MachineCode(15) = &H89 'move dword ptr [edi],ebx
% l' V7 A* ?0 B- M. M; }- {+ M: ~2 ^4 T MachineCode(16) = &H1F
5 [$ ]+ c! i* Y8 ]. Z0 g- [
- M6 N- f( L/ n8 P3 o MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
2 i4 r8 ]5 F+ P- A' h8 G S; Q MachineCode(18) = &H7D
8 p/ e7 j+ }$ |0 {; e; J1 j MachineCode(19) = &H10
8 r8 e9 w0 w& R
0 D8 M0 D6 \5 O" ?0 _2 h0 P( i MachineCode(20) = &H89 'move dword ptr [edi],ecx% A6 h9 M& o, a, f: Z9 K4 W+ R
MachineCode(21) = &HF
0 U) V+ w5 V# G+ M/ P6 H ) [! g/ N" |) f5 R Q8 J; G
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
0 l. o. a1 N0 }% Z1 M/ ]& Q MachineCode(23) = &H7D
/ N3 B1 x5 k4 E' p, [ MachineCode(24) = &H14
# @: @7 I0 u7 L" F* N ( I1 ^; d% G9 P
MachineCode(25) = &H89 'move dword ptr [edi],edx6 w0 L. X6 t z1 q6 b0 p
MachineCode(26) = &H17
0 c! {3 H6 f8 j" ` , q. ?( e4 {4 Y: g- B9 r. O) B
MachineCode(27) = &H58 'pop ebx6 X1 g; l: b- @, _5 k* M
9 Y6 O, C& b7 U% X" ?, C( I' j1 n MachineCode(28) = &H59 'pop ecx
" G1 i* e r5 f- n4 \) p n' O
2 [4 m* h E( G3 I d MachineCode(29) = &H5A 'pop edx t* Y3 j. b" t. a4 m
# C/ |; m) J# D5 _1 F+ @; O X
MachineCode(30) = &H55 'pop edi- F$ U5 h+ n7 e: A- R2 N
5 C. d4 ?7 D4 p# u0 F MachineCode(31) = &HC9 'leave/ f5 U N* Y1 P1 s
) C, i& m) o( v! ]" @ MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24 i, k! i! P" m, r0 }
MachineCode(33) = &H10 ' but all produce the stack error8 l: y1 j- C6 F; e# S
MachineCode(34) = &H0
: D* |6 O8 A: g
3 i& \( {0 \% [. t7 p) U) h8 d1 \ 'tell cpuid what we want6 @" C2 h* ] p, `+ N; `5 Z
EAX = 0
1 T& c' T$ k4 D# z! G 2 a! l; {6 V V+ `( x" @: s% z
'get address of Machine Code- G3 m! J8 l( N9 O& `
VarAddr = VarPtr(MachineCode(0))# m( M" b( v' X; E- T% B5 E3 t7 r
$ ^" m6 j4 _$ }3 T4 G F6 n5 g
'get address of Sub Dummy$ O( r: A' b# ~1 f c- {
FunctAddr = GetAddress(AddressOf Dummy)4 M/ j7 Z0 \5 R$ k
. H$ o- _. C$ I! p4 N! K 'copy the Machine Code to where it can be called
) Q, q K7 \$ g! ~: z CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code7 M, ?: H3 G% x: V* f, y6 P
) z! Z+ g) c b+ J, Z# Z0 }( S
'call it
- n" Y- J3 I; z On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
# |$ ]$ {0 x- h4 _: D) x+ n' l& E, ` CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))9 A% h* y5 m5 R
'Debug.Print Err; Err.Description- ~: J6 s8 l7 }! m0 C6 H
'MsgBox Err & Err.Description% n2 m% ~7 ?2 C: t/ S: k+ k
On Error GoTo 0
" M; V& \ b. u: `% g8 S) b
1 p( T% D1 K; |8 g GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
4 }' f( ^) P$ l
) k* y2 c1 c8 s, E7 e7 MEnd Function
5 Z% j1 z4 m( L9 r1 w# Y; U5 P: b8 U) B6 [& S' v9 @
Private Function GetAddress(Address As Long) As Long7 T% w5 U& A! ?) j) N! n/ U+ C. a
8 j. Y. v0 Q+ A6 `
GetAddress = Address
! b n1 I8 X( n0 H
9 u: O+ s1 u4 i" `% o2 cEnd Function/ l$ S* p, Y# W7 u
$ f/ T% b8 R. Q8 `& T6 x
Private Sub Dummy()' p3 }* k/ [1 V& [- N. g6 O! a, p
5 }1 O& u7 g' X& b2 V6 V 'the code below just reserves some space to copy the machine code into" w [% ?9 U1 C: d3 g
'it is never executed
: x: ]$ X$ W% U" k: S
h5 v% C8 F% P x = 00 V) {; ^$ ^+ W& O4 A
x = 1! c( G9 f' }$ E/ K8 ^- e$ {# _
x = 29 e4 }0 _6 W" {# W5 y4 O5 i$ H
x = 3
4 i) t& S% _7 C8 C x = 48 m9 I2 C" C' y9 W! \- j! ~' U
x = 5
4 J/ q3 l5 P d* P x = 6" h. p h d# B1 t9 Z& O
x = 7
9 |$ t" K6 ]: Z- I, s/ H x = 8
9 a. V/ [8 m$ y% I x = 9- c5 |/ ~- U& Z
x = 10
4 P; P9 Y, L' U( X( d4 F0 S. N x = 0
5 q" X3 d/ |( C3 w0 O* `2 Z' M x = 1. n8 f* g7 @2 C
x = 2
0 V+ ~# [" \: `2 x1 d x = 3: g. m; N" l1 |3 M) V, b8 h
x = 4
4 x: n( Y) k$ V; ]5 e, j1 d9 |' |( L x = 5& c! Q" L6 ?. D4 X4 D
x = 6
* e, s! ~8 k& x! y' Y x = 76 x* n$ s( H8 ?2 R% @
x = 8; y8 I1 Q/ ~4 q; s
x = 92 V" i/ }( D% L9 I" E
x = 10/ Z% }8 c6 N0 s
) R# O: C3 M+ REnd Sub9 A# t0 X! x0 m' p" T6 g9 I
------------------------------end--------------------------------------
6 A, a j0 Q" @& E) F
* L7 w8 N$ ?8 I6 Z
: k0 n# L! n+ Y/ r
& V/ C" a: i. y& z- t" h |
|