该用户从未签到
|
转载:
; Y( B! C# F5 _0 Z来教你如何在vb里嵌入汇编!
- M3 {# x; E' f# Y作者: wl3000wl * e) B: ~0 _6 O, v; `
本贴绝对值得你珍藏.9 {! E/ R) s# P4 g! n* Q: }
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
; i$ q D+ E2 a工程文件分为一个form1.frm 和一个模块module1.bas0 [ ~! a# Y/ k- D1 y
----------------------form1.frm的源文件---------------------
8 a0 _0 t: d( o3 A6 S" ^( b8 e' n, dVERSION 5.00
: Z7 _- Z! r& z+ i- ]* y' f& j; RBegin VB.Form Form1 + ~0 A0 h5 E; j8 F# F. b! E' m
Caption = "Form1"
0 a" h; c8 l5 g7 [. o ClientHeight = 1965
* s$ ]/ h [& H2 y, Q3 v$ \ ClientLeft = 60
$ \6 [4 U4 s# T1 Z ClientTop = 345' p6 R" s+ J# i& G
ClientWidth = 31052 z* p& i6 L) x$ q4 V( c% B( M
LinkTopic = "Form1"
4 m3 d3 p" b( [% G2 l ScaleHeight = 19655 b& ?9 c7 [3 v
ScaleWidth = 3105# i$ W. j' w0 {4 Y' E) W
StartUpPosition = 2 'Bildschirmmitte
7 |; d, l7 n% ^0 V9 M Begin VB.CommandButton Command1 ) @3 r6 {9 }' y+ K8 O" i
Caption = "Get CPU Name"% W+ @2 R+ }; R
Height = 495
1 S4 F" f3 h: [; { p Left = 840# b U3 x& b0 N2 Z/ n9 a
TabIndex = 0( C* \3 [3 z/ T; b0 R: i) m6 k5 `0 J
Top = 3155 ?% |& I! A8 @/ i3 x
Width = 1425
6 ~! Z$ p1 q( P$ q# ~ End
- H }5 G6 M! m* j, m. ~5 u Begin VB.Label Label2
; N; ~. Q( q$ u/ ^" M4 n) { Alignment = 2 'Zentriert
7 i( S. R( [3 C4 y) @; V( G AutoSize = -1 'True8 I# i. q" h) N2 `. x# j; r
BeginProperty Font 0 e! A6 L, Y, r( `' x, [# l
Name = "MS Sans Serif"6 P6 D4 c6 I) l7 J$ K) q
Size = 9.755 z5 W! ^ E. c! v8 U' ^, C* R
Charset = 0
# A6 d- w7 d. _% ]5 n) S Weight = 400, t- X- `( a5 U9 n
Underline = 0 'False# c' L4 F$ ? W4 ~0 [' M
Italic = 0 'False
& B# R i N; l' n Strikethrough = 0 'False2 t4 L4 r1 o0 _/ P; s' l
EndProperty
) U* [; c1 Q& c( ~: Y Height = 240
- p4 ^$ C/ @ r Left = 15155 _; R9 m7 P% {$ K; W7 _% c6 [0 w
TabIndex = 2
j" D. M' f' ]/ L5 ]7 Z% j' b2 o Top = 10655 N# w# V( _2 N% l, l. x
Width = 60
( _9 u7 ^2 U* q W% t; i) Q! ] End% D/ @1 n5 j2 u% A$ Q6 V
Begin VB.Label Label1
3 y& G q( C9 F0 n! A: y$ s& f Alignment = 2 'Zentriert, Y+ R0 \, A) g( h7 l) j, ]2 L5 v
AutoSize = -1 'True4 ]& r& t( r( K- z$ i7 c
BeginProperty Font
, z6 }# L/ O7 j& S: t0 W Name = "Arial"
8 A" U$ f8 q3 Z# R7 u Size = 12
1 {4 T. X* `; d4 f: E Charset = 0+ ~. O2 a9 P8 V4 G% p2 N
Weight = 700 h2 J0 B7 L# A) O
Underline = 0 'False
) x/ ?$ ~! _6 X. Q/ g$ r Italic = 0 'False3 o/ U4 T: q9 s6 U
Strikethrough = 0 'False& i1 T9 e$ G. s) U7 O
EndProperty
' p5 w3 m2 j8 H5 C0 R( Y. O Height = 285
8 P' g \% k& ?, m2 [: I Left = 15159 X1 j0 w/ d6 |& B) q8 v
TabIndex = 1/ ?( I) h- k- n! i
Top = 1350
' h' {& _& s& U$ z/ U) D6 C" W5 Z) C Width = 75
" _1 }3 Y* C6 `8 A6 d' h End' J4 O3 M1 r) }0 F
End/ [; J( e# w- _2 Z4 [
Attribute VB_Name = "Form1"
, s5 ~4 _* _0 T4 Z' J% l. BAttribute VB_GlobalNameSpace = False
0 y% Z, p/ _+ h2 l: ^Attribute VB_Creatable = False
3 I% O/ C# I( i6 I3 V/ mAttribute VB_PredeclaredId = True3 p5 p; D7 g' @& R
Attribute VB_Exposed = False
; ^+ D! q' |" U C3 nOption Explicit6 \4 t e& t2 V1 ^
& i$ r' ^5 f8 T9 l4 HPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)4 j; s% y c. {
& y. v: ]' {( y8 n7 k Label1 = ""
. K9 _# O; G* U) h Label2 = ""
9 I" z4 e# T- f+ ?/ r7 ]8 n5 p' B! a Q3 B, h
End Sub$ M3 \ H! M3 O0 {
8 y" f% L7 i) E$ R$ ?Private Sub Command1_Click()
. l8 L o4 Q( u" `
+ l1 T" o& w9 n* L1 S Label1 = GetCpuName() & " CPU"
9 s$ p/ W- k$ J) t$ j7 y# g Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
8 P6 [) t# }" i2 F" l9 Q9 w
B' `# ~8 I) w8 f; QEnd Sub( e4 }& z: f3 Z- Q [: A: S1 r4 |
------------------------------end---------------------------------- S0 g" Y+ G8 s) a0 R+ F: ]( [+ S
% w* ~4 {5 @( `8 n* H& @
7 s& ?6 i5 M( h1 X! {: {7 h
- G( i' V) C. L! I3 q5 _
# Q8 ^ s0 w, R- e" v* c8 B% h1 W7 z8 x
下面是modu1e.bas的源代码
) Y& L1 u+ C# b4 O$ K% a5 l/ r: Q X- ]1 ]; j5 ~- N% ]
----------------------module1.bas的源文件--------------------------+ l' v, n2 T! I1 r) D9 n+ N6 Z
Option Explicit- M* i5 T8 t$ v7 U8 {
'0 K* q; M/ m2 W C6 q
'This shows how to incorporate machine code into VB+ q- f0 F5 X% C* n2 z& N
'''''''''''''''''''''''''''''''''''''''''''''''''''0 _2 V6 j4 |9 K! L m/ h m* Q" D
'The example fills the array with a few machine instructions and then copies
9 v3 f. ` V I8 ?- J'them to a procedure address. The modified procedure is then called thru
, N; ]! k/ p5 x. j" d# k' N'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.: e' O' ~- K0 @( Y9 M# _
'& m; @& d; x+ e7 L3 L! g
'########################################################################### N1 e/ M0 Q/ S' U
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody+ a2 M( X9 f g0 p+ T. @
'can fix that please let me know... UMGEDV@AOL.COM
6 r7 z0 z) ?; k2 ]# l. U2 Z'The Error is not present in the native compiled version; so I think it got
. i Y7 B5 [1 D1 H* `'something to do with the P-Code Calling Convention (strange though)...0 b( S) k: C: l* q
'##########################################################################& q3 J/ `$ g: a: ^
'0 J6 q$ F, W3 D' @
'Sub Dummy serves to reserve some space to copy the machine instructions into.3 z! I" u& x" ~1 o2 G
'. t c' c3 R( s& g$ i$ w; D5 n
'# |1 H6 U( H2 m9 S
'Tested on Intel and AMD CPU's (uncompiled and compiled)4 I$ j& O9 H# x4 x# t5 J
') g& s7 B7 J7 m _& T
'
1 ^5 W" w, G3 f+ s. x' X" DPrivate 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. \' `) y U4 J& K- H# Z
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
5 t2 `0 w9 j, e( P. e/ iPrivate x As Long
) _) r3 Q" Y! g w4 t- q6 y
7 x2 u( w- A0 n1 V0 BPublic Function GetCpuName() As String
; M/ B2 I6 { v/ m" V* V! [0 K
& B* q0 q0 O7 W4 k* d4 r Dim MachineCode(0 To 35) As Byte+ r) m1 w0 I) m/ O- i1 d
Dim VarAddr As Long
0 R' s* ~6 e: t2 F Dim FunctAddr As Long
F6 q- c- [# r4 t* n) b* D9 w8 _- G Dim EAX As Long) \8 H" a7 L; }
Dim CPUName(1 To 12) As Byte$ w4 G/ U$ y" X, Q
* U4 m# N9 B' k3 o$ z" ~* n
'set up machine code. ^0 B0 R- D: p- \, {" L) m% d
( x) O+ m4 Y0 n% G1 x MachineCode(0) = &H55 'push ebp
( q1 M( a6 n0 m% m% ^) G 4 f% j# ]0 R5 E1 S9 @% H
MachineCode(1) = &H8B 'move ebp,esp9 u# _4 h8 p; {" u9 h; [/ ^8 G
MachineCode(2) = &HEC4 m u1 V5 t2 [2 L
5 T- G3 n% o: B/ a MachineCode(3) = &H57 'push edi3 S; j+ M; {% G
" [5 ~& Y% r1 c5 W5 O+ h
MachineCode(4) = &H52 'push edx( t) P' |- E/ U! o# ^0 B, ?3 M5 W
% V& k, v3 R2 ]! A3 a MachineCode(5) = &H51 'push ecx# H" F7 f! Q) ? V$ u
+ e0 Q! C, r; @
MachineCode(6) = &H53 'push ebx
3 E4 S/ G T H4 O! i ~5 p% B' d) ^6 l; P6 _
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]1 z( V" T! Z# ]* Q; T
MachineCode(8) = &H45
& y+ ^( {3 h1 X/ [ MachineCode(9) = &H8
$ `$ y8 t/ C: x' u' \! D
4 ~( Z6 e; S8 O( p8 I MachineCode(10) = &HF 'cpuid
: C, a& \+ R, {- u& n, z8 w MachineCode(11) = &HA2- M6 }) D- F: {/ z8 j7 }2 p
4 `- d0 o d" n) J1 F3 Y1 y# k3 ` MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]8 W7 h9 K: |' {' [0 U ^4 m0 H
MachineCode(13) = &H7D
' m: ]. T J( B& q% e* a# ^ MachineCode(14) = &HC
& s! A v0 s6 p8 J- X 3 W7 ^) u& N3 k6 p# ~
MachineCode(15) = &H89 'move dword ptr [edi],ebx
h* ?4 z `# E# f! q! s/ U+ s' W MachineCode(16) = &H1F
% ~8 k) w R) ?) k
* }, E0 j4 D# f5 b1 Y1 ^5 z MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
1 r5 o. G2 x1 f) K! h- F MachineCode(18) = &H7D2 I, x8 g% k7 U' I& ~: a, `
MachineCode(19) = &H105 s' g$ U, s& P+ K( c+ N
; X* Y+ X5 g2 j& f, Q
MachineCode(20) = &H89 'move dword ptr [edi],ecx
% }) k5 H! d: z. {" } MachineCode(21) = &HF
! n3 ]% j2 S7 a( [% j' Y. a
/ Q3 n) l2 a, {, Z- T4 R% v! j$ ? MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
1 Z S) v! q. Q' a8 Z- r8 V MachineCode(23) = &H7D: G* X. Q( s! _/ B
MachineCode(24) = &H14
, f8 k- P9 }9 {0 Q: y 8 w' Y6 U/ n. a6 F! w) L1 P* J/ k3 g
MachineCode(25) = &H89 'move dword ptr [edi],edx2 M; ?" v4 k+ E0 g/ Z+ z+ |
MachineCode(26) = &H17
- f8 f4 G9 D* c s ' R; A1 Y9 c( m" |) D* f2 C
MachineCode(27) = &H58 'pop ebx
# v! P, s% R3 ?( \6 P( W! `! D- y: d1 ^
MachineCode(28) = &H59 'pop ecx
. H j7 o% M: y. x1 s0 a5 J- D: ~ {' s- |* {" J
MachineCode(29) = &H5A 'pop edx. a w7 C, z- p1 f: _/ ~" P% E
( R9 \3 K, R3 m: N
MachineCode(30) = &H55 'pop edi
. t& _+ S2 `2 L" M+ r3 F/ E - l1 Q! E, [! ^$ ?7 F! d3 m
MachineCode(31) = &HC9 'leave
% q( \, X1 N: l' E3 a6 t2 b
+ h! M% p! G/ z$ p* q4 [/ Y9 Q* F b MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
# w b9 J6 ]. N- {! t MachineCode(33) = &H10 ' but all produce the stack error' t5 c5 N3 E6 z$ m$ Z$ d/ X
MachineCode(34) = &H0
; o0 v" g# F+ c% g( C/ O
: B+ N$ j, K( m+ \4 H% @5 k 'tell cpuid what we want7 n: r% M2 U: i' a6 Z% m0 D0 X8 Q* L
EAX = 0
) L) I( m8 M3 c d! \) o1 _) c
a. Z" @# J/ W2 S0 } 'get address of Machine Code
* ~, v7 k" Y1 I) { VarAddr = VarPtr(MachineCode(0))
% U# X- a) K$ @4 a! s4 Y ; T4 n) d! Y+ t5 a
'get address of Sub Dummy ~% ]7 H/ u% g; B, l: P
FunctAddr = GetAddress(AddressOf Dummy)! B. ^" O% E p* [5 D' p/ D1 c
0 q5 D$ J* B6 g7 Z
'copy the Machine Code to where it can be called! ^0 x! e$ j) ~7 k
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
8 D9 h: h# h" q
$ {* q' }6 |" I0 n 'call it4 E. x! J; A9 W8 @
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
8 w2 V( }1 a" j3 Z CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))* Q% F* F+ o/ ~2 n: \- c$ v
'Debug.Print Err; Err.Description! I0 K# L0 r2 ]8 T9 E" w
'MsgBox Err & Err.Description9 ~2 [8 ^! Y4 d U! f
On Error GoTo 07 T! t5 G4 `7 b( S. r
: K; U3 f# h' a1 A GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName f( p- I4 F8 @# u* j$ s# e5 d: l& P
. u: J. j6 q( _; V! F0 g% W
End Function x$ @/ s+ e6 s7 @3 |
9 A$ R+ ~ x/ f m, S
Private Function GetAddress(Address As Long) As Long
: _, W: ?1 O6 V" ]8 D1 `& _; V/ \( j: ]
; B- D% q1 L5 V5 C c6 ^ GetAddress = Address
* L* H! h% S; ?' _) p' f
7 p0 c' z3 S. b/ ?End Function
+ H% e) F3 H/ F; o8 c; F/ q3 E* P2 d+ |3 N L# R3 F
Private Sub Dummy()" v% h% t* J6 i9 R" C
8 ^, I5 ^( A9 U7 \2 P
'the code below just reserves some space to copy the machine code into
4 V6 D3 |+ M- o. N9 X# q 'it is never executed
1 d* k ?9 f# c4 [: ~+ E3 F
* ?2 u+ U6 s. H! a+ ?3 G x = 0' [ c- e, ~) D/ f3 Y# ^
x = 1
0 X( z4 a1 C6 W9 u x = 2
6 x8 S4 {+ n6 g& @3 Y x = 3
9 X" \5 w8 g2 [0 e+ `; S, p* R x = 4& |; v. P, @1 p8 |' t) A
x = 5
2 q* X) f, _; h r% l3 \3 [% ] x = 6
4 y! G1 h# {* ~. L2 y1 Y7 V x = 70 H0 ^# b5 t: W* S, C, _
x = 8' M2 A; U! |0 t$ J1 t
x = 9
- |1 i$ M/ D- J" a' z/ b# U x = 10/ |1 J3 u" W& B) U. n5 ?! t. X
x = 0
0 m, E9 |) g( t) k* N( `% Q/ b x = 1
1 [6 o3 l8 u8 n% I x = 2
1 }" o& V% c# n( F* f: @) e x = 3# q# A4 F4 k+ m- v( ~7 [$ m
x = 4! q+ W- P" s; { U
x = 5
4 h, ]$ L3 K7 T& a3 n; n' } x = 63 t& M; m/ {1 G6 E9 V
x = 7
- n/ h. l. e8 ?0 T" o) W3 _; n+ w x = 8
6 U% [) @' A8 w( |) a4 L, N: \ x = 9
9 b* N/ M, z- E" _' b8 L x = 10
7 S" u3 V/ e# f p$ l- l
7 K( \9 k3 U6 Y# F9 d# r. IEnd Sub
, C) n+ M2 s7 l: z------------------------------end--------------------------------------' P+ f0 O+ Z, [0 c- ?
5 y) R; u3 Y9 e6 c 9 s- p& ^$ V% C4 L6 h- {
: n/ l, r' q- W
|
|