|
转载:
* n; D; I( s4 k. Q4 U# _来教你如何在vb里嵌入汇编!# u3 Q* p2 ^; Y; d/ J
作者: wl3000wl $ c+ h6 g) a: m
本贴绝对值得你珍藏.3 E% N1 d: Y9 O" D, d6 S( D
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.3 J" A4 |6 ]3 c- [/ p( a6 i# g
工程文件分为一个form1.frm 和一个模块module1.bas
# |. G( ~+ H1 _) s j( A8 D; O----------------------form1.frm的源文件---------------------- S1 T- O7 E; q' t
VERSION 5.004 C1 s& G0 o- K7 T+ A! r/ X8 K
Begin VB.Form Form1
+ I9 T# m) W& p% t1 n! k N) G Caption = "Form1"
; `7 }) S- C. K" ?2 o- y ClientHeight = 1965) T" h5 K: @/ A) o0 b4 s8 _* O
ClientLeft = 604 F$ H. J7 Z* E; u0 F0 U
ClientTop = 345
% C& f: p2 c" l; Y: ~6 ]! ? ClientWidth = 3105/ A5 t7 V7 N7 m# l5 Y; W
LinkTopic = "Form1"0 B, q2 |- [0 v3 q2 }6 W
ScaleHeight = 1965
7 j. k# p& P/ s d7 ` ScaleWidth = 3105
; r+ E$ R: B4 d1 t- c) x StartUpPosition = 2 'Bildschirmmitte8 Y2 `! s( K2 l2 S% N9 i9 |4 Z& R
Begin VB.CommandButton Command1 9 c2 v& ~' t! U
Caption = "Get CPU Name"$ w; ~1 ~5 \2 B' ]: X, B2 k
Height = 495
) m% J2 y: ?7 m4 k$ G; d- y Left = 840
; a7 z1 ~$ N3 P" p TabIndex = 04 Y: H' U, N$ i$ C( W2 }
Top = 3156 R- N* U3 s e3 I4 G) |/ }5 z
Width = 1425
4 V1 `! I# R. G End5 |4 K7 j0 ~2 f
Begin VB.Label Label2 + r) l5 g! ^, [$ }# K
Alignment = 2 'Zentriert2 h3 n& V7 o' K2 x9 P( m
AutoSize = -1 'True
: I, N/ t: |3 y4 h8 l BeginProperty Font
2 @" Y9 D& p+ k; t; U Name = "MS Sans Serif"
, O- y* s' A9 V Size = 9.75
* y5 |& b8 M% @1 o. a( s, \( P Charset = 09 C7 w) \1 s5 U4 D) Q# ~
Weight = 4008 [. v% E, S% K) Z& y9 Z
Underline = 0 'False8 e) P; U; e6 `2 z: a" g; w
Italic = 0 'False+ l- v# v1 X+ ^; t# l' m
Strikethrough = 0 'False
5 e5 i9 O2 t9 T7 Q$ ] EndProperty
S) d( B4 g/ e5 G& P& H! M- B+ G% y Height = 240: }; `& n p/ U
Left = 1515
6 N) g% X2 L) V! h" o0 S TabIndex = 2
1 W" H' j& h" i5 { S { Top = 10657 C! [8 F9 }: p
Width = 60$ d$ Y( T2 c5 }1 G8 }0 N
End
$ u% B/ b& K* r' |* r9 ]5 e Begin VB.Label Label1 9 }* `+ u1 y; Q0 p) M
Alignment = 2 'Zentriert
0 m6 o0 {) O2 U4 u" [0 N AutoSize = -1 'True
" Q! b9 N: H, w BeginProperty Font 9 w9 a! O6 g; {! ~6 b" r
Name = "Arial"& `5 Z% R F3 B5 Y
Size = 12
1 u7 G5 P% S3 C* B7 B5 i1 ^% F Charset = 0
& c8 m, B* y5 t! N" T Weight = 700
1 ]5 F$ X9 X& j$ J. G+ Y Underline = 0 'False
5 ^- k" }3 q0 U$ x& _5 s Italic = 0 'False0 H/ v& ~8 v F
Strikethrough = 0 'False
p8 S& x$ j* X T$ U* O EndProperty
4 Q. H/ o- C9 i/ S Height = 2857 Q" O. A; g4 n) l' }9 Z% Q6 V
Left = 1515% i1 W) v; |7 y5 K. Q
TabIndex = 12 i* f; ]- y$ x! {. O
Top = 1350
$ ~) U4 P8 [5 D* ]3 [6 j Width = 75
+ m9 | j" { a }/ k End
+ ]6 a' W2 W- REnd
3 `: p) F, K [6 LAttribute VB_Name = "Form1") a* i/ h, T" M# D9 Z
Attribute VB_GlobalNameSpace = False7 W# }# w |0 `
Attribute VB_Creatable = False
: j: o9 l T1 _0 ]" zAttribute VB_PredeclaredId = True+ P; `. W6 n! k5 q
Attribute VB_Exposed = False
8 O0 N$ z Q5 n3 q! z3 ^! iOption Explicit
/ d" M% S8 Q$ k$ @% j. h1 ?& U9 u$ W; t6 p/ D
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
$ x/ w2 o4 b& D) Z4 p# E
; \% s. D# y+ i/ D Label1 = ""! E7 b2 v& _& d6 F! W
Label2 = ""' a0 n$ P0 b5 v7 d# r+ N! \
8 ^7 i* _; k$ n1 Q) X: KEnd Sub/ E" a8 j; R2 G/ a2 X
1 H+ D# J. [ ^" NPrivate Sub Command1_Click()1 L) D$ K' }3 I8 s! R
# r& @& P0 M) j1 v
Label1 = GetCpuName() & " CPU"6 t% t8 g% i8 H% v4 X' s- H
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
/ P1 E( @8 ]8 s. F
6 e3 k# [' ?4 j0 ZEnd Sub4 d# a' I( ~2 ?$ m$ ~ b, C" N
------------------------------end---------------------------------
* ~% W7 h& \6 B5 o6 R) W2 b" H1 {* J0 C
/ |, }& i" g! F# _4 L7 n! I8 x) V' ^
$ S8 z! [9 {* x: `! L; [0 i
) V+ A4 J- i5 O% W
下面是modu1e.bas的源代码; [! R: Z7 a+ V$ l' v5 D% \0 k
! d J' b9 I0 w) C
----------------------module1.bas的源文件--------------------------% z6 K" D, }7 O- @1 I x. Y
Option Explicit
& K& \( f9 n) O& b( y'
0 S n3 p$ M4 Y( J z5 {9 U& r1 B'This shows how to incorporate machine code into VB8 I" H3 i4 w9 b0 z6 _* s1 Z
'''''''''''''''''''''''''''''''''''''''''''''''''''
8 P" B2 G" e7 @8 q; ['The example fills the array with a few machine instructions and then copies
0 v- h% K' O3 _& z'them to a procedure address. The modified procedure is then called thru0 f% }6 e, o$ V' j& } n: j
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.* W: U# s$ ?+ y2 M
'
7 H( n) f% r9 ~1 g0 H" |'##########################################################################: i8 O+ t# I( I0 x
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody+ K$ V) S5 v. L. f5 r
'can fix that please let me know... UMGEDV@AOL.COM2 G! C4 Z! s! Q0 G2 `1 e6 R8 i+ P
'The Error is not present in the native compiled version; so I think it got
2 G- L) Q( q! W'something to do with the P-Code Calling Convention (strange though)...
H) ?/ W1 W4 v3 V2 G3 u'##########################################################################
7 d/ d) t5 P3 j V'
# R8 A# E' l3 g- E- v'Sub Dummy serves to reserve some space to copy the machine instructions into.
5 g. m1 A# Z0 {1 E: w/ u'0 H3 P, c2 |3 b/ ?9 i
'$ b$ n" y5 ~1 Y
'Tested on Intel and AMD CPU's (uncompiled and compiled)( S6 \6 l1 d A J
'# `1 `6 D# Z2 l i% K. M/ x
'
* Q/ F+ `) d" E: u' v3 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+ u, e3 I) e5 L6 f
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long) K* J. @2 z( a
Private x As Long4 \, m) Q: \5 ?9 a W
& R9 n" v. G1 i! K, O- k# t
Public Function GetCpuName() As String# P* i. z, }+ r$ J/ f0 _
' \. \( A7 x2 l7 ~3 c: ?( h1 G1 L5 n
Dim MachineCode(0 To 35) As Byte
. e# A+ K- S* G" |% j# q Dim VarAddr As Long3 k' K( m7 O& x4 _8 _# X4 X
Dim FunctAddr As Long
5 t' s# C) D& O( @ Dim EAX As Long
- X' C/ I- T$ s/ h1 h l3 w Dim CPUName(1 To 12) As Byte
8 ]) _- v9 P& K7 n
+ a3 T4 ~! L* J* O 'set up machine code" D" g- E1 o. L: l* m7 q
5 y" T8 [& y t
MachineCode(0) = &H55 'push ebp
( l( a F3 d6 K" Q & |7 ~4 |" m- O8 B+ H" v" t0 I1 q
MachineCode(1) = &H8B 'move ebp,esp
- y# x `7 p7 m. C4 R. d MachineCode(2) = &HEC' w7 `: N6 e: b% t% g
. b" G# H; @$ c' A+ l9 R" I( ?, S
MachineCode(3) = &H57 'push edi
/ V8 ]+ Q$ f/ R5 H) i
* c% T3 w/ i- I( u MachineCode(4) = &H52 'push edx
' u1 X+ z( g6 l" k" d. c 6 \1 l( q6 H6 n6 D: G
MachineCode(5) = &H51 'push ecx
+ L! y- t9 b5 Y5 t$ r2 @- y, J* x
! v% b8 ?) z5 O, U& w! V7 x4 k) ` MachineCode(6) = &H53 'push ebx. k& b! Z$ z' b5 B1 j
# z& w. @' l- [3 \4 q MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]5 M: p# R4 c+ |# z8 H- L
MachineCode(8) = &H45
, T7 k! Z8 p' i; { MachineCode(9) = &H8' B4 H+ G2 y, Z/ V6 h5 O& h& A& s$ X
( g. x, G! J2 c5 b MachineCode(10) = &HF 'cpuid
" s0 M% ?1 H* V! r MachineCode(11) = &HA2& A1 ^$ @+ j( c( b1 G
! n8 v' I, w B7 C8 b! o! e0 } MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]% m* K& ^( n* G1 X* y
MachineCode(13) = &H7D
% W7 }+ d. P$ o8 p MachineCode(14) = &HC
3 w' D4 g% D" t+ `" a ) P; N0 R. q/ A9 Y3 d `
MachineCode(15) = &H89 'move dword ptr [edi],ebx" ^* s( {- U% l* s3 W
MachineCode(16) = &H1F
$ S9 m0 ]0 O+ b& P
% w* g- h+ ~; L, _- r: h MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]+ G% D- K* E7 l4 c8 o7 T2 @
MachineCode(18) = &H7D
% |6 E" X4 ~1 E: \6 ~; w MachineCode(19) = &H10
+ v! D% }. @; Q) U; K8 b" N * ~9 L# C: p5 ~; g( \
MachineCode(20) = &H89 'move dword ptr [edi],ecx
* v$ Y! U. Q, k9 M. k4 l MachineCode(21) = &HF
9 r, j0 S# ~; W# h& G$ h, C9 U) u
' L1 l5 N" H7 }! d MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
- I8 S1 k% O$ {- j) c( o MachineCode(23) = &H7D7 @ v/ a1 r2 j& ^4 `' z
MachineCode(24) = &H14
* E6 d) N' {; N7 ~+ d: @ 8 ^; _- E/ B6 `6 E7 n
MachineCode(25) = &H89 'move dword ptr [edi],edx: L$ D+ V5 `" H$ e& K- C
MachineCode(26) = &H17
% L, K: c0 Z4 K ?$ u - }1 x+ L7 ^7 `- b
MachineCode(27) = &H58 'pop ebx
% J$ n- H7 m8 r+ w
+ R# B6 ~2 m( i/ R3 ] MachineCode(28) = &H59 'pop ecx
/ E: j, U: c; T7 v3 P5 j
( s. N: v$ ^0 Z3 `9 | MachineCode(29) = &H5A 'pop edx. Y" v2 X0 }- j
% e! }- x- h' Z( _4 I- q0 `1 A MachineCode(30) = &H55 'pop edi5 @7 Q" a- v$ k
; |; j1 E3 C" V; L) k0 d5 U$ k b MachineCode(31) = &HC9 'leave
/ `- Z0 m9 X% K
8 Y E9 u- f8 O1 F* o# @ MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
& E. L ~; Z( @ MachineCode(33) = &H10 ' but all produce the stack error6 _& T2 i8 `6 |! p1 w9 X* e
MachineCode(34) = &H0! d" m8 t% A0 g4 M
1 f7 ^5 ]8 l; r! g! {" A 'tell cpuid what we want* [- @' N6 D# \% m {( l
EAX = 00 Y' y2 u/ |$ a0 q. ~
, z* f) O" {$ r0 T' f0 j8 U 'get address of Machine Code
1 e2 A0 E& @* a& v% l M VarAddr = VarPtr(MachineCode(0))
: k M5 {; K4 t2 Y
# d9 x. w" ~! A5 O, K' _% z 'get address of Sub Dummy
& Y7 V% p% c* Q FunctAddr = GetAddress(AddressOf Dummy)
2 W- Q4 d4 C6 q' \3 U% E3 {' O+ l 2 ~3 N1 H5 H5 J& _
'copy the Machine Code to where it can be called/ J$ y; U2 u7 D" [
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code) j* r! B+ [' y8 {# V# F4 [: I
/ v% v# `' p5 y; b4 i3 u, S 'call it
/ O5 ^3 K; N8 g6 Y6 H: u2 u On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
1 g& w6 k, K, b2 N0 { CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))$ u$ W1 ~" Z8 s! |3 _
'Debug.Print Err; Err.Description' G- F1 b% p1 z0 S
'MsgBox Err & Err.Description
# k6 q9 F' v" k4 f# z n2 u$ ` On Error GoTo 0& E( y6 O4 Y5 `+ b
3 o& e1 g/ {* z0 m5 D' n6 A
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
/ D1 \, B$ A0 T
+ x/ A- j2 }" SEnd Function$ G7 U0 [( t: R: C+ K! }
; d3 A3 `& u: s1 ]2 b! wPrivate Function GetAddress(Address As Long) As Long" Z2 T4 p" W) M+ u, k F! V) T& C
3 m$ r0 o: w: }6 N/ ]0 _4 }7 I
GetAddress = Address
- K Z7 p* h" l+ G+ s Q v
% F1 R" T. {! Y TEnd Function1 h5 b8 K3 k4 t* D) t3 O
% G& J' r' e! Y% BPrivate Sub Dummy()6 y+ k, m' [( c e+ ~
! {( P0 m; O* K: G 'the code below just reserves some space to copy the machine code into
, b# s6 z; k$ |6 ~, F+ d7 T+ G 'it is never executed, H# {4 ]' \+ v3 a" _ v+ w( X
) I' C& A2 ~$ m* l) `' I9 J4 M) U x = 02 P& R7 t8 G8 N! `
x = 1& [. X- W$ V' H3 i& k0 Q
x = 2
- @1 O0 I. R: A; |: P x = 3
) h* G# @% H8 ]. W X x = 4% N7 [4 z. h1 E/ L+ I
x = 5
" ^: r1 \* Q5 V( u; H: B x = 6
y$ g4 c D" U1 `3 F x = 7( I4 `# d+ j: y+ J& e/ ~6 M. x+ |
x = 8
! n# T* S7 D" |: w x = 9
7 ?- D/ z' n0 [$ A3 n1 C# a x = 10
/ |# A3 l6 }7 s5 Y$ l* G0 B4 q x = 04 w# s$ C4 y, X, j; q. w3 d l$ \
x = 1
. o4 ^- M% \6 S [# D" h$ _- A x = 2, D% Y$ e; k9 L2 C6 g- U B
x = 3! m7 m2 o/ I f; K+ F
x = 4( ^" T1 F* g9 G
x = 5
& I( b U f# `& I c T x = 6
$ p- u$ o+ K1 Z# S$ Q1 y x = 7
7 ]/ L f# u" Y/ Q$ {) y# j x = 8! c+ r) t* ~" g# }, q0 O+ g& A2 s
x = 9% e( ?+ x% K: X. C( z
x = 10
9 P4 A7 o6 v, L& ?9 l 7 t; c' e1 ~4 {) i# K# S! ?5 s! C
End Sub
* n! n9 x. m! x( {9 J------------------------------end--------------------------------------
! `+ m1 W+ z9 W0 T! i
1 k5 b3 p. R% n0 K% u. |+ g" B & B0 B- g5 _2 ^: U
. _" u: u' }) ~
|
|