|
转载:: r9 O% G4 q* I. Z9 J* P5 _
来教你如何在vb里嵌入汇编!
: | P$ V5 f8 U X" h4 Y; n. Q+ e作者: wl3000wl
- w( i6 _% {+ s3 z本贴绝对值得你珍藏., Y. t( G; G5 F& l* M
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.! D. V9 }2 y& J+ R3 Z
工程文件分为一个form1.frm 和一个模块module1.bas# c. r. R% B+ P* u1 K
----------------------form1.frm的源文件---------------------
8 w9 V2 }" n; z' jVERSION 5.00
/ X9 B |- C4 B, l- H: w0 f; }Begin VB.Form Form1 + Y. x* S4 O2 e; Y- [! G4 q
Caption = "Form1"1 B+ E6 } Y6 v% |, z
ClientHeight = 1965 W' V: c1 Y8 O: K8 u! H
ClientLeft = 608 R. @4 d" k$ B3 V3 h4 Q. s
ClientTop = 345
" ]5 r: y; y3 C- N/ P ClientWidth = 3105
" u+ K. q( V3 ~3 c LinkTopic = "Form1"7 Z& q! D3 |' ?8 p' I; I
ScaleHeight = 19651 C/ S) `! t" c
ScaleWidth = 31052 V2 }, H3 c; J& U- S3 L0 h" `
StartUpPosition = 2 'Bildschirmmitte& j3 a1 l8 M: V+ V
Begin VB.CommandButton Command1
& [( g1 s% T& U, I$ X" E0 x4 r- Q Q Caption = "Get CPU Name"
7 |" e# Y# \# o) n8 D* `# c9 U Height = 4956 b6 y C0 a' h( [. ?4 v0 Q. j
Left = 840& M! {. m, K: }* X
TabIndex = 0# r5 H. a l* c x
Top = 315& M" N' i0 i( q% H/ ~
Width = 1425, V6 R4 h# P- X0 \( O+ T
End
# k! w5 K& g! W7 y% Q# w Begin VB.Label Label2 9 P1 u& Z e4 I/ m- x- U
Alignment = 2 'Zentriert
! O5 J. R& l3 ^1 r& o/ g Y# v AutoSize = -1 'True$ ~9 t/ G& a/ S
BeginProperty Font ( h3 m/ m+ \6 e i5 P( ~
Name = "MS Sans Serif"
0 a* [$ x" P' A5 U0 F" E; Z/ K Size = 9.75 C- V% {; p+ ~* y; u; W
Charset = 0
A# @- ^! D& G* A: l1 M9 Y Weight = 4000 E6 o# o! h. w8 `8 M2 V1 B
Underline = 0 'False
~" n: W" r& s. l# _# W6 j Italic = 0 'False# i, M z8 K) y4 W6 P
Strikethrough = 0 'False
; m1 T5 k9 \- K EndProperty/ Z3 M& [# |. H% O" M8 R
Height = 240
- J, z5 \& r4 P& d7 G! V Left = 1515 [& a2 W- ?2 D+ E# e* ~
TabIndex = 2
5 f5 Z' X8 }1 V Top = 1065
3 L5 {' H, {. e; u4 O* `" g Width = 60
) Y0 q" u* Z; Q End
( D8 v3 D, W7 R3 c Begin VB.Label Label1
2 d( b- b* r+ p Alignment = 2 'Zentriert
: Y* s$ P7 E/ Z( D5 w6 F% \/ v# n AutoSize = -1 'True
; v( R0 h! ~, d3 O a$ H BeginProperty Font
$ C0 n2 Y9 i K O Name = "Arial"
- j# a" z" e9 [& y$ f( O Size = 12) _, k5 H0 t5 G% f. {2 V, [
Charset = 04 W% r) x0 X& c9 t6 o5 i! Q: ]: }& D
Weight = 700/ ~7 n$ q1 e+ T b0 L, p
Underline = 0 'False
& {, D: Y" ]2 }/ n$ \ Italic = 0 'False( F# u' L; {$ L
Strikethrough = 0 'False" {6 g q( N' Y/ C: D- @7 r+ o9 i
EndProperty
. |2 g7 R6 L' {2 q Height = 2854 s9 m9 J8 e) I- w
Left = 15159 I& u3 f- ] x
TabIndex = 1- ~/ r' Y5 S) @) f2 @8 y# z5 x, v
Top = 1350
5 [% E1 S: k% w" L0 O: q0 u1 E9 r+ c' r Width = 75
2 z- G$ S/ d( Q0 z2 J& m r$ c4 F End; U* X5 Y0 D; ^6 p: N; M
End
5 |4 o5 U2 g$ b. EAttribute VB_Name = "Form1"
% R8 r; W3 m+ ?7 R3 Z3 D" zAttribute VB_GlobalNameSpace = False$ z- ]4 {- f: z3 G
Attribute VB_Creatable = False! g, F, E2 b; @" N0 N2 @
Attribute VB_PredeclaredId = True
# N2 x( I7 [+ F3 WAttribute VB_Exposed = False
8 O/ A! S) y# j e" _$ nOption Explicit6 P! Z- J0 m3 L) L8 c8 _
( p( Y4 _9 Z! C; x) {/ ]
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single); F4 A% y! e- F5 h
# }4 T- Y) { m B T* A' h; E
Label1 = ""
3 B* J) X1 s% X Label2 = ""' a. M5 R& J0 v
; R' K8 B9 D" K `1 C% g7 _3 W1 k8 [
End Sub
7 t! D" b0 p/ S7 e* I2 C; a- |1 D. C, r' Y
Private Sub Command1_Click()
6 O" H1 b/ G: |# S- z% X. k
, ?& g( n; d3 w; h" K9 e Label1 = GetCpuName() & " CPU"# t& g2 L k( T2 {
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
) t0 b, z* p8 Q$ k' z: I( [" D" f% P, V* E( A# B! d
End Sub& y( w0 x/ h: h
------------------------------end---------------------------------
# v* G3 S, R6 j! ^, k# Z3 O5 ^+ T3 e0 s9 q9 j: Z" y* I; \! A; \
- Z& m* J x3 o! m1 _. h2 b( N+ d, G( l
: m- {+ e/ b3 K7 T4 F4 Y
- {; O. K( q" v% H: D% y0 U下面是modu1e.bas的源代码
! k* \1 K/ \, O; F7 U9 U6 H! f& L, W8 ^. t: s! A( ]
----------------------module1.bas的源文件--------------------------
! o; F9 P- Q9 H4 Z, x A, b" V# wOption Explicit
: g& s- _7 f: H4 X7 b6 u'! O% \8 G p7 C7 Q2 n
'This shows how to incorporate machine code into VB
; z1 U* G3 R6 F6 a0 b! Y0 y1 k'''''''''''''''''''''''''''''''''''''''''''''''''''
3 @; \" [* h$ a: @'The example fills the array with a few machine instructions and then copies
# K# N7 b7 T: f, T% i- C'them to a procedure address. The modified procedure is then called thru( `$ a A* H% w
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.: I* c" `. c$ x, h
'6 {) v' Z% u9 X" t0 g
'##########################################################################. A0 o7 L: b; _6 V$ A! G! `
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody. S% t/ `; F+ S
'can fix that please let me know... UMGEDV@AOL.COM! e. ^. X" L, m% M1 J" x7 _4 S& u
'The Error is not present in the native compiled version; so I think it got
[ j4 X& m/ ]/ e'something to do with the P-Code Calling Convention (strange though)...0 {5 M4 F& x! L
'##########################################################################
8 W9 P( u* @3 `% f* T. a'
3 k8 l! j+ M9 o0 u% E'Sub Dummy serves to reserve some space to copy the machine instructions into.
* s+ R* M' J, O: \" v1 ['
5 I- L2 I# x/ C8 b' R'* U. {- X7 Y9 A5 B& R2 |
'Tested on Intel and AMD CPU's (uncompiled and compiled)2 ^7 t! P. {4 W' I( r$ g5 r) Q
'
. U* G) }7 N9 p" b8 E ]'( X; {# n+ m# f3 C) `
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
+ f% Q8 c# \; q) }# g# b: F. qPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)7 {$ K+ _' g( e& Z+ G
Private x As Long3 D, a8 o' @0 L1 ^
% [" g0 d- c1 p% A4 S) U1 f$ ?Public Function GetCpuName() As String
% X- u# _! a6 E 1 x. {! ?5 U5 ~2 ?, h, o
Dim MachineCode(0 To 35) As Byte
+ E( E/ @0 h' V; A9 h- ~8 c8 P Dim VarAddr As Long
3 e3 ?9 v/ J# f8 b9 i' Q Dim FunctAddr As Long
* r, X7 t' C* e0 g4 R! i Dim EAX As Long6 u% T$ P4 n& C/ P6 t: B% E
Dim CPUName(1 To 12) As Byte
# ]$ B0 z4 X S5 F& H
[0 J" n" @2 g 'set up machine code
. g4 v# }6 } T
: [ D- b, y8 y/ f, h9 ?. _0 d4 W7 ] MachineCode(0) = &H55 'push ebp6 P! t1 S o3 b5 R/ i
7 \9 I( j& k5 k+ `0 e, u0 s& D/ H
MachineCode(1) = &H8B 'move ebp,esp
. W9 E8 V# ]" `9 d' A) { MachineCode(2) = &HEC
: S% q8 ~2 l, z& v6 o0 ? _- S
$ m5 m0 h* k) X- J( U2 p. c- I r MachineCode(3) = &H57 'push edi
" W* p6 H& M/ Q& y1 F% d8 s: b5 Y' { # X& [$ J, Z6 p5 e7 r
MachineCode(4) = &H52 'push edx+ \" w3 B; n, _9 L6 y' J* x- a
" s0 g9 X9 _* t MachineCode(5) = &H51 'push ecx
5 [, E( f+ j. a5 ^& D5 y' B ( I. h9 R. k7 @/ Y
MachineCode(6) = &H53 'push ebx; ]1 `) y5 ]$ U5 [4 b8 R) W9 y. T
, a5 T: H. U, ]1 v MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]9 C/ L1 O* h( i
MachineCode(8) = &H453 T: L6 c! A! c4 I# C8 ^
MachineCode(9) = &H8
: h4 j7 C7 p: ^9 a. q& C) |; n
# ~/ w( c3 P' k; A8 J MachineCode(10) = &HF 'cpuid! [. Y2 i6 P: @$ g+ D4 f% ]
MachineCode(11) = &HA2* V+ }" P9 I% n6 X* y9 i4 R- y8 n
+ k6 r0 |% P4 @0 c# | MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]
0 E; W1 R$ b4 n# Y MachineCode(13) = &H7D
* L) d+ Z$ c; L/ a9 ` MachineCode(14) = &HC
6 K0 E. s7 c6 U' f# R) h9 G$ B
- q: n0 Z0 I) Y MachineCode(15) = &H89 'move dword ptr [edi],ebx
% {: j# U4 d+ z) O- ^7 r" U% S6 t MachineCode(16) = &H1F c; y/ X @2 v2 Y9 s }
+ V, e' c8 {* Q) L3 i
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
! N. }# }- P2 f( C( T MachineCode(18) = &H7D; @( O3 N$ X; r
MachineCode(19) = &H10
5 _, o- h9 u6 H' o 6 U( i! L8 Z `; c: y
MachineCode(20) = &H89 'move dword ptr [edi],ecx
2 x0 k3 K2 k8 u' e+ A0 w MachineCode(21) = &HF
; y" B6 d& n5 k& b4 F0 | 9 x- S/ n' p( U% p# E
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
; n) D/ [3 c U6 M5 c8 s- O* v MachineCode(23) = &H7D3 N2 m0 D7 g4 v+ U# I
MachineCode(24) = &H14
4 J7 K2 F2 M6 Y3 G
) E6 J* R! Q6 d, B, h) d9 N MachineCode(25) = &H89 'move dword ptr [edi],edx
4 P! y, Y6 C4 y$ r; s+ I' Q MachineCode(26) = &H179 Y" W( V. X, V0 |" U5 h" x
) v' l/ d# K! |& l+ p7 M) i* ^, y6 h
MachineCode(27) = &H58 'pop ebx
# w* A' i2 v& }- v7 J& k* g. h
: r+ \ `8 ]- i' W8 h MachineCode(28) = &H59 'pop ecx c% d7 {$ f+ A6 d, B4 Q, x; c
% Y' d$ J2 y: z9 F4 k MachineCode(29) = &H5A 'pop edx
3 X; l2 _- i8 ]0 u3 I
! H7 J) ~$ G* q. i. ^8 Q3 ~& M$ I4 N MachineCode(30) = &H55 'pop edi
8 S/ e: j( T7 Q% h* s
$ s k0 T+ L% ` MachineCode(31) = &HC9 'leave
; @+ k; l# P+ Q: P$ Z2 m* ]+ S; I
2 R M. e; t6 D" O0 u MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
. f+ S2 m% M* e: @( t; X MachineCode(33) = &H10 ' but all produce the stack error6 [# p6 ]; e/ p3 A8 a7 o1 }
MachineCode(34) = &H05 D3 Z8 S: m8 p
+ P" {+ A1 _* C0 u 'tell cpuid what we want& a& ]" v6 y3 Y
EAX = 0+ x5 s1 a+ ^0 f! r# m, `4 p9 u
: o8 j( ?5 B5 q; S0 X- e! i 'get address of Machine Code
8 y) c# R) z/ K( U VarAddr = VarPtr(MachineCode(0))/ i* i) \# j8 z. g
: S- v8 V. A$ V* _( N
'get address of Sub Dummy
! y( `& o$ A3 U! \7 i FunctAddr = GetAddress(AddressOf Dummy)% V* |, P* g( H1 J: `0 p+ ~
! M7 E6 o& k- K/ G 'copy the Machine Code to where it can be called
/ f6 _9 ~* X: d: ]! e- z' Z CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
( |" v) x8 M0 ]9 A# X J
: e- O2 B- n% r3 F 'call it
/ p, X3 Q3 l6 c8 P6 T On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
; U$ a. H9 s- b" a3 ? CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))* B* y, g) b) O8 G* }% A8 H: p2 s4 v
'Debug.Print Err; Err.Description' a; f8 L- Q: T9 r& V/ j8 T k j
'MsgBox Err & Err.Description* V8 V, G, ?" j- ?3 v
On Error GoTo 0
" t! U0 u3 p0 K! P; U 0 X2 ?7 h2 }' J) D
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName9 g# c6 H) Y( i/ C9 L, C
# G3 O" h+ y* ` R$ `* y' F
End Function
- @) |3 n0 J; l3 m( a7 r
0 Y* {3 ^& r7 Z5 i1 [Private Function GetAddress(Address As Long) As Long5 X+ H7 r# d8 I9 I. e+ d
& Y8 P; k5 }! J" P GetAddress = Address
. H8 T- G. y h) I- j {$ g
: W+ N6 q0 a; f/ }End Function% c- T" W) t- ~) N* y5 o1 Q. B4 s6 T
7 M1 E- }* y) Q! v& t$ h
Private Sub Dummy()6 k/ X, q5 }0 h6 c' G2 @" t) ^9 k
$ y; s Z# G6 _0 }; l
'the code below just reserves some space to copy the machine code into1 W8 ?, m% m5 W. N' X- [3 a5 a
'it is never executed& V! a; `' i. q, F
, s* H& Y5 P/ K- x. H: J2 V' c x = 0# U6 E$ H) L3 L, e, l
x = 1/ ?9 X" |' j6 T
x = 2( C( o' [ e F* f- ~! D9 B
x = 3
; S$ x9 N8 r& p; _/ g x = 4
5 ]. z8 ~5 v& `% z0 F1 X x = 5
G0 m5 q: ]8 Y- I x = 6* [+ g/ P* g+ ^, I; X5 W
x = 7 K& j" I' F' m$ y7 u& q& Z2 s9 V
x = 8
7 C3 c2 F. b; F. Y, U1 q x = 9
* Y; I' j/ T' d2 ]$ ?* o+ g x = 10: ~" K( Z6 R$ Q. s5 R2 Y
x = 05 H3 y" n" a( Q
x = 1. M3 _: f+ z. A2 Z* ~! S$ L
x = 2
7 @0 G. ?& b3 k. P. l x = 3
/ b8 P0 t) m2 F# r* A x = 4
+ V3 u0 |+ J2 C5 E, G x = 5
3 }6 l3 g8 X* R& P x = 65 ?% p. h9 u, @4 p; I3 B2 L
x = 7
* t7 Y6 [8 |6 d% m5 k( l, Q x = 8, K; S s: X! t
x = 9
5 C2 e Y0 P1 E" V: G x = 10! O2 m+ C0 l/ T! B
. k- l9 O& ^2 o9 ^( h4 y! VEnd Sub5 e# a( {5 M7 L& L9 d8 E. N* u4 S
------------------------------end--------------------------------------0 L% c! ?. E/ o/ |. J- |, C$ B
7 a) g2 o+ j& R7 j! c
$ D' ]. A( F! m& |, L% V! z4 o3 f9 A/ E
( j: Q3 {- T0 M |
|