下沙论坛

 找回密码
 注册论坛(EC通行证)

QQ登录

QQ登录

下沙大学生网QQ群8(千人群)
群号:6490324 ,验证:下沙大学生网。
用手机发布本地信息严禁群发,各种宣传贴请发表在下沙信息版块有问必答,欢迎提问 提升会员等级,助你宣传
新会员必读 大学生的论坛下沙新生必读下沙币获得方法及使用
查看: 3562|回复: 3
打印 上一主题 下一主题

请问VB调用我汇编写的函数

[复制链接]

该用户从未签到

跳转到指定楼层
1
发表于 2004-2-7 15:59:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我写了几个函数,VB调用后会出错,我自己汇编写的则不会
4 r+ H/ s  _. t! R我是恢复了ESI,EDI寄存器(在WinAsm32附带的一个帮助中说要恢复,EA/B/C/DX则没有要求),老是出错(全部恢复当然就没错了).不知道他还有什么要求,知道的告诉小弟我啊!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 顶 踩

该用户从未签到

2
发表于 2004-2-7 19:21:00 | 只看该作者
你用DEBUG程序跟踪一下不就知道原因了么/ f3 e* }. f, f: Z! X8 C5 E6 W+ z  t
既然全部恢复不出错了,就用pusha/popa好了,省事。
; K+ J1 k, t1 p" ^! T4 M" X7 {对于VC程序,ECX常用于函数间传递this指针的,VB就不知道了

该用户从未签到

3
 楼主| 发表于 2004-2-8 19:42:00 | 只看该作者
谢谢呀!

该用户从未签到

4
发表于 2004-3-19 17:50:00 | 只看该作者
转载:$ u; i; I/ W- c6 P/ `2 T
来教你如何在vb里嵌入汇编!
3 g& k2 Q' N; p: Q0 T# F* n) ]作者: wl3000wl 
' m, M% \) o4 b/ p5 r0 _2 p本贴绝对值得你珍藏.2 T0 o! ?! Y9 ~6 k' m% A
下面的例子完全用VB进行ASM编程的示例,本例获得CPU ID.8 a7 T7 _2 e/ V: A3 g1 L
工程文件分为一个form1.frm 和一个模块module1.bas
' p; w$ H: A7 @" d5 `0 w8 {! X----------------------form1.frm的源文件---------------------8 x- N( D  a9 S( }# e$ A4 P
VERSION 5.00
, \2 M3 O0 H4 Z! vBegin VB.Form Form1
* p6 I  L7 b: o" P   Caption         =   "Form1"1 b* k" l  ?) ^7 p8 z
   ClientHeight    =   19651 {& C8 j# R7 m3 X# L
   ClientLeft      =   60
1 f; n* D# q9 G3 y   ClientTop       =   345' w6 \1 ^& x8 ^" f! ~5 n' \4 p
   ClientWidth     =   3105
: c% S" B( S/ n7 l1 }' u' n   LinkTopic       =   "Form1"
  n/ e  P; a2 W1 c. q: A   ScaleHeight     =   1965  }% ^* R9 d* ~$ [( x  K# t
   ScaleWidth      =   3105
' Q/ F* C5 M& b5 w   StartUpPosition =   2  'Bildschirmmitte
8 G& R4 S7 W" M; f6 a* q" j4 Y2 r   Begin VB.CommandButton Command1
3 r' c! j" e- p$ ]. A# M# S( G      Caption         =   "Get CPU Name"8 N0 g  F$ b) J/ Q& H
      Height          =   495
0 m7 ]1 d( m& y; E      Left            =   840
9 c2 N* s0 \; z0 r      TabIndex        =   0
5 Q* n* p; X) E* [& e      Top             =   315' P" Y8 c4 N0 K% T
      Width           =   1425
4 M9 i# I- j' s* o+ n; ?; y   End
1 n0 C- e6 {) k( J- X   Begin VB.Label Label2
5 g7 E, T) X; f0 |9 ]      Alignment       =   2  'Zentriert
/ b3 T$ a7 _" K% n+ A1 |2 _      AutoSize        =   -1  'True
; J) J, {* B! n4 x      BeginProperty Font / _  v, M# g$ e0 f$ C2 C9 I4 H8 Y
         Name            =   "MS Sans Serif"
2 F3 s+ T0 f& s; ~         Size            =   9.75  H2 e- r: U3 J4 S, n! R
         Charset         =   0% }. g2 O/ |% I) b" x- e
         Weight          =   400
% ?3 p$ K, @. M$ k& Z1 \( p5 f         Underline       =   0   'False4 j# _' |$ S& u. G0 z
         Italic          =   0   'False- I3 X8 i- `$ ^9 r+ ~# @; @, ~4 d5 n
         Strikethrough   =   0   'False$ b4 X3 o. T( v( d0 i: B
      EndProperty' W' K0 S1 J4 Z( ^: W" x
      Height          =   240% R" Y" t/ {! j2 n- u9 `8 ]  [3 O2 u3 n
      Left            =   1515
+ D' g3 y# o' W+ h3 p4 U! J! ]: J      TabIndex        =   2
( a- ^3 q7 K- q; x4 h      Top             =   1065
6 w& C! g1 o2 U# j# a1 J: h      Width           =   60
# K. E9 ^  C0 b% x( y   End, B6 W" j# A; K+ o) U( I7 j8 ^
   Begin VB.Label Label1
: o* }: @" M+ n+ @& Q3 F8 {; X      Alignment       =   2  'Zentriert
8 M: d9 F) [3 G8 V1 X: X      AutoSize        =   -1  'True
1 m1 P1 p- y/ {2 T7 ]9 H0 Y      BeginProperty Font
( ?! R5 m& d+ ~6 T  U2 j) d! o         Name            =   "Arial"6 U, u! x* I+ ?) H1 W
         Size            =   12
2 E$ z% A6 f' L  W- r) ^         Charset         =   0
0 [9 z0 d3 W6 s+ X( v         Weight          =   7000 G6 G; N# V9 y: i+ p5 i2 W& ?1 B+ Y, Q. N
         Underline       =   0   'False
# H" |7 N. d7 x/ ~5 X4 R% z         Italic          =   0   'False
' t* ]& ?% L: {7 |% `: f" N3 z' G         Strikethrough   =   0   'False
4 m( o' {& ~1 ?! ?      EndProperty0 b2 Z8 ^7 B: r" u* C' f
      Height          =   2851 a) ~9 O+ G+ C( w; @4 C
      Left            =   1515/ j3 L1 x; p4 c9 J) p
      TabIndex        =   16 d5 \1 _) p+ V2 \- {2 |
      Top             =   1350
' `7 l% y1 `- _$ Q1 V( [, N      Width           =   75
0 q6 ]" h, L: ~$ i  G5 H" c, m8 |   End! u5 M* o. S, {+ X- }' x, s! q
End) }  P$ A# b# t: G6 d" s
Attribute VB_Name = "Form1"
: ~( x  @" o. d# a1 NAttribute VB_GlobalNameSpace = False
/ ]& ~3 E/ F$ T7 c4 }Attribute VB_Creatable = False4 `- v! r& G# O1 T- r( }! g! W
Attribute VB_PredeclaredId = True+ i& o5 u  e/ z/ p' r1 B
Attribute VB_Exposed = False3 Y( x6 K# e2 |/ C
Option Explicit
- h+ d( }! `) g
6 c6 O8 N9 i+ @5 wPrivate Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
" ?. Z0 {8 j2 s9 j% U) A; @0 Z" ~  B8 B+ n
    Label1 = """ _6 n$ \5 B7 V3 C; v( N
    Label2 = ""
/ t( J2 y# b8 b( Z2 q
7 N. X3 T8 C3 L% ~End Sub
  N$ N+ e9 _4 k) T$ r" u4 i# r# U; v1 m1 [; r0 q! L
Private Sub Command1_Click()5 X" X, M+ }! f( y9 m: D3 }* k; Z
    4 t1 f2 G* }) E3 y( m
    Label1 = GetCpuName() & " CPU"- u3 A. `, D3 ~# I5 g0 t8 M
    Label2 = "You have a" & IIf(InStr("AEIOU", Left$(Label1, 1)), "n", "")6 s0 P; a0 a! w: k3 r9 H. H, P
' F4 H5 B. \0 I! A1 U1 A
End Sub
) K4 H) g4 i$ b1 b9 }, O------------------------------end---------------------------------; w! P# ]$ B$ `" x. [7 l

- b, r5 a6 e5 t* [- Y0 Z# {) w* A6 W! q

& ]% D+ S4 R- j/ g8 a* I& ?7 S
) j5 {8 M. l1 S; z1 Y% g( T6 p1 d
下面是modu1e.bas的源代码' w# K9 m5 o) r7 J

! m/ F$ a5 t* I/ u1 |----------------------module1.bas的源文件--------------------------
+ W' p9 F! A) v7 `Option Explicit4 m  {  [/ J' ]( F; s: P! k
'3 P8 Q, n  t& c: j" ]) h
'This shows how to incorporate machine code into VB; g; Y2 F- ]: j6 V. T, w7 P
'''''''''''''''''''''''''''''''''''''''''''''''''''  [# L* K" l! Q0 q6 S) {
'The example fills the array with a few machine instructions and then copies
' H+ E9 X8 P/ Q- S'them to a procedure address. The modified procedure is then called thru
: \# o/ h# K8 x# F2 ^/ R' g3 d'CallWindowProc. The result of this specific machine code is your CPU Vendor Name.
/ v9 K1 K* P, r- A) o+ _') z, f/ P- O0 I( z
'##########################################################################
" n$ p$ l  m" y/ y/ u'Apparently it gets a Stack Pointer Error, but I don't know why; if anybody2 ~; H7 }: ]6 r5 T1 b6 l
'can fix that please let me know...                          UMGEDV@AOL.COM
$ a* k. ^) {$ Y! X3 R, s8 F'The Error is not present in the native compiled version; so I think it got' ?; a; O& M8 f9 t
'something to do with the P-Code Calling Convention (strange though)...; x( Q. ?1 }4 u8 U, l/ S
'##########################################################################8 @. G5 G- X, q- \$ B
'4 b- Z) s1 K9 f5 d
'Sub Dummy serves to reserve some space to copy the machine instructions into./ K0 m. Q; q9 x+ T5 D! c% N
'
4 i4 F% t& V( i3 ]) g! P! l% D'9 e+ H0 T- j9 k  w5 f" q
'Tested on Intel and AMD CPU's (uncompiled and compiled)
$ P: [8 r" \; j. G'
) z3 {4 z4 Q/ C6 A, T; O8 Z. a2 ~'
2 i# F6 I1 L' U6 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
/ k; c4 N0 e( ?9 ?2 pPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
2 _  b* N" m) tPrivate x As Long, D5 H. e' A  k9 S
1 f3 y- B1 e" y0 l' A. ]7 b" A
Public Function GetCpuName() As String
5 j8 r4 H6 Y. D  g; W( M  
) l' ?0 B+ X5 E4 D. t  Dim MachineCode(0 To 35)  As Byte$ ?! X- m5 V& z- n# q9 f" G0 B
  Dim VarAddr               As Long
4 }, B  h! g, A$ V  Dim FunctAddr             As Long
. k3 L/ c- k! ?9 ]  Dim EAX                   As Long
! b8 T, c2 |7 v2 G9 q5 {, P  Dim CPUName(1 To 12)      As Byte. D; H1 p5 \4 I8 c
  
+ ^; Z/ ~* [3 S! n) D# Q  'set up machine code
: t1 S( E" x! J/ b! ~* X0 }! z( }/ g    - T* t. T0 T% u
    MachineCode(0) = &H55    'push ebp6 G+ g5 O5 W* \" [* U
   
1 r2 k( G0 z5 [/ D: s' |( s    MachineCode(1) = &H8B    'move ebp,esp& C( I; Y. u% F) G2 j  C3 q
    MachineCode(2) = &HEC
% }( o7 T$ Z# x0 _' ]    & u1 N$ `% c) U% B: r4 P* [
    MachineCode(3) = &H57    'push edi1 x: [$ v8 ?2 m4 f- T
   
; f% B5 e6 f* s    MachineCode(4) = &H52    'push edx
+ \+ y; M* Z0 s3 A    8 }3 I* ~: t7 Y6 c
    MachineCode(5) = &H51    'push ecx, C2 [6 f" ]: V. A/ L, ~: R
    ! s8 d" \/ L! O. m
    MachineCode(6) = &H53    'push ebx! A+ \# k/ d  V3 _
    ! Y, E% Y( b3 u( P4 j& @8 z2 R
    MachineCode(7) = &H8B    'move eax,dword ptr [ebp+8]" b0 g+ I8 [$ z, S
    MachineCode(8) = &H45
% z' x) Z) f! m    MachineCode(9) = &H83 @6 Y( n( w0 R0 p0 D1 h, _1 z; N4 V
   
, I& n, N& \& V9 R    MachineCode(10) = &HF    'cpuid# u  L. z2 b0 f( N3 B2 G. U& V5 f4 r1 e
    MachineCode(11) = &HA2( A3 k; Y; N6 s. r& B
    3 C6 Y, `- M* ^3 X
    MachineCode(12) = &H8B   'mov edi,dword ptr [ebp+12]5 T1 p3 O0 I5 i6 T
    MachineCode(13) = &H7D
! h! G) K+ o: I2 S9 w    MachineCode(14) = &HC
3 X: M. G& z" [1 d# ?3 x+ F; @2 @    9 b% a  h# ?$ K+ e& x& s
    MachineCode(15) = &H89   'move dword ptr [edi],ebx
  y: [: v  K" z4 b. t0 G2 [    MachineCode(16) = &H1F
. t+ P5 Q& I4 f! j7 z    + j1 `% K2 h- ~
    MachineCode(17) = &H8B   'mov edi,dword ptr [ebp+16]
0 e) k4 y6 B* y, t  `0 u  {    MachineCode(18) = &H7D) t8 Y: G1 |' o  D2 O
    MachineCode(19) = &H10
0 s, L  ~) }7 Q; J( Z  I   
2 _# j) E" Y  q; q) i    MachineCode(20) = &H89   'move dword ptr [edi],ecx6 u% g7 _: O, V! ]8 H! S) i
    MachineCode(21) = &HF
0 L, r! \. w7 I8 I   
+ d% X/ @' t9 F" {4 ]; ?/ S3 B    MachineCode(22) = &H8B   'mov edi,dword ptr [ebp+20]
# |) @, _6 O  g( r1 T) B  d    MachineCode(23) = &H7D) r6 j4 B& M% Q
    MachineCode(24) = &H146 Z5 @; X: V( k# ^8 n0 ?6 G0 b
    / ]! {0 @* Q7 q# F
    MachineCode(25) = &H89   'move dword ptr [edi],edx
7 z6 h7 G. S+ [    MachineCode(26) = &H17; K; }3 ]3 a- f3 s2 R$ i& k+ P
    % I" w3 U9 R7 S/ N+ u0 Z
    MachineCode(27) = &H58   'pop ebx
6 g& R+ j- L9 j0 C. Q
% E3 p/ E. K% Z" a6 B6 q: x  ?    MachineCode(28) = &H59   'pop ecx6 o) V& m- u7 ?7 W  z1 ]
1 y) H  x  O+ ^
    MachineCode(29) = &H5A   'pop edx
& }0 c6 Y$ M- S' Y8 Q+ V4 \; }4 q, l1 n
    MachineCode(30) = &H55   'pop edi
8 f  K  L! S8 c" k. n4 q   
3 Z) _, g, m2 u    MachineCode(31) = &HC9   'leave
9 i! O; p1 x3 A+ B( z* _& _+ [/ t! N( x, R5 i4 c; ]
    MachineCode(32) = &HC2   'ret 16     I tried everything from 0 to 24
5 r: r8 I9 O" w& B- E6 N/ g    MachineCode(33) = &H10   '           but all produce the stack error
9 j1 R) l: d6 U% ~    MachineCode(34) = &H02 O; t/ j, _; y; J; v- R1 f: \
    4 b9 u/ r7 F, K" |
    'tell cpuid what we want
7 e1 W8 b) D. A/ y, i+ L    EAX = 0. q+ l' P  s: a. I* L) z
    8 ]- W5 W/ {  w+ l: Z" e& Z, Q
    'get address of Machine Code5 m5 g, o- p6 t  s9 ]- s
    VarAddr = VarPtr(MachineCode(0))
$ f& \2 H; |$ M( Q3 f' ~1 l8 P; C8 z    : }  B5 I: R+ d$ i8 O  `5 J; a
    'get address of Sub Dummy' t: H& G( x" S- a
    FunctAddr = GetAddress(AddressOf Dummy)
# O- P8 ]; J9 ^* \1 G! R    * N/ x% R6 h6 m) m% A6 l0 D5 {
    'copy the Machine Code to where it can be called
$ V  f$ @1 ?8 e' F8 p    CopyMemory ByVal FunctAddr, ByVal VarAddr, 35 '35 bytes machine code
1 L3 y$ W7 k2 {0 m+ Z* H4 U- [# a   
$ w5 w! N& @8 W0 A  v- z! b    'call it
" R: ?: R) F9 p6 S, @( G; N9 J    On Error Resume Next 'apparently it gets a stack pointer error when in P-Code but i dont know why
: E. _% c" m5 ]      CallWindowProc FunctAddr, EAX, VarPtr(CPUName(1)), VarPtr(CPUName(9)), VarPtr(CPUName(5))) a8 i" i' L& ?- V; g
      'Debug.Print Err; Err.Description
/ K) G( r% p" g2 L      'MsgBox Err & Err.Description' Z! s4 ^+ u  ?* f' z  [% k
    On Error GoTo 0) ?9 f- G1 i. p
   
  n# F; ]# Y# y9 p- t% f    GetCpuName = StrConv(CPUName(), vbUnicode) 'UnicodeName* g: w% H( @* d1 j/ D7 E* j
    2 G: e9 h8 r; }# M+ |- g
End Function7 Q. X4 C* N% v/ v, f! f

, r9 ]: q+ e# w# T$ p  yPrivate Function GetAddress(Address As Long) As Long
* i0 T6 A) p2 h& t" z
! \- O- _+ O! n7 g    GetAddress = Address
/ G! C) K& N3 O" U0 |1 v' I
7 Z/ M/ X$ v+ k0 j% z9 s6 T' Q4 ~End Function6 ]' }  ~0 j. j* r% P8 r: \. |
) K# i& q$ G7 L
Private Sub Dummy()
# h0 p$ `# [7 i, m
( ^7 v) k7 u. d0 X8 ]  'the code below just reserves some space to copy the machine code into
" T- ~, r  r2 `) ^7 k  'it is never executed
/ v" p0 S7 L6 {& g9 H& w/ D* t7 R$ [9 p, C3 v+ B
    x = 0, B# L2 a9 Y5 }
    x = 1
" v7 ~9 D+ ~3 r3 D7 Y; n/ S    x = 2+ X( i) V6 G7 v* U( Z- z# b
    x = 3
8 p2 b7 k% ]  B    x = 4
  `3 g0 F6 j/ n. B& F# j    x = 5
2 L# P! T3 a2 g; a0 h. \* j    x = 6
+ G# H/ x7 B: G    x = 77 f6 D8 t! m7 C" w& t! c" ^
    x = 8
: n2 D7 s0 W, k; v& m1 U    x = 9
) Y! r; b$ U) d    x = 10
; \* r' t1 ?! Y1 v    x = 0
' a7 R# m8 R! z: i. V    x = 1+ _: b6 X: G# [* f$ Q% G
    x = 23 }8 @7 w6 s! ^# b, q4 p0 a! N; B
    x = 3
- {6 x; h  V4 D7 u% U    x = 4
. u: ?# j' h8 a1 H& M    x = 5
  R2 I! b; U! }- X) I6 z$ w$ |  j: t    x = 61 Q1 b( u( P( l( j
    x = 7# F) S& |" k& ~* L1 U% z# Z
    x = 89 P& e: U+ Q0 f3 R. o$ N  {
    x = 9
% s/ k# E4 A0 A) Q+ P' Q2 B  L    x = 10* e& b6 y7 \5 }
   
/ Y" d* @7 z6 O2 N  q0 @' K+ x1 }End Sub
' J. c! R/ e* ^8 q# d/ I- x------------------------------end--------------------------------------2 i& v3 k' @' G& j

8 a+ I- i: k1 V) O2 M! H; D- t
' ~3 }- M) b- N1 x; v. K
; o6 Z/ r$ g+ S; n, [' L$ U

本版积分规则

关闭

下沙大学生网推荐上一条 /1 下一条

快速回复 返回顶部 返回列表