|
转载:
/ A2 }( G" z5 a/ ?. C, k来教你如何在vb里嵌入汇编!
/ L N: x- I% N" Q9 n作者: wl3000wl 1 _. s( ~' D$ i! g3 [2 _
本贴绝对值得你珍藏.
" C4 n H7 V% G6 U; J, s9 {下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
$ S% f; R# h) v6 R工程文件分为一个form1.frm 和一个模块module1.bas5 H( S* \0 D7 r9 D4 Z9 N. A
----------------------form1.frm的源文件---------------------
2 @* F) j* j4 C& u- ~VERSION 5.00) C; b+ l$ m( A+ t/ T ?5 W8 _
Begin VB.Form Form1
. C; L/ a6 a( S) R5 L% i Caption = "Form1"0 N+ m* Q2 b- ~1 G
ClientHeight = 1965
, ~: _, S$ C+ M ClientLeft = 60
, x6 z W* q- T, g ClientTop = 345
5 G+ B8 @6 D1 y ClientWidth = 3105/ T7 F5 Z. ]* E5 i7 \. z
LinkTopic = "Form1"
: G' D$ Z" N1 W/ I2 z9 z* k ScaleHeight = 1965+ f# C) G3 v$ |7 O
ScaleWidth = 3105
7 v- k/ b/ F/ }* r( j8 q3 ^ StartUpPosition = 2 'Bildschirmmitte5 M0 b$ @ h, `+ M5 j$ ]
Begin VB.CommandButton Command1
* X& Z. w! V6 |; U& i, D Caption = "Get CPU Name", |1 p! W( h( I8 G; O
Height = 495
7 R: X- |, H$ \+ A Left = 840
6 [4 L' A q+ a4 r" u2 X7 _% [: U, p$ T TabIndex = 0
. s; ]/ B+ D4 @" `5 D Top = 315
- j& W- N& \ x; O, P2 Z) b* D Width = 1425
! ?- ~1 V6 f: P0 @. F9 ~ O8 L End9 {* Y, t9 S+ z# q5 }
Begin VB.Label Label2 " k# X/ Q9 V3 Y. {
Alignment = 2 'Zentriert# `% o. @5 V2 [( B0 S% ~
AutoSize = -1 'True5 J# @$ F1 i8 ~$ L$ |7 z
BeginProperty Font ( t3 D6 r" g& M5 W. X3 a3 i% b
Name = "MS Sans Serif"* x8 L+ k' i2 |
Size = 9.75
i$ z/ b7 a1 k* `, X7 M# {7 b Charset = 0& p0 i- {/ _6 E$ j, Z) k6 W
Weight = 400
* ~( n ~. ?4 C& d Underline = 0 'False' }) M/ W- X1 p" E1 O9 y" V S
Italic = 0 'False
& J, R+ v) ]% S, P& @, w( N% E w: ^ Strikethrough = 0 'False
9 g2 e/ E, Z& i8 a H EndProperty
* M* C) @& m% l4 g- _ Height = 240
" A9 k" _( k- G% L1 ?- S* M+ `% | Left = 1515
% U) w# T+ s8 ]/ y TabIndex = 23 ?; X% Z( ~- b# }
Top = 1065
% l" c$ b0 l& u, ] Width = 60
1 z5 f/ t5 m2 O* y- K. E6 { End: \* O' O1 H+ `7 y, p
Begin VB.Label Label1
! l- E; Q4 Q! h3 O, J2 E- ] Alignment = 2 'Zentriert: R# ^! C$ P6 n. e; }" `
AutoSize = -1 'True
" M; I" E8 ~; i( s0 M) m BeginProperty Font
0 x( n# u- |/ o2 |; D Name = "Arial"; u9 j% W5 q) \5 w) s. u
Size = 12
( |5 n x0 L+ d! l# \ Charset = 0
/ o! }5 p# q0 N, o# `* ] Weight = 700& n+ }9 {5 o* [
Underline = 0 'False
0 f& t. q7 F$ [2 ^* e Italic = 0 'False
4 E9 Z+ Z9 g, h- } Strikethrough = 0 'False
x3 u4 G, g" f" h% [ EndProperty
2 z# c9 d# L5 ?& V }, c Height = 285
h" r* k [ B3 V- k Left = 1515# A8 x+ ~7 U! b; G5 Y b7 s
TabIndex = 1( X9 v8 }5 U7 \1 k. P0 W [
Top = 1350, n5 }2 o) {2 e& K7 N; b
Width = 75; [ {* k. a2 W( P0 q) f# o
End: x' r) I+ b+ Z
End5 F& Z& _2 e1 c5 A+ S
Attribute VB_Name = "Form1"
. I% \4 p( K" N9 jAttribute VB_GlobalNameSpace = False v+ j1 C& B+ U; }6 h! g/ p
Attribute VB_Creatable = False
/ p6 T) U0 q- u9 J) b9 `. J& xAttribute VB_PredeclaredId = True6 M: u9 n ^/ w8 y w. R
Attribute VB_Exposed = False9 }, A7 g. T3 a6 U+ P
Option Explicit( c* P9 z/ u9 D/ o: v4 N* \
6 L- W& K P5 J
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single). S' U- M0 V$ u- d! P
" L; c2 h2 c: h$ S7 O) i Label1 = ""2 t6 d7 I7 o3 ?5 b v' ?) Q
Label2 = ""
L9 y+ s# Z* m7 a [, ~* {; H/ z) y4 N% W' b
End Sub8 }; `6 [" j8 s+ G
~; c5 q7 W yPrivate Sub Command1_Click()4 i' m9 B' d2 g- f- m0 |6 l+ {% u, q
' J( h# D6 I1 H1 s9 J R Label1 = GetCpuName() & " CPU"5 V9 p1 Y* o ]1 B- q! {
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
; ]: J+ t# k" |. I
0 V z6 P/ Q8 V( H+ E% PEnd Sub! W6 X5 h2 K) j @$ K, K
------------------------------end---------------------------------2 C" L7 d5 |0 H) d" y
" r! l7 S6 c) |7 y2 W6 k+ D
9 j$ x. P+ r7 x8 B* D0 H6 ?
, M4 K1 @* R" r7 Z' @) @3 T& v3 ^7 q9 x) Z7 ]
: K; |$ b N) F+ |2 T+ n, A
下面是modu1e.bas的源代码/ K- ]% _! o( f n3 J' B9 O5 e5 X
0 R+ B! V: j; i
----------------------module1.bas的源文件--------------------------8 K. m: C7 Z5 u. z8 y+ u5 }
Option Explicit5 G6 ?) ?8 t1 W8 K3 ~5 d, u
'
1 i4 B# d0 ~! A4 K* Z'This shows how to incorporate machine code into VB9 D4 \/ n, B3 I% M5 N/ [+ D* A3 Y
'''''''''''''''''''''''''''''''''''''''''''''''''''
* o: o& J& [+ u5 y'The example fills the array with a few machine instructions and then copies$ m' N% r6 S2 o7 ?! u. D
'them to a procedure address. The modified procedure is then called thru
' ^2 r, W1 o' p B) K" A'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.4 N3 B6 f9 Y4 d# u1 K. C |! w. |* o
'% p1 R/ L1 L/ E9 }$ |
'##########################################################################$ W+ K0 ]. j; F) x/ I* w! ^9 o
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody4 d0 J" w* Q9 [
'can fix that please let me know... UMGEDV@AOL.COM
}! Z& m0 q/ B! y9 {/ _. Z'The Error is not present in the native compiled version; so I think it got
6 ^. y; l1 x& o$ w$ Y. q'something to do with the P-Code Calling Convention (strange though)...3 S0 e( v; ~5 {: K8 K& \. | x d- ~( U
'##########################################################################
3 B4 I8 C" W+ |0 v# W6 L: X7 q'2 h% X6 s5 { }
'Sub Dummy serves to reserve some space to copy the machine instructions into.
, N# i. `$ ]3 ~3 {'
, b: L' }$ @6 ]3 S, m'4 S1 l& u! K: G! m3 |6 g
'Tested on Intel and AMD CPU's (uncompiled and compiled) U$ y8 E" N. Q5 ?9 w
') f# K" Z/ I. O0 r3 }0 B
'
& W/ j- `- S6 W: i7 G4 M, aPrivate 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
i" ~$ r4 }' l* A9 f) RPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
+ L, E( {( O, Z3 K6 K$ e, z& mPrivate x As Long
4 F0 \: e5 P* B% r# Z4 x2 M: ~: X: U8 h- r# y B s/ h
Public Function GetCpuName() As String; x. r" X. c: w4 w8 z+ Q. ?
3 r, l; l$ B: Z4 r Dim MachineCode(0 To 35) As Byte% @! C# Y) z' }! K6 m
Dim VarAddr As Long: _, X% ?, b* z& p; E" O
Dim FunctAddr As Long7 W7 X5 G4 J# E4 K( L
Dim EAX As Long
" M; m! D. B5 v/ X, H$ e Dim CPUName(1 To 12) As Byte
* e* a! Y% R! }+ p8 R0 o* Y # A' j6 i6 p& q
'set up machine code
, q# Z) s3 f" g % u1 a% B; t9 Y) |
MachineCode(0) = &H55 'push ebp' S& W' D! n& A' b
- Z1 {/ d X8 }9 `! q- i0 ~7 o
MachineCode(1) = &H8B 'move ebp,esp) W& ~ \: ?) c4 E
MachineCode(2) = &HEC. {8 ~) l1 E" k! R; c* z
& E' W' b4 C6 n6 J2 q MachineCode(3) = &H57 'push edi
% E# q$ i; C0 ~- U5 k
5 H2 M3 U2 Z+ [8 T# F" y MachineCode(4) = &H52 'push edx
& ~0 J0 b( I- m3 n, P 5 }+ k, t* V/ C' p! ]" K
MachineCode(5) = &H51 'push ecx
4 |6 ~& Q+ U/ G 9 E' X5 Q$ z4 c+ G0 H
MachineCode(6) = &H53 'push ebx
( h1 q0 Q H( L2 ?' B& Q8 ^) |5 h b3 C+ S9 Q2 y
MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]2 l3 E t) b) h, @4 c [
MachineCode(8) = &H45/ }" z: _3 t f; f
MachineCode(9) = &H87 b+ t% ^: o) }5 S; r: R \5 k
; k1 h& z9 q" h3 t7 m# @" M
MachineCode(10) = &HF 'cpuid4 y0 d( T2 Q% e
MachineCode(11) = &HA2
6 t6 s& W. N/ B4 q5 S$ @' s
' |1 K3 t' D, y MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]& N. M0 @' r, F2 `
MachineCode(13) = &H7D
5 ?/ c/ L6 b2 i8 w& o9 \# ]1 p MachineCode(14) = &HC
# a: _# I+ d b! i1 e2 [2 g: B
9 P" G- q7 E$ M W, J. ?7 C MachineCode(15) = &H89 'move dword ptr [edi],ebx& A" F& i# R% U2 Q: l a3 u- O, h5 \
MachineCode(16) = &H1F
, A6 k) f4 B! k
' P r7 ~7 e2 f6 @# s; Y MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]5 i( S5 f L3 h" _; A( K, C* n
MachineCode(18) = &H7D$ m9 h2 z$ ^" A1 ]
MachineCode(19) = &H10, x6 f9 [. j8 ?, D Z k% [
3 Q9 v, |9 Y* n4 M6 W. N
MachineCode(20) = &H89 'move dword ptr [edi],ecx+ y2 a" s" y2 I% Y5 g5 A& E; ?3 Q
MachineCode(21) = &HF+ p+ ?9 c' c }/ r S# F S
7 s8 r# B) D/ m% w3 Q MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
! c+ Z$ a+ p, W2 |( |0 y MachineCode(23) = &H7D
$ T K) ]0 O9 x3 q( K6 P: f: b* D MachineCode(24) = &H14& c$ W! D6 \1 e: B
2 ] y, ]- [. c5 a
MachineCode(25) = &H89 'move dword ptr [edi],edx
: u2 Z. z- D2 e( j. K6 W MachineCode(26) = &H17
7 @: V J9 r9 E4 L; T
( f+ \+ \1 ^2 l5 j MachineCode(27) = &H58 'pop ebx
$ @) @& u# o7 v
. P- R6 \, O, x MachineCode(28) = &H59 'pop ecx
! S0 S# p; Y6 O0 u A9 _. r% Y- B( p" N& x# V. |
MachineCode(29) = &H5A 'pop edx
; X! I( e9 I2 R- }0 B' E& t. E
1 J0 I9 l, X0 w, ?2 L6 l5 K MachineCode(30) = &H55 'pop edi
& ?+ v8 p' q# D _* s0 Z) A " S/ T8 P. s- r: ~' [+ K
MachineCode(31) = &HC9 'leave
2 d9 `* k T# ^* o; a5 }. C; P, \0 } @# D D/ `7 {
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 249 y. Q9 F% S5 I; q( m2 [$ L
MachineCode(33) = &H10 ' but all produce the stack error8 t* S6 s# T1 _0 H
MachineCode(34) = &H0: k" D' @* f1 b- \1 C
% a& f8 ~/ i1 j6 ^
'tell cpuid what we want
9 d7 k* F- S+ A3 i! U( } EAX = 0
, l5 x+ U6 s+ A# X
) y) U% a {& G B; Z2 \ 'get address of Machine Code
5 E$ m! f5 m% M J VarAddr = VarPtr(MachineCode(0))% |5 b3 S2 J# M, m* r; l
5 P: G7 R% S( |! C/ j' e
'get address of Sub Dummy
+ B( r! P- z+ w! a3 Q! o FunctAddr = GetAddress(AddressOf Dummy)
3 E+ k# D! X' s
; ~& X6 ?6 j/ H# \' x- c# J b 'copy the Machine Code to where it can be called
6 E! z: ~& r4 N2 w' @! O b CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code( V0 f+ k& F4 Y2 I% L
9 e3 a6 X- j g/ K: k 'call it
9 O' l9 T2 Z) f On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
+ e- e/ T7 u& n! \- P. M% Y CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))* T* R% S6 A+ b* g4 o; F9 l# v
'Debug.Print Err; Err.Description
0 f* a' E+ W, l7 { @& U- f! H 'MsgBox Err & Err.Description
5 C, W% D: c, K" A& {* `! A On Error GoTo 0
1 n, D6 \( J* b' E0 D( P* j
9 @2 h z8 t6 z6 k& L GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName( Z6 W6 i: B6 F7 A. v" H5 ]* u! k
: H, e0 S6 _: c6 X# z2 Z/ NEnd Function
* ]7 a' E8 H% P( W0 `0 P, _
! [, a" Z& |/ t( _Private Function GetAddress(Address As Long) As Long: X( k" a# j5 Y; _4 X; ^8 J
2 ^: Y' [3 R1 `/ Z0 W) O* X4 p GetAddress = Address
: [! u6 Z8 J. g4 s8 [% b
+ T# e d; c4 r% Z! Z0 ]End Function
* `% t6 u3 J% i: |( B
2 L; ]' u. m( @( j6 L4 G1 W1 ]Private Sub Dummy()" z3 x% V- c1 h1 l2 _
5 Y5 e5 [* i4 u 'the code below just reserves some space to copy the machine code into
- P7 O8 E/ A o 'it is never executed
% L2 I$ [$ Z1 m
- y; G- L' ?7 s" M$ ~7 K0 c# T( X8 M5 c x = 0
U( e( |' @+ o) v) H x = 1& Z% s: P7 E2 K
x = 2' I. k/ x6 v0 E% D. S* Z1 J. t2 Y
x = 36 f( i: T1 A: y2 c O$ c6 J) v/ e
x = 4
' a. v2 W# V1 B( ?+ Q0 p x = 55 G1 [, @8 Q" U
x = 6
+ Q* u2 w! A3 u# p! K x = 7
7 o$ X6 U* C( w) U5 f. z0 w x = 8' s8 p/ E1 y' n( P
x = 9
4 }4 B( Y; N& i! C( m x = 10
0 B/ Q& e) E9 K8 b/ z9 |; _ x = 03 |+ S8 F# h& c" m
x = 17 s8 |* u# y8 c/ M
x = 2+ \6 H: r: h3 \2 K2 W
x = 3
% x8 E% s7 v7 ^' {0 i l x = 4
& |* S; V. E6 b K4 m# M5 r& n x = 5
# c+ n* K6 t n x = 61 X& o& V1 o) ~& n
x = 7
6 N" i6 `5 f T: `- ]% m x = 8" g% @1 b! `2 L! U% t. _- }: P- u' e
x = 9
; E+ d9 `1 \" [; h x = 10
8 e( x# d7 B6 \0 c1 a5 C, u 8 J: r5 x' ?. u7 G- s7 [
End Sub
( y" c/ L' |* L------------------------------end--------------------------------------
$ k/ Y; D; Z8 J' |2 s* c, C, ]' D. t k0 u* o
; s8 g2 S+ N0 |
4 F1 R I5 ]2 i" g1 U
|
|