转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
使用 Visual Basic 通过 32 位 地址访问内存(中英对照)         ★★★★

使用 Visual Basic 通过 32 位 地址访问内存(中英对照)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2874 更新时间:2009/4/23 16:38:16
ReadMem = DB
         Case DefineWord
             Dim DW As Integer
             CopyMemory DW, ByVal MemPointer, 2
             ReadMem = DW
         Case DefineDoubleWord
             Dim DD As Long
             CopyMemory DD, ByVal MemPointer, 4
             ReadMem = DD
         Case DefineQuadWord
             Dim DX As Double
             CopyMemory DX, ByVal MemPointer, 8
             ReadMem = DX
     End Select
 End Function

 '''' =============================================================================
 '''' 允许直接写 MemPointer 指向的内存
 '''' 用和 Asm 一样的字节数定义 (DB, DW, DD, DX)
 '''' =============================================================================
 Sub WriteMem(ByVal MemPointer As Long, _
              SizeInBytes As e_BinaryData, _
              ByVal DataToWrite)
     CopyMemory ByVal MemPointer, VarPtr(DataToWrite), SizeInBytes
 End Sub

------------结束剪切---------------------------------------------------------------


用例:

通过内存为变量赋值:

 Dim ptrVariable As Long
 Dim xCounter As Long

 ptrVariable = VarPtr(ptrVariable)
 WriteMem ptrVariable, DefineWord, &HFFFF
 '''' 与 ptrVariable = &HFFFF 等价


读内存的内容,使用:

 ptrVariable = ReadMem(ptrVariable, DefineWord)


  现在我们能够获得指针并访问它们了。但是如果你一步步跟着以上步骤看下来,你可能奇怪一条原本的 Visual Basic 赋值操作比这里介绍的直接内存赋值操作快得多。然而本文旨在指出可以使用 Visual Basic 访问内存,而这一点的主要意义不仅在于读取和分析变量,接下来,你可以通过获得内存地址简单地处理运行的 DLL。同时利用 modMemory.bas 和 PE (Portable Executable) 文件格式的知识,你可以分析 DLL 主体,看看它们是如何处理的。最好的是,可以获取它所有输出函数的列表;差点忘记,可以把它们 spy 出来或者干脆获取函数体的副本进行反汇编,比低级语言访问更多的内容,这也是 C 语言被称为工业标准的原因;现在你可以书写跟 C 表现相同的 Visual Basic 程序,祝你好运!


- Chris Vega [gwapo@models.com]

 

Accessing Memory by 32-bit Addresing in Windows using Visual Basic

July 6, 2001

Manila, Philippines

By: Chris Vega [gwapo@models.com]

When we talk about *real* Pointer and Memory Addressing, most of us thinks of Visual Basic limitations, ie, VB cannot access memory because VB has no pointer datatype for a variable declarations. This confusion grow even larger when a scenarios needed one *address* of a variable instead of its value, ie, from where in memory was that variable located into a virtual space of currently running process or a process or dynamic library.

Yes, there is actually *no* pointer variable for VB, but have you ever tried to turn a regular VB Datatype into a Pointer? do you think its not possible? well, think again, cause in Visual Basic (starting from release version 5), a serries of handy funtions is presented by Microsoft to turn this regular variables of yours into a pointer, these are:

 1] VarPtr - Returns the Address of a Variable or Array Element
   
           StrPtr - Returns the Address of String

  Variables in Visual Basic, except Strings are located into its
  Memory Location, you can get the Address of this variable by
  calling VarPtr Function. Strings however are stored as BSTR''''s,
  a pointer to a "pointer on array of characters", where you need
  StrPtr to have the address of "pointer to the array of characters"
  instead an address to BSTR if you used VarPtr in String.

  ex.
   Dim ptrMyPointer As Long
   Dim intMyInteger As Integer
   Dim strMyString As String * 25

   '''' A call

   ptrMyPointer = VarPtr(intMyInteger)

   '''' gives ptrMyPointer a 32-bit Address of the Variable
   '''' intMyInteger in Memory

   strMyString = "Address of Variable : " & Hex(ptrMyPointer)

   MsgBox strMyString

   '''' Next, a call

   ptrMyPointer = StrPtr(strMyString)

   '''' gives the Address of the First Element of the Array of
   '''' Character, ie, First letter of the String.


 2] VarPtrArray - Returns the Address of an Array of Variables
    VarPtrStringArray - Returns the Address of an Array of Strings

  Arrays in Visual Basic are store in SAFEARRAYs, and you need to
  use the function VarPtrArray to get the address of this array, but
  before you can use the function, you need to manually declare the
  function from msvbvm50.dll to your VB Application.

  ex.

   '''' for VB 5
   '''' ========
   Declare Function VarPtrArray _
                                         Lib "msvbvm50.dll" Alias "VarPtr" _
                    (Var() as Any) As Long

   '''' for VB 6
   '''' ========
   Declare Function VarPtrArray _
                                         Lib "msvbvm60.dll" Alias "VarPtr" _
                    (Var() as Any) As Long

   '''' The Call

   Dim lngSafeArrayAddress As Long
   Dim lngArrayOfLongs(6) As Long
   Dim i As Long

   Randomize
   For i = 0 to 6
    lngArrayOfLongs = Int(Rnd * &HFFFF)
   Next

   lngSafeArrayAddress = VarPtrArray(lngArrayOfLongs())

   '''' Returns the Safe Address of an Array lngArrayOfLongs, you
   '''' can simply use ''''em for *fast* sorting or many more!

  VarPtrStringArray however are more difficult to incorporate into
  you application since you need to create a TypeLibrary and manually
  refference the Library into VB Application. 

  To make a Type Library, you need a MIDL compiler, a CommandLine tool
  that compiles *.odl file into a Type Library,

  For VB5 Create a Text File and Save it to VB5StrPtr.odl with content:

  -------------Cut here--------------------------------------------------
  #define RTCALL _stdcall
  [
  uuid(6E814F00-7439-11D2-98D2-00C04FAD90E7),
  lcid (0), version(5.0), helpstring("VarPtrStringArray Support for VB5")
  ]
  library PtrLib
  {
  importlib ("stdole2.tlb");
  [dllname("msvbvm50.dll")]
  module ArrayPtr
     {
     [entry("VarPtr")]
     long RTCALL VarPtrStringArray([in] SAFEARRAY (BSTR) *Ptr);
     }
  }
  ----------End Cut here-------------------------------------------------

  And compile it with:
   MIDL /t VB5StrPtr.odl


  For VB6 Create a Text File and Save it to VB6StrPtr.odl with content:

  -------------Cut here--------------------------------------------------
  #define RTCALL _stdcall
  [
  uuid(C6799410-4431-11d2-A7F1-00A0C91110C3),
  lcid (0), version(6.0), helpstring("VarPtrStringArray Support for VB6")
  ]
  library PtrLib
  {
  importlib ("stdole2.tlb");
  [dllname("msvbvm60.dll")]
  module ArrayPtr
     {
     [entry("VarPtr")]
     long RTCALL VarPtrStringArray([in] SAFEARRAY (BSTR) *Ptr);
     }
  }
  ----------End Cut here-------------------------------------------------

  And compile it with:
   MIDL /t VB6StrPtr.odl


  Now, you have the Type Library, and Referrenced the Library to your VB
  Application, you can get the Array of Strings in this way:

  Dim MyArrayOfStrings(3) As String
  Dim AddressOfArray As Long
  MyArrayOfStrings(0)="Chris"
  MyArrayOfStrings(1)="Vega"
  MyArrayOfStrings(2)="gwapo@models.com"

  '''' A call
  Address

上一页  [1] [2] [3] [4]  下一页


[VB.NET程序]通过短信猫发送短信  [C语言系列]使用C#实现ADSL自动拨号
[Web开发]狂人采集器规则使用详解  [电脑技术]windows7快捷键使用大全
[办公软件]PowerPoint模板使用经验之谈  [办公软件]如何在PowerPoint中使用(插入)Media Player控件播…
[办公软件]如何在PowerPoint中使用(插入、创建)书签及书签的…  [办公软件]如何在PowerPoint中插入(使用)条形码
[办公软件]PowerPoint通过对象播放视频电影  [办公软件]如何通过网页打开PPT
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台