a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 149|回复: 1

[Visual Basic] VB如何动态调用外部函数的方法(2)

[复制链接]
发表于 2012-7-31 22:10:12 | 显示全部楼层 |阅读模式
′执行Assembly Code部分   RunDll32 = CallWindowProc(GetCodeStart(hProc, Params), 0, 1, 2, 3)
4 w1 H8 [$ S, D1 n  FreeLibrary hModule ′释放空间
2 M6 _; _+ @5 G9 v  End Function( w0 v6 p7 R* S6 e# U
  Private Function GetCodeStart(ByVal lngProc As Long, ByVal arrParams As Variant) As Long3 d4 j6 |3 a% P8 f: V- v
  ′---以下为Assembly部分--8 Y0 i) @0 L9 L6 T7 v" V8 L+ L
  ′作用:将函数的参数压入堆栈
. K0 c7 {7 j2 G6 G& M  Dim lngIndex As Long, lngCodeStart As Long
# S! C: }1 Y& ~7 L8 c! P: T/ J( f5 Q  ′程序起始位址必须是16的倍数- ~1 X# H3 e2 A" |
  ′VarPtr函数是用来取得变量的地址1 b& H% }  [6 ^# }- b
  lngCodeStart = (VarPtr(m_OpCode(0)) Or &HF) + 13 ?. R& T' B9 M) ^: j, }% ^
  m_opIndex = lngCodeStart - VarPtr(m_OpCode(0)) ′程序开始的元素的位置" N2 G) ], v0 L+ [) [
  ′前面部分以中断点添满
+ i& g; h0 ~$ s+ q: K8 ?+ S6 W# v  For lngIndex = 0 To m_opIndex - 1
, m: ^3 H9 }+ B  o: h* e9 ^  m_OpCode(lngIndex) = &HCC ′int 35 L3 i1 r9 k+ |& K% v
  Next lngIndex7 K* c# P: O' [0 W$ ~" \, R
  ′--------以下开始放入所需的程序----------/ b3 H0 @/ Z6 T: a) P
  ′将参数push到堆栈$ r7 [5 ^9 ~. Z' A# S. D
  ′由于是STDCall CALL 参数由最后一个开始放到堆栈) V$ T) c$ L0 w) ~! V# x
  For lngIndex = UBound(arrParams) To 0 Step -1
; e+ m* U8 x$ [0 P  AddByteToCode &H68 ′push的机器码为H686 |5 U2 ^) H/ n$ u
  AddLongToCode CLng(arrParams(lngIndex)) ′参数地址/ ?# x6 O: M0 y6 `6 T
  Next lngIndex" S" o) t' `$ ^7 R% N
  ′call hProc
. n. I- }  x  L5 o* x1 H4 G  AddByteToCode &HE8 ′call的机器码为HE82 l/ S  I) B) H
  AddLongToCode lngProc - VarPtr(m_OpCode(m_opIndex)) - 4 ′函数地址 用call的定址3 G  i. C( Z* S7 l
  ′-----------结束所需的程序--------------
回复

使用道具 举报

 楼主| 发表于 2012-7-31 22:10:13 | 显示全部楼层

VB如何动态调用外部函数的方法(2)

</p>  ′返回呼叫函數
, R0 g7 r# i( a- d7 D! m9 |  [! N  AddByteToCode &HC2 ′ret 10h
3 W, ^. s9 y4 P1 n' g( X  I  AddByteToCode &H10
) Z) Y4 l6 M- C  AddByteToCode &H06 @" F2 I4 `: i2 p  U3 l
  GetCodeStart = lngCodeStart
8 z% _% [$ S+ A2 E5 y0 D& T  End Function
2 j3 R' R2 Z3 |7 e; e  Private Sub AddLongToCode(lData As Long)
6 q, V' {  g( J, Z  ′将Long类型的参数写到m_OpCode中+ Y! _. s& i4 |' J& `
  CopyMemory m_OpCode(m_opIndex), lData, 4/ F# v* W0 r/ q2 }# V% c
  m_opIndex = m_opIndex + 48 f5 u  C8 P- a4 u) x$ V4 x
  End Sub
3 u9 x4 Y, v; h- o' h  Private Sub AddIntToCode(iData As Byte)
& B, e. n0 [  P- a( y; Q  ′将Integer类型的参数写道m_OpCode中2 l- I& A8 L5 y; A8 r! A. H; i
  CopyMemory m_OpCode(m_opIndex), iData, 2. i/ A2 S+ b* c3 U1 V8 ]& C0 N* v
  m_opIndex = m_opIndex + 2! A/ J8 o& k- }8 j6 k4 U
  End Sub
3 a# G! v, r/ L$ |  Private Sub AddByteToCode(bData As Byte)5 z2 A! C' F( N& Y% d: z/ Q' l0 B
  ′将Byte类型的参数写道m_OpCode中
9 E4 G9 _0 l- C+ |1 D  m_OpCode(m_opIndex) = bData
$ g! r& f. ^' q/ H. d  m_opIndex = m_opIndex + 1: p' _  x* n4 d5 P% ^8 W( z. k
  End Sub
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-4 05:34 , Processed in 0.201344 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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