该用户从未签到
|
转载:
) { t5 W! B9 G w) g+ _: E来教你如何在vb里嵌入汇编!
" a5 s, {& r; m) z5 m作者: wl3000wl
( |! A- e5 Q3 Y* n* {本贴绝对值得你珍藏.
: G0 {0 O8 v: U下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
9 w& W0 L8 ~/ W7 {. ~工程文件分为一个form1.frm 和一个模块module1.bas
6 a# t- p4 ?( D2 {, ^, l/ k) z----------------------form1.frm的源文件---------------------- g. z( X% g, U d& Y7 y
VERSION 5.00 o1 W; G( c' {# _
Begin VB.Form Form1 4 V2 N* w% t2 ^1 a1 s y7 y
Caption = "Form1"
9 J5 S: R; F( }) ? ClientHeight = 1965+ F) t. U* W' s1 ?+ V1 y1 P
ClientLeft = 60
' H- y! A1 i/ Y& K. R; g ClientTop = 3454 b0 _0 C H/ m# x% R& x) Z
ClientWidth = 3105
6 T6 F. o" z& {1 I8 y LinkTopic = "Form1"8 C& P5 H/ d. t8 Y" H, S# ?& ?4 N
ScaleHeight = 1965
. T; r! p, @! [. @( w2 _; v ScaleWidth = 3105
1 o4 y- S: H% @( m* ` StartUpPosition = 2 'Bildschirmmitte. U/ ?, n/ t* C
Begin VB.CommandButton Command1 " K/ X- q5 y# K; l \& {3 z( X Q0 q
Caption = "Get CPU Name"
6 ]3 O, B" }) n' S) B0 Y Height = 495# @1 c+ K) Q% J, p
Left = 840
. Y' N2 m& I* |. F7 u; i# S- l' q. Q TabIndex = 0
8 W( u' _' l+ }- B Top = 315: Y- _5 Z# I8 K2 \# R
Width = 1425" s* U H" T9 V0 c5 X
End
3 y/ ^1 s3 Z1 b! w& m* S Begin VB.Label Label2 : G. h" C. J7 ^4 R" _
Alignment = 2 'Zentriert* ^) G/ s% e+ x: R) J. Y
AutoSize = -1 'True: Z% Z" J* E: s; K* T
BeginProperty Font ) n. K6 ~6 ~+ g- L8 k
Name = "MS Sans Serif"! c- o# q8 ^+ l& l) @( J
Size = 9.75
4 F# f" \. l" I+ l% W. n Charset = 0
, m5 V4 T- S8 H- C Weight = 4007 B4 C x2 A; E) U5 O
Underline = 0 'False
5 ^! v8 F4 j# v) n* |! e& ~ Italic = 0 'False
: b0 u I4 T: ^$ s Strikethrough = 0 'False2 N) q" ^# Q g3 l5 q
EndProperty3 l0 B2 ?+ m: _0 |! i/ K
Height = 240/ w; |& n- @) v
Left = 1515
0 f" Z5 h2 w5 F- v! E z' D TabIndex = 2( O! k7 y% ~2 |
Top = 1065
- Y) T, h% D8 q1 p) \: p) X! @4 J j Width = 60
6 J4 t: _& |$ m- T0 [$ y! m End
' p9 w5 k. O3 Y) S# S Begin VB.Label Label1 & S3 d, u& ]( I
Alignment = 2 'Zentriert
; s- r: ~' s4 h# G AutoSize = -1 'True6 l- r! L# I C2 w% x, ~
BeginProperty Font
; M4 X+ f/ J8 |: \& ? Name = "Arial"
: O0 _: c- N* B) @ Size = 127 T1 _; O; F5 ~/ m& X
Charset = 0
/ X& `4 ~# A# i$ _( W Weight = 700
5 B# g- \. ~7 S& ^8 ] Underline = 0 'False
! Y m" x4 c% G/ z Italic = 0 'False
: f: [6 O0 g& ~5 d: h Strikethrough = 0 'False* \) _8 I; R6 K$ f" d* T' Z
EndProperty& |8 A- H5 f. H9 r6 g/ |5 G; c
Height = 2851 p/ `4 a; z& J
Left = 15155 B6 H/ R- ]8 {. v# B
TabIndex = 1
4 M4 ?9 {+ q& f8 o% E. y Top = 13509 `, R0 U4 ~3 f) @! I
Width = 75
% y$ ~: ]: C0 A2 K7 h% R& d& w End
0 n$ ~; o, N4 T( YEnd4 v8 f; P3 e5 @/ S
Attribute VB_Name = "Form1"# s; f; v. F7 h m
Attribute VB_GlobalNameSpace = False) a! ?4 U9 s8 |$ V
Attribute VB_Creatable = False
. E8 {4 C8 y3 e6 ^, SAttribute VB_PredeclaredId = True5 w; V$ }% q0 y$ d
Attribute VB_Exposed = False
, ~' q0 M. O, }( T2 m) ?& V1 r& lOption Explicit
h$ [' e8 `! X8 \. Y. h& i% ]: o: {7 R3 ]/ J
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
. `2 l4 k. q( K( y% L3 h' {* r/ X# T* {/ G3 {
Label1 = ""' W/ x! W% [4 q" P3 b
Label2 = "", x6 z* w" O4 O9 O# d7 K
L7 k0 g. x/ A: KEnd Sub6 |; @7 T$ [3 T
6 o8 ~" N- w, ?8 l+ iPrivate Sub Command1_Click()3 \5 E& k8 H4 u$ L5 @6 l
8 m& S; B4 P$ L Label1 = GetCpuName() & " CPU"+ y# t8 C0 e T0 w6 ?' v/ ]
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")
4 l) U' K5 ~2 O5 Q( J1 _
v0 F1 H4 y ?! p4 G6 E3 j3 bEnd Sub5 P, A$ b& ^' I6 t1 r+ [- X
------------------------------end---------------------------------
# \5 y$ _, j# S; W: l/ e4 Y- `, g. C
& D+ i9 V) _! L5 N, U
4 y9 b/ y; E: S. E# Z. w; C, f2 J+ h
& i1 c' ^" _8 C! W2 |; u/ [. c+ k
( H) H4 T u5 X9 A* J; w: V下面是modu1e.bas的源代码; k* f! _" e( E6 c2 l: E
) a6 F# l, S/ k6 q4 o$ L
----------------------module1.bas的源文件--------------------------
2 _6 W6 M1 S: R+ a' D" y7 XOption Explicit
N$ z3 o/ K; k4 N$ ]" A'
, L6 Y2 d1 | p1 [% y3 j |- h'This shows how to incorporate machine code into VB7 X) _( T- Z9 X) R
'''''''''''''''''''''''''''''''''''''''''''''''''''+ @6 ~7 f; D+ W) h/ X" S- C/ x- E0 A
'The example fills the array with a few machine instructions and then copies' u& r( _1 d/ ?
'them to a procedure address. The modified procedure is then called thru
* s* Z. P( V. |7 ^# n" J9 j'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.# ]- r) O! B+ Y" T$ s
'
: P+ Q# _+ i" U'##########################################################################5 Z8 |+ f5 U+ W6 P' M0 O
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody
( b: v% c. V x'can fix that please let me know... UMGEDV@AOL.COM
2 n) h- _ I M, R1 ?, Z'The Error is not present in the native compiled version; so I think it got
( i$ [8 J, @4 B0 ]$ L( N'something to do with the P-Code Calling Convention (strange though)...1 R6 C, L# N3 Q* j0 ~. `
'##########################################################################
/ [1 @( S7 F6 z'
% O' W. O' ~7 x7 E'Sub Dummy serves to reserve some space to copy the machine instructions into.
8 j, X7 |, P1 l) {'
) i+ x* |" ~! p7 y'
& s: ~2 E4 _- C9 M, h'Tested on Intel and AMD CPU's (uncompiled and compiled)* @% q3 o b( a! q+ ^ {
'
: S1 `8 G" j T1 N `# g'
L# X# ~# G( Z$ v- RPrivate 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 Long6 E" _1 i9 h4 Q. ^2 ?/ [
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
4 O" ?8 [( D. p$ G( U1 |! NPrivate x As Long
+ J' K; t3 l% n$ s9 ]$ J9 ]8 U8 v# n9 o1 H l$ W2 o
Public Function GetCpuName() As String
+ ^9 a% H9 I& m
* U5 A4 |4 H* h0 P8 y Dim MachineCode(0 To 35) As Byte
; X8 M, Z& X: `. N Dim VarAddr As Long
# v) i6 X; _' W5 f4 v Dim FunctAddr As Long
& T! a+ N- _' Q6 q3 S5 X Dim EAX As Long
6 w, S7 Y) C) ~& p! x. f Dim CPUName(1 To 12) As Byte2 j5 ]7 r2 R) ]. Y$ W+ q) b
, s6 k/ G" c7 }1 b 'set up machine code
; S* X! U2 A `: \4 @ ' k2 t( C8 i4 ~
MachineCode(0) = &H55 'push ebp/ U. b( Q h9 R; K
" q: F, o2 k8 I! T/ g2 |. F
MachineCode(1) = &H8B 'move ebp,esp1 }; u, r- t8 N% G
MachineCode(2) = &HEC, v9 _$ E+ B3 b7 A8 o
0 D/ j W) h8 |4 K3 z0 z2 c/ r( L
MachineCode(3) = &H57 'push edi1 H2 N8 y# R! w- R
+ Q" l2 o/ M/ P: l
MachineCode(4) = &H52 'push edx
+ k- O* h4 w4 T7 X9 Z7 x+ |! a
$ | z( F; `. x" W6 l1 c: ? MachineCode(5) = &H51 'push ecx. n, f J/ `9 A, X, s7 V& ?
4 m- |2 w) H6 `! x
MachineCode(6) = &H53 'push ebx4 Q) u+ y9 {+ w, x* V- W
5 x" e, L0 I7 t& F# H y0 {8 H& i MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]. [" H* Y# j& z2 Z( C, ]
MachineCode(8) = &H450 I5 h3 T$ K) l; e$ G3 C; I& o5 _
MachineCode(9) = &H8
7 h- K8 n8 ]1 \$ v , R) y& o, S* T$ c
MachineCode(10) = &HF 'cpuid& B9 v1 D: F$ K. d9 Z& n) c7 O5 o
MachineCode(11) = &HA2( j" X- H% \% A0 ^
$ V/ j7 {( |% |' k( P: Q MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]3 Z% n0 L! E) Z$ u# g8 C% I
MachineCode(13) = &H7D1 [' b0 @) C# b
MachineCode(14) = &HC
$ t: x3 d1 ^6 x0 s y" j1 h 6 f- U+ P+ ], D4 R( {! S
MachineCode(15) = &H89 'move dword ptr [edi],ebx4 f& e$ f/ U. n& e4 g$ n2 e+ F' z+ U
MachineCode(16) = &H1F- P4 W2 \' x4 I7 [+ n
9 ]; w4 I5 I. w. K
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]* `1 e1 ], l( e% n! S
MachineCode(18) = &H7D/ D0 D& v& |3 U5 { ~* \* I! T
MachineCode(19) = &H10 U4 q. T6 n& V9 [2 D9 o) a$ l
# G1 m! E1 M A, W0 F ^9 z9 k MachineCode(20) = &H89 'move dword ptr [edi],ecx
. ?5 i+ s: l& V8 s8 J MachineCode(21) = &HF& l% x3 l1 J6 Q
# X& z) U0 C! s1 D MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
. V- `4 A' ]6 u MachineCode(23) = &H7D
' X7 _* R' p( _ MachineCode(24) = &H14
- E2 g7 h5 B$ O* z$ ? u
+ D2 a( ] T% m7 E4 C2 P MachineCode(25) = &H89 'move dword ptr [edi],edx
3 U/ f, L4 ?# G MachineCode(26) = &H17* m/ \3 Y) E( m( M: R" n0 \
" X) d: W+ ?# W0 z: L! y+ {
MachineCode(27) = &H58 'pop ebx
" r: o! y+ m0 M
9 e1 K7 ~. L. i/ L- o5 l, T3 r6 } MachineCode(28) = &H59 'pop ecx
% E2 M, K/ j! u" R
: @9 x- o* M- f% |) A, R; g; H: I MachineCode(29) = &H5A 'pop edx7 }( M( g9 k$ i" L3 G' P
5 u: W" R" b# j; c) N0 l; R
MachineCode(30) = &H55 'pop edi. L* ~- c8 t' U/ U+ t+ j
- s9 ~- ]9 a- l4 C) A8 v
MachineCode(31) = &HC9 'leave! h0 J5 D! L8 b |( t% X6 [
. }9 y8 h4 N6 O
MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24' P0 t% ^+ f) S2 s# H1 ?
MachineCode(33) = &H10 ' but all produce the stack error
) {- ?( W2 O! s( p. P" o MachineCode(34) = &H0
h1 u) U, d2 _1 e9 \ / J3 g3 t# k: c* B P/ g
'tell cpuid what we want& \; p% S5 B T2 C* [. H
EAX = 0! L* Z' ?2 h; Z, s, p. \ d
Y* ]: J$ h% u. E 'get address of Machine Code
' q- o) E9 A4 U y' O0 m VarAddr = VarPtr(MachineCode(0))+ k! {6 C1 c8 f& H Y/ V2 d
5 \2 O! i+ ]9 s, f 'get address of Sub Dummy
) n$ ]8 z J' v3 ]* w FunctAddr = GetAddress(AddressOf Dummy)/ B/ u. ~0 p0 r: t7 I
) I( N( q4 ^% Z# |4 p 'copy the Machine Code to where it can be called: w4 k2 v+ r$ W' H- c8 m
CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code3 l( }: T# W5 s7 W8 m
3 L& g, K0 b$ C6 R) z 'call it6 |/ w3 F+ Q5 F; Q9 y0 k
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
- O% m4 A+ g- C& T CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))
3 _2 P1 Z1 o, v7 s, ` 'Debug.Print Err; Err.Description+ [6 I0 ~% |* F8 d- h8 p, f
'MsgBox Err & Err.Description
7 Z5 W5 q+ }# K+ [9 L5 A On Error GoTo 0
" M% @$ Z! G: d" J & W# Z" y) J Q* O _, a
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName+ C2 C& E7 q4 i& S6 B
& I' F+ s2 x5 Z* M. IEnd Function3 a* p( k% r9 p& [6 g
4 `. ?- B2 U2 S1 E) u
Private Function GetAddress(Address As Long) As Long
. o! G5 h: C+ p) E
7 Y9 D- B' J5 `1 X; M GetAddress = Address
( ~1 u8 a1 R$ ?
9 @3 y# B$ _! r- PEnd Function
- |: u* Q1 o9 [, z; x+ @3 H9 X6 e1 w, l# K! ?, [
Private Sub Dummy()' N% _" Y/ Z. y( ]: ]
* b6 u* H; i- T8 t; F/ H
'the code below just reserves some space to copy the machine code into
' j5 U# B' E+ B; ]7 q7 \2 Y; b 'it is never executed) j5 ^% V; O7 Z$ ^# h
( s8 K& i# E1 s0 u# O, k* R
x = 09 L! M1 ^' D( |7 s, @* @
x = 1
5 M( U8 X. {0 g/ ^+ L9 O x = 22 p5 c4 @, [, N8 Z5 C& [
x = 31 E# d8 `* s, A7 y. k
x = 4
' v7 @0 Q- D% r7 i# ]5 m x = 5
: d. Q/ K: E h3 U4 f x = 6
- i5 A% w( {. U# ~+ H; k x = 7
5 H( y+ r2 U8 x' y: n6 k* ^) t* v x = 8
8 c! x& m5 K7 B% Y; n8 N0 R9 U x = 9- m9 M/ L3 e! f- h$ A
x = 10
" n5 O3 O& h# f3 ?) ?1 m, {/ A x = 08 e6 Z+ C; L6 K
x = 1+ [9 o7 d9 N+ H- }2 f
x = 2
; [+ B$ x; P: |% P x = 3, |3 s3 d. B' B7 Z
x = 4; H$ s# B8 X: f1 v1 }5 B
x = 5 e0 e' x5 m o$ ]9 X+ i
x = 68 C4 k. q( }+ [, C, {
x = 7
" r/ _* {! h% ]/ P1 A$ ]/ \ x = 8) d$ K% O+ K: K6 V- s
x = 9* m5 U( W7 e# X3 K
x = 10# {# }& p1 N# L9 ?
* J! _. |. Y$ \9 k" m- ]
End Sub
, u; |+ L! @: ^ y Y. k------------------------------end--------------------------------------6 [- C% F; [) f% E3 `
; ?( L5 c9 ^; t ~8 k
1 Y! E* y( ?7 u* Z ]5 K' w& Q8 S% l1 t$ p) e5 U; B
|
|