该用户从未签到
|
转载:* w* }, K9 @" U ?
来教你如何在vb里嵌入汇编!
! i- k- n0 | k7 m9 C作者: wl3000wl & _3 U3 M% z" e7 Z9 x
本贴绝对值得你珍藏.
: e6 d3 v- r' u& y下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.
( L+ R9 l j2 N/ r; X o3 @" s/ @9 [工程文件分为一个form1.frm 和一个模块module1.bas# G: g6 w" g' m3 @
----------------------form1.frm的源文件---------------------
4 u `& W! t9 z% n8 }VERSION 5.005 w+ x4 J0 u) [
Begin VB.Form Form1
9 G9 J! ?8 w9 S% A e Caption = "Form1"
! B$ M5 [% E& G! d, { ClientHeight = 19656 t- D( Q+ d7 k: a9 O$ F
ClientLeft = 60" c' L* |9 Y; T4 C2 X1 L8 l
ClientTop = 3454 M' F* Q6 T( |
ClientWidth = 3105
4 C+ u6 I0 T8 q LinkTopic = "Form1"5 c% L6 e1 b- j2 |! i
ScaleHeight = 1965
2 h9 n& f3 X- K; g2 P ScaleWidth = 31053 v( I0 T5 d) E- R+ e
StartUpPosition = 2 'Bildschirmmitte
* C5 d: S8 p1 k, n! g# _/ N Begin VB.CommandButton Command1 9 [' o* ^" n9 R* L2 Z! o
Caption = "Get CPU Name"
+ T+ r3 N( e. h Height = 495, E, }2 b( r, W
Left = 840* f+ Y* i+ ]" Q P9 `$ G) n( M
TabIndex = 0% l4 o* K4 a( c: J
Top = 315
3 K* b+ @0 V7 ~ l& u6 L& ^ Width = 14259 u, r* g0 D* p4 F) ]' K! X) b
End7 ^: ^- Y& | h
Begin VB.Label Label2 " l9 d/ @1 b* z# [( T; v
Alignment = 2 'Zentriert) k4 G. N4 W% _
AutoSize = -1 'True
$ K2 ?! P. v, Q! o BeginProperty Font + Z l& } a$ V4 Q8 D3 b
Name = "MS Sans Serif"3 t: C! n; `0 _) b
Size = 9.75
! z. y, K, |+ S" @# Q Charset = 0
- a) l2 ` g: [' ~4 G2 F* J7 E1 R Weight = 400
& l1 z8 t: h; t# f$ J Underline = 0 'False
8 `9 m/ Z: m7 A. ]6 } Italic = 0 'False9 a7 o% @+ I, }5 @+ D- U0 `( R; V
Strikethrough = 0 'False
- f, Q! d3 X" o& R EndProperty
* B5 R$ ~$ Q, V, I Height = 240
, K6 N4 e [- T( U9 r5 F Left = 1515! r% S0 p7 x* Z# C8 ?+ }9 m$ d
TabIndex = 2
4 }/ ]( Q$ U2 K Top = 1065" Y* I; Y! l; Q/ ]7 N1 T
Width = 60& l, ^( _* [5 C/ |$ H0 K% C T
End" N8 Z' s2 O+ X& m
Begin VB.Label Label1
9 L) l l( s1 a4 ~6 ^: T, | Alignment = 2 'Zentriert
1 q7 o0 N$ A8 m/ K! H* Y3 T AutoSize = -1 'True
& D0 D7 J2 @ n BeginProperty Font 1 _4 Y1 u$ m$ Z. l
Name = "Arial"/ Q) S G3 k, ~, R+ G
Size = 122 L: r8 Z" A5 O& n. X2 H8 U( c! |
Charset = 0
6 W3 `8 [! b0 w Weight = 700& |$ y" N2 \6 Y6 i* y: f6 e
Underline = 0 'False: c! h- ^0 T: E* h) h% y
Italic = 0 'False5 K! [+ ]5 B/ N, {" u9 P" x
Strikethrough = 0 'False, u0 \2 w# E9 X
EndProperty
4 w, u7 j" ] K: N+ H, N Height = 285
2 b6 \' H% a" o$ `" O Left = 15156 x5 t' `& X) f+ X+ U- `8 d) ~
TabIndex = 1
0 k7 q' P& h2 [( T' F5 v# a Top = 1350' U: x. p; U4 @0 o
Width = 75; Q9 q8 A& v U: a7 T9 b
End8 F. R3 j) O6 p3 Q! z
End& ~0 n1 x' c5 T
Attribute VB_Name = "Form1"
! O7 Z" E; X% }( |: a$ P4 J4 {$ U% PAttribute VB_GlobalNameSpace = False7 }6 W- e- T9 X2 \$ u# I/ R, }
Attribute VB_Creatable = False: B( f& }9 {! l. E0 t4 j
Attribute VB_PredeclaredId = True8 l2 E+ B! A- }0 C' ]
Attribute VB_Exposed = False' D3 L+ I$ i G- p" w
Option Explicit: C- [ z5 E, K
: N2 b) N: M; w; b3 m' x4 \8 _! m% @Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
8 |( D! Z x; ]- h& W b
0 ^" `; z7 _0 E' c! u' q: x/ r# h Label1 = ""8 t0 f5 ]" q) B8 K3 s
Label2 = ""* W: B# f" X% t5 ]2 s( J
# E" S; G o% `$ l
End Sub
+ {# ] ]9 x' ?. @7 P( l
$ f: g' r2 X2 S- t% l5 s1 lPrivate Sub Command1_Click()
8 G" V, Q K5 E( F & ]5 ]/ [/ O7 f# E3 |
Label1 = GetCpuName() & " CPU"* G! P( d% O, ] ~% a. D
Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")# I( Y# H1 ~; R \
* D7 v" R o* i" V5 j2 {End Sub
# V5 B" I2 e) a ^- R5 X7 B------------------------------end---------------------------------
+ q: o/ g" Q: v6 a, }( _" i; I: o0 c" L3 B& a( R+ N u# W6 K q6 K
: K7 [7 x* N5 D0 A/ c/ Z) A/ X/ \! I0 d, K( `
5 c9 C( c3 M3 A
; u' C2 H: F' U下面是modu1e.bas的源代码' E/ ~( \$ z" k3 R
) ~$ f1 _# w( t# v6 X! {
----------------------module1.bas的源文件--------------------------
6 H# x8 i) T7 Q' p' T a+ t4 \Option Explicit( t3 s- }! W8 b7 |
'
: z1 V' C) p0 o1 m'This shows how to incorporate machine code into VB
8 l3 x- l8 r+ a5 k8 ^'''''''''''''''''''''''''''''''''''''''''''''''''''0 z6 o8 k" C! n* U W& k F0 B
'The example fills the array with a few machine instructions and then copies
/ C5 I9 g i- h1 j+ ~4 } K+ c'them to a procedure address. The modified procedure is then called thru
: j# ~* ]* A- k8 p* ^5 n) s y" C'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.2 y9 u* y6 ~: B; K1 V6 W: n
'
/ T" U6 ~& l+ Q6 Y0 y) T4 G'##########################################################################) q3 O9 v6 s( O3 `1 G
'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody, U* B6 X* N- k
'can fix that please let me know... UMGEDV@AOL.COM# O" v g- I5 C5 Z) @
'The Error is not present in the native compiled version; so I think it got& [8 M( V( R) ]+ `& u
'something to do with the P-Code Calling Convention (strange though)...
# G+ k" I/ B0 N7 _- Q'##########################################################################
) _5 I# F( b2 w$ J'
% C* h% D1 T6 G5 M1 u'Sub Dummy serves to reserve some space to copy the machine instructions into.
& M- j! v5 J3 ^ q'
& r2 c$ G; t4 e1 j% l: C'! ^$ z) a T/ b4 ?3 Q
'Tested on Intel and AMD CPU's (uncompiled and compiled)
! C) q% |4 Z: r+ m' f'% i8 E# i) W& D, f- U& T
'2 _- f* H8 f T5 V& ^
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
! s' K' c# U5 D# m$ K6 t8 HPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
7 w& }! D: y$ r! v* Z( p' jPrivate x As Long% P* n; M; F& l) b
7 i0 K0 d- c+ h% w- l( x( ?8 H1 |
Public Function GetCpuName() As String
" u1 f" J* K i; m " u' {% `, |5 o! Y# l( n/ Q2 z
Dim MachineCode(0 To 35) As Byte
' i. i; _$ n5 R% c. E! s Dim VarAddr As Long6 u( q+ J( a3 {- i
Dim FunctAddr As Long
2 Q, L7 `& Q" R" b9 W Dim EAX As Long
7 S1 Y2 X ^& ` ]# o* V Dim CPUName(1 To 12) As Byte
# e! U- A* |& u
. A, g" Y% c4 F, u 'set up machine code
2 a1 e/ S Z" L9 C% K8 M) T- J
2 ]( ~2 m; P- ]4 [3 | MachineCode(0) = &H55 'push ebp6 n. u6 `7 m% z; }, |8 z
% r- X; p! B- B7 t2 q! U
MachineCode(1) = &H8B 'move ebp,esp# V+ H* B, L/ x0 w; @. H2 e
MachineCode(2) = &HEC% n" L& B: [+ z5 p
6 _( D2 \$ y/ z- B! d
MachineCode(3) = &H57 'push edi
B. B( \. c' q% a/ f( ?3 z ( e1 P0 [ V6 O* j4 i
MachineCode(4) = &H52 'push edx
5 Q' N; H! i1 b; |7 I8 g! Z9 p
! [& _) Q9 r" \. X1 c2 X9 {; J/ @ MachineCode(5) = &H51 'push ecx4 w5 r' u# I7 D8 s; q2 | u! M9 V1 X& [
. V& @! W7 b2 I* ?
MachineCode(6) = &H53 'push ebx0 k" v0 G5 k# A' ~5 h7 P0 G- G: T
4 k! W w* \ E/ o MachineCode(7) = &H8B 'move eax,dword ptr [ebp+8]1 N0 `6 F9 l. a9 A) |3 t
MachineCode(8) = &H45- d" x& ?3 W( S, \$ y# i! `
MachineCode(9) = &H8
+ q+ r& K2 i o5 |
0 W2 I P, z- y6 i MachineCode(10) = &HF 'cpuid
2 O r) u6 {+ i4 h4 {2 x u MachineCode(11) = &HA2* p3 ?0 z, C3 v! \/ f
+ m5 d6 P8 c% V8 l MachineCode(12) = &H8B 'mov edi,dword ptr [ebp+12]( X) p1 c: ? Y% m4 y
MachineCode(13) = &H7D
7 H6 |6 n8 v2 O$ ` MachineCode(14) = &HC
& K# L4 E% D) ]! H 2 O: E$ Y: ]( }
MachineCode(15) = &H89 'move dword ptr [edi],ebx3 R" n1 B- y' U. S/ Q" `
MachineCode(16) = &H1F% d M" H# k$ R
# r9 z0 s% k0 g
MachineCode(17) = &H8B 'mov edi,dword ptr [ebp+16]9 _5 r3 J3 ?5 j% ]3 S% M, }- T
MachineCode(18) = &H7D. g; F( Z* B& d8 _6 j
MachineCode(19) = &H10% P) Y0 {6 K: `; Z3 U3 C
) W2 y' o j: v# k) {' x- w MachineCode(20) = &H89 'move dword ptr [edi],ecx- A) E/ a( A( s7 P1 ^; q
MachineCode(21) = &HF
8 s5 ^$ T. P: }- I; I @ . }' L( J$ V9 @& h) b
MachineCode(22) = &H8B 'mov edi,dword ptr [ebp+20]
) H. E4 z& K* }1 S MachineCode(23) = &H7D
) H7 b( z. ], ]( s MachineCode(24) = &H14, L2 N% x8 R: e2 V2 W$ d
7 P( b7 m" ~) o1 Y5 V
MachineCode(25) = &H89 'move dword ptr [edi],edx
5 N3 r4 H; E# I+ _+ H MachineCode(26) = &H173 S/ F6 @5 s! \, d5 `
# P6 U6 c4 Q# h$ @# f- Z9 s: u MachineCode(27) = &H58 'pop ebx
# ~3 ~, l4 q; m0 b
) x; T/ `$ q( D. }+ {- j; h5 C7 [ MachineCode(28) = &H59 'pop ecx
, E M X7 |" w$ ], T6 o% F/ i4 `( U3 [2 o+ R$ G
MachineCode(29) = &H5A 'pop edx, H4 d* L2 s4 m" A# ^
; o+ i5 q& A* \! B' F$ S
MachineCode(30) = &H55 'pop edi7 A/ e* U6 B: E& p
$ g# B) ^! [- v9 c3 b3 F, [
MachineCode(31) = &HC9 'leave
' Y) }/ E, \3 _5 ~
- n1 P0 ?7 t. d: m8 V! \+ M7 p5 O+ c MachineCode(32) = &HC2 'ret 16 I tried everything from 0 to 24
3 T% x1 C2 H' }( j6 G MachineCode(33) = &H10 ' but all produce the stack error- p% t( X$ `2 y# {3 V
MachineCode(34) = &H0 w. e" V3 a& W) Z+ K
/ c7 ~& G1 f9 h6 \0 o. [
'tell cpuid what we want
1 C) @! z4 H H! @/ R2 \1 {7 M' ` EAX = 0) Q7 k, W" \7 n0 W9 L- I
( G! v; m- a3 ? 'get address of Machine Code
$ Z6 m0 g& E+ @5 t# n VarAddr = VarPtr(MachineCode(0))
$ c- K: c8 S! ]/ E9 `+ l- G
# I* W% e' y4 p( L+ d7 n" ] 'get address of Sub Dummy5 W$ u* }) i, M! h- M# s, \( R
FunctAddr = GetAddress(AddressOf Dummy)
0 ^0 m f5 [% Y* b# p( E) c: l- l0 G
' P, S2 }$ s i! B9 W5 n 'copy the Machine Code to where it can be called
' L4 r: y0 ?9 F D CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
( u/ h3 O+ w$ b% u + H: X8 h7 }' E5 Q6 s
'call it4 F' M6 G. z( P5 P) _+ {
On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why; W! o9 r @" h$ B" L3 j
CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))( p( t8 t" Z; D8 [& p. @0 T2 K
'Debug.Print Err; Err.Description6 R+ O; W" x& S: n
'MsgBox Err & Err.Description) @& q* z8 }8 s( B4 p# a; Q/ q, Y
On Error GoTo 0
( l7 _. `/ a9 U+ ]6 n+ g 6 f+ F- _) i" x3 [& V
GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName
" V: `; ~7 F5 g: h/ s
9 ?$ n* Q* Y& A7 J0 F# T$ WEnd Function
* M# _1 T) f2 p( ~
# q5 z) v! a2 mPrivate Function GetAddress(Address As Long) As Long% s! I, u# P S' e+ n* _) d7 H
* T' k# q- z+ O! J) p, |
GetAddress = Address% h* K. C2 Y( D
# s" z% d5 Y5 P3 h% V+ i6 W' w: MEnd Function! x3 Z% Q3 G; v& S
5 Y0 N# S) ]1 f' U3 w2 G- U0 k
Private Sub Dummy()
9 h% J5 A) e. e* b( j6 y( Q3 W& z1 q$ {- j
'the code below just reserves some space to copy the machine code into
! K# k7 r! N( S8 T 'it is never executed0 t9 y5 W3 x, B) ?3 L
0 j) Q' J) L% J: Z+ u0 d. u x = 0
7 \- S( f; L. K( b5 J1 O+ i x = 18 z; }5 S# a2 x
x = 24 f! @% `$ _2 x
x = 3. c z j7 H, z0 r2 |. g
x = 4: |2 C: o9 Q- A1 ]- v, |4 B
x = 5+ j' R$ O! k. N. T
x = 67 j* q0 u2 N) `+ t, m, P- P: r
x = 79 p; y2 [$ Z" n, L z" _
x = 8& ?2 T- |6 V; @6 H7 g$ u
x = 95 f1 Q3 ?2 H! _2 J( J
x = 10
/ X8 r* x" d" U4 S x = 0
! R. Z' a+ P! C1 |$ ^: V ` x = 1& t! |* q8 _" y) l, {7 j. X* F
x = 2/ k) ^3 t: K+ ?1 ]0 E M
x = 3
9 x. ~; ]' A% z( o x = 43 V# \, L" G6 C' j" L3 R
x = 5& e# m8 G$ {4 @9 o
x = 6) h E" ~9 j3 S, {
x = 7
) o* C" n' c8 n/ }, h$ S x = 8
8 a+ R9 f6 h6 z$ w o x = 9
# c- ]" p5 F3 P x = 10
0 k8 N; \# W- T' q9 K( Z! L- C ' S s7 }+ Z( j+ k1 M1 @6 n5 }0 v
End Sub
8 L2 V- j* P; Y3 z------------------------------end--------------------------------------
" v5 _9 v) W6 A% u( X. v. u
+ B; w/ q( E7 I% H6 N9 u
$ \" L6 K7 C# K- n5 i
$ Z4 g2 j) b* X0 |1 P& l+ }. T% r |
|