|
转载:
. Y/ Y2 { V- Q |) L" o' Z来教你如何在vb里嵌入汇编!
: L* |) X* }# \3 @* f# P: n0 w. @作者: wl3000wl % x6 n( V% C# @# h( o7 d
本贴绝对值得你珍藏.
! q5 x+ P* q4 P! ^5 ?% Q2 ], H, b下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.3 J' P1 \4 D2 \
工程文件分为一个form1.frm 和一个模块module1.bas
& u: q2 R+ c7 X/ \; w$ A----------------------form1.frm的源文件---------------------
' g. t9 ]# c" Q1 DVERSION 5.00# T8 z3 z5 y2 m: M' d! ^' H5 _
Begin VB.Form Form1 - |: V0 p# v. T! s
Caption = "Form1") j2 t; K$ O5 z# n# _" g. n
ClientHeight = 1965* u, Z4 [/ f! A# z ^0 V) A9 u1 `) E
ClientLeft = 60
+ z$ e$ k2 C2 e2 K Z ClientTop = 345
: W! R" J3 R8 R- r1 C$ M+ B; C0 Q" l ClientWidth = 3105
% X& D' p" H: A4 E LinkTopic = "Form1"$ n! I0 u& f, P1 v5 ]1 m# E
ScaleHeight = 1965
0 x% H. J" S% e4 z9 a* M ScaleWidth = 3105
; B0 t1 g( E2 ^1 t/ U StartUpPosition = 2 'Bildschirmmitte4 ^. s3 ]' n; c E% f2 N+ x/ C
Begin VB.CommandButton Command1
. r( D: h7 b7 w7 u6 i Caption = "Get CPU Name"" j7 g$ V. r6 |& n
Height = 495; F: ^/ @, s1 b' u: |
Left = 840
- P8 N( A$ w: w0 U' r t( w" v TabIndex = 0
! r% `) Z7 E" w) ^5 ~1 d) a Top = 3159 k* i& M) C9 G/ c# f: s* g! L
Width = 14255 n) S. _0 j7 ]* d
End* r3 b' u0 z7 T3 J9 c" H
Begin VB.Label Label2 ; O5 {$ c8 t6 K9 j6 s' Y, _" x/ k3 B
Alignment = 2 'Zentriert
0 W' Y0 x/ i& R, j6 q0 b/ a' j AutoSize = -1 'True0 S; d# ]- E, d9 X y
BeginProperty Font
$ y& n) b w! i- ` Name = "MS Sans Serif"5 C* ]0 m0 n: V: b$ Y3 L& }
Size = 9.75, ?) ~# A' X; k6 A+ v3 F& E1 @* ]
Charset = 0
, y4 A% a! Z. @6 z8 Z& v Weight = 400
6 i% A. e0 N5 F& m3 q Underline = 0 'False- n3 y( C3 J% R
Italic = 0 'False f6 I5 T+ e7 Z* o% j
Strikethrough = 0 'False
) m i q7 ]7 _ EndProperty
8 e. H: y. l) X' Z0 w Height = 240, ^# o3 x# L. L
Left = 1515: d# e0 V! ~3 Z1 p: q* c/ x& L; C/ E
TabIndex = 2
) i( [, A8 V/ m* X4 I' T Top = 1065. O+ i: |7 K- T' D
Width = 60
$ p e; a6 ]0 }+ R3 [. x End( `, D7 a: Y2 F+ d, n; t( c1 [
Begin VB.Label Label1
# j5 v* i9 y W7 u( b. R Alignment = 2 'Zentriert
+ G+ f4 T+ R! k AutoSize = -1 'True$ c3 f- v& u. M* s0 k# G0 \
BeginProperty Font
$ `2 l% T$ r" V# g& o9 e' P, l. ^ Name = "Arial"
2 m( E, K5 T( {: t Size = 12
0 {' O* `5 c& R# c2 {& l8 G+ q1 k" | Charset = 0+ J9 o, i# U# c; `8 j+ y# R, J7 r
Weight = 700
$ I& P5 J2 {3 @; b, ?3 i$ k Underline = 0 'False/ n' g' }# G1 d* A( c2 Q2 F K
Italic = 0 'False
0 P r" l" J4 |7 E" E Strikethrough = 0 'False( K. x8 ]4 y/ P* A% ^
EndProperty
4 ?- h( w9 L' \0 o: a9 Z Height = 285
7 |9 N4 N6 l( h; l Left = 1515
' H- o* s( \2 e( M# X) [1 p5 y TabIndex = 1
" f- {- j0 V0 t% I% m7 G Top = 1350
8 E1 }3 c2 z( Q0 H; F Width = 75
2 b% a9 Z. `8 ` End% ^# Q, _' o) I+ t/ ? G
End- i+ W9 V3 N8 }; ]' H2 j# [
Attribute VB_Name = "Form1"
+ g+ ^3 D: P% \& _# n2 a3 V& | |Attribute VB_GlobalNameSpace = False$ H/ @9 }- a% }4 T
Attribute VB_Creatable = False) P+ A) N4 w: Y
Attribute VB_PredeclaredId = True
3 j" K; r" T' \( ^) K% ~, mAttribute VB_Exposed = False
6 R' q) A8 c% Q/ X4 x( r+ b; T. ZOption Explicit
" U% z3 B$ T) Z" W6 B5 x0 B( g9 P7 \
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)$ E. v* x% L9 b! r' [
: g4 \! ^* z1 G) O% H* ^+ z; | Label1 = ""0 B2 V& h$ `0 S/ ]/ A* G8 Y4 [
Label2 = "": h# z9 G' s2 d: M2 Y
% }; Z4 r4 Z: |! \- S, ^. DEnd Sub
p" i5 u1 h5 m/ o; ]- [
( y) l; Y2 L- T( APrivate Sub Command1_Click()
+ {2 r4 P6 m' q- `- W ( q3 D4 z5 K6 g1 ~- h4 W
Label1 = GetCpuName() & " CPU"% O- B9 X H5 [1 q }0 {( U
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")! {8 D" @: c( s( N4 u' v) A
: }! a6 c" D5 n( I: X
End Sub
& y9 l$ p( |! C9 m, G------------------------------end---------------------------------* a9 T4 R- ]# S9 X t* F$ b
7 a* V9 z, V- D. F3 w4 {/ x
" S! C, r$ O1 Q8 E! O, Z D
* ^7 x; ^+ q' N: |' L. @4 h6 ~8 P8 h: S( u) e
9 ], S2 J6 Z7 C1 S
下面是modu1e.bas的源代码
/ E% v2 f9 S2 [
e" m8 O$ A* f$ Q6 d7 c# ^2 R' s----------------------module1.bas的源文件--------------------------
2 I6 A6 P- I! k4 S) K" A$ c' Z& K+ J! |Option Explicit, p) j: @* c @0 @6 P4 V5 b
'
/ r, M/ R$ p5 m4 O1 M$ Y'This shows how to incorporate machine code into VB
" J1 T( O0 e- C+ {" x. s& S'''''''''''''''''''''''''''''''''''''''''''''''''''
- O* U$ O8 h* C'The example fills the array with a few machine instructions and then copies
0 A; g: }! K1 m% b2 s4 H- f'them to a procedure address. The modified procedure is then called thru( Y3 a7 N8 W8 k" s$ n, j- L
'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.0 b+ o1 y8 o3 T5 I) v/ G4 Y, U) H
'
8 V9 P4 f7 T3 ] A/ e% {9 ]+ B5 D6 |'##########################################################################
* j- s" i& `5 J9 r% b'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody5 e: b# T- n4 t
'can fix that please let me know... UMGEDV@AOL.COM
7 p' ?/ H4 j$ ['The Error is not present in the native compiled version; so I think it got
; A0 k4 O) A! d% `2 p* e'something to do with the P-Code Calling Convention (strange though)..." I) R& [# O( m0 {. _
'##########################################################################
( R! N/ I! P8 N3 ?) }' k( G' n! G1 @2 R/ _! W5 a
'Sub Dummy serves to reserve some space to copy the machine instructions into., C; r6 y# ]; m' g$ \( t; u
'
4 e" H8 H9 x/ e'
* F0 p: r0 ?5 o( V0 _; H'Tested on Intel and AMD CPU's (uncompiled and compiled)
3 m, n7 U; Q9 Q, M* m1 u8 d; a, u'
1 V) Q! ~- C- _; e3 u'
, Q; t1 k" h% f$ vPrivate 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! A) q+ b8 q/ }+ g
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
1 v1 o; l/ s! u& x: }' ^; z) x1 VPrivate x As Long
* C& b4 H( }; \+ ]/ {. f+ h9 l% D% o" K: _6 }
Public Function GetCpuName() As String/ }4 B' i8 m/ |, C0 t& _& F Y3 J( X
4 ~2 V" J! a0 i1 L# [& V/ ]
Dim MachineCode(0 To 35) As Byte n- C+ ^7 f& H) w$ b2 _
Dim VarAddr As Long
2 f- b4 q& e$ B8 f+ Y Dim FunctAddr As Long) F7 t2 O T) R F
Dim EAX As Long
- @7 d& Q' l5 @ Dim CPUName(1 To 12) As Byte
9 m0 O, F8 `: ]
( \* l$ x; L' W5 W' C 'set up machine code
' {2 f- b G/ _, [9 t
- ^' b& Q0 p. c: ~2 }6 ^ MachineCode(0) = &H55 'push ebp5 r. e' T2 y# F9 X. Y- `3 V. A
% q0 |9 u" M4 I( e) { MachineCode(1) = &H8B 'move ebp,esp3 n& G! g$ s& ^- C) E+ V; s$ B
MachineCode(2) = &HEC
' ]# t+ D6 v0 O7 t
5 [7 M9 E! E% {! z6 Q9 Q MachineCode(3) = &H57 'push edi
: v& f# r0 l( e7 [+ d: G, v 8 L& [0 A& R) _/ D6 j1 C+ E
MachineCode(4) = &H52 'push edx
$ q! {, I% S$ V
0 Y+ a) O( H/ r* \% @5 ?5 A MachineCode(5) = &H51 'push ecx, t8 a3 Z f9 F# R- ^
% ^" [1 r. V+ l9 B+ m MachineCode(6) = &H53 'push ebx7 m( O1 A* ^' T2 ?* d3 M* `' X
1 F; A, S, h- N MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]9 K! q2 b4 C8 A1 R; z" v
MachineCode(8) = &H45! ^. U7 p* Y3 L# S% y! Z9 U
MachineCode(9) = &H8/ f; o3 p# q% k4 @ B$ {, r
6 W8 w* i9 ]5 E( c7 j$ i; d) }
MachineCode(10) = &HF 'cpuid
. g" J* H8 D; O% d( i/ ~ MachineCode(11) = &HA23 T- D! V5 i" H: L
5 W2 n- S# |/ g) h. O9 T MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]% p$ r* V K( r) j
MachineCode(13) = &H7D
/ U" o; k* D+ d MachineCode(14) = &HC: w1 W6 R" x, X& `% W
5 y) r: A, E' z$ @ H2 U" n
MachineCode(15) = &H89 'move dword ptr [edi],ebx) ^. b# A1 O8 a- R* W
MachineCode(16) = &H1F0 ~3 J9 C/ j5 B) H4 j
! F. c1 b: x: M& }/ V6 Y MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]
- q. @- e$ G9 n$ S1 |5 F- R8 y MachineCode(18) = &H7D
5 q0 m5 v$ {- G6 I0 C MachineCode(19) = &H109 L( @7 `! [, r% b
: T3 j( A: d! f* @- ?) V
MachineCode(20) = &H89 'move dword ptr [edi],ecx
0 |) M1 |1 x; t( T4 c2 E0 @( b MachineCode(21) = &HF" k" T. @/ ]% X! _$ }
3 a# Z2 y% a+ \- N MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]) ^" D3 ~# [! j2 t0 D9 |6 l: w
MachineCode(23) = &H7D; Q3 K) z& C- @6 K/ L
MachineCode(24) = &H14$ O+ Z) }. r. P( c+ g; ^$ \. |( U
+ k1 C+ P- K5 L; W! g MachineCode(25) = &H89 'move dword ptr [edi],edx3 D7 h+ F+ \+ K
MachineCode(26) = &H17- L. M" Z0 K7 U1 H! [4 G7 G$ g
3 x# \* L/ R& ~& Q MachineCode(27) = &H58 'pop ebx7 D( B8 Z/ ]7 n0 Z$ i
4 h! Z# U# ^! [4 S% {0 B MachineCode(28) = &H59 'pop ecx6 R. O% n m7 S+ a" L) b
$ Q' w* e! L0 u C" U: n MachineCode(29) = &H5A 'pop edx
9 H( N g; Y) q- L
0 A! l) t8 Y5 q# N+ g MachineCode(30) = &H55 'pop edi) r: ]# ^* P, k
! ]; s: H. o6 T9 m
MachineCode(31) = &HC9 'leave
5 Z, `( r* Z: F& N& v- V% c; C0 _& Q, |# o" o) C4 f* n' Y) Q
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
& j, \3 _3 H: L' d" ~ MachineCode(33) = &H10 ' but all produce the stack error2 I, s. n' e/ t/ p: @, w
MachineCode(34) = &H0
# {0 c+ n& B W3 _: I3 v + t* S- z( ?4 ~
'tell cpuid what we want
& v0 D4 _2 k1 V% a6 U# ^5 b: E EAX = 0 S( @, d9 M8 b8 J3 J0 r1 _
6 h# p- e+ |2 Q5 V! p 'get address of Machine Code
; j# T% o. q* X) E( S) i VarAddr = VarPtr(MachineCode(0))
- X5 @7 A, n1 \% i! l2 B " s5 ^8 d2 M" e/ Q: E0 |+ B
'get address of Sub Dummy4 z5 [; v% u/ X) Q% l! n$ c
FunctAddr = GetAddress(AddressOf Dummy)- k1 L4 u& N6 t9 E0 e6 y
|9 |# B0 o. r. g+ [$ x7 }
'copy the Machine Code to where it can be called& H W% d/ I' j/ ]6 a
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
( K' W. F9 q" `* k 1 O) I S _0 A) U1 O
'call it5 p5 b3 e% ^9 V9 f
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
+ J3 \1 K3 V+ m CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))) Y+ M3 f& @$ E4 y
'Debug.Print Err; Err.Description& J+ p) S, H, c$ K
'MsgBox Err & Err.Description/ P6 I: x# X Z7 j/ a+ ^! q
On Error GoTo 0
. W) v+ X) ~1 w& G7 u, o2 y' k ! D$ K( s! Z0 W4 Y" S3 Q5 m
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName# A0 e0 y9 h( E
?0 s+ A# ]+ t) }End Function
7 j2 r, o6 L6 g( ]. U4 _% K5 j
]: o8 W* S, _( ]Private Function GetAddress(Address As Long) As Long
/ X5 p& m/ i* N6 f
+ L* R+ V% B* L0 ?1 t, E( h5 A6 q GetAddress = Address X4 t. I; [% i2 S
* J) c% R M. _) X( kEnd Function
4 C9 h" C% z0 ?3 K5 X: I" B; ~8 U. X5 |
Private Sub Dummy()
7 X5 K/ e& J8 f! E2 P2 C& [% s8 I& Y3 L3 D2 Y
'the code below just reserves some space to copy the machine code into; C2 p5 _" o9 R; }% J
'it is never executed
5 d f/ W V4 P4 [- ~) D8 x+ f4 ?. J [: _: `/ z" C
x = 03 W5 q I# q3 N& J
x = 1. ?8 l# O B; V5 R" V: m
x = 2; U( t8 M/ K1 G0 d
x = 3
" y; K' N: x/ G' i x = 4
1 t, v. I3 s: z, l x = 57 c0 m% F2 a3 _& @/ Q
x = 6
6 J6 M/ }+ P+ F' x+ K( h' a x = 7
# }2 d3 B2 Z9 i x = 8
! C5 ?+ v( W" a) ~* M3 @4 \2 V x = 9+ K d2 |0 J8 G8 c P
x = 10% l/ {! L* ?* i
x = 0
4 \7 {! t. K! E x = 1
/ w: d0 P0 _; e1 ~) G9 e( O6 u4 o2 \ x = 2- @0 m5 z3 ?0 ]2 ^
x = 3( S% C3 x! A8 E5 y0 l; o" p/ [0 y0 r
x = 4
2 {+ A a! S7 c4 w x = 5% C- P e) w( X& ?
x = 6
2 y$ m3 V% |2 o$ q8 i. T% Y x = 71 `- {; E; i: K7 O \2 ~! z
x = 8
# N/ w; z' N2 W$ j) T7 o% H x = 9
" e. g3 d; G" I& r/ u x = 10
- E4 X) I; g, F: u s! n
/ C' C; Q3 c' l1 S' Z, G2 QEnd Sub
, t3 I" K# j6 E" D V/ Q5 d------------------------------end--------------------------------------
# }0 l; K; A# Z2 J7 M" d1 S/ Y0 `1 i9 r6 d
. K, H- Y; N8 ] D+ Q8 H
Z; K5 a/ q9 d6 T/ V7 _ |
|