<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
如何在Visual Basic 中取得变量的内存地址(Address of Variables)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> |
本文适用于
- Microsoft Visual Basic Learning Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
- Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0
摘要
很少有vb程序员需要获得一个变量的低级信息,例如内存地址。然而,一些API函数需要这种
信息,本文描述的方法可以帮助VB程序员取得这些信息
VarPtr - Returns the address of a variable.返回变量的地址
VarPtrArray - Returns the address of an array.返回数组地址
StrPtr - Returns the address of the UNICODE string buffer.返回UNICODE字符串缓冲区地址
VarPtrStringArray - Returns the address of an array of strings.
返回字符串数组地址
ObjPtr - Returns the pointer to the interface referenced by an object variable. 返回一个对象的接口引用指针
正文
VarPtr
这个函数用来取得一个变量或一个数组的地址,传递一个变量名或者数组然后返回地址。但是,你要明白动态数组可能会被vb重新分配内存,因此你必须小心当你用VarPtr取得数组的地址的时候。
Dim lngVariableAddress as Long
Dim dblMyVariable as Double
lngVariableAddress = VarPtr(dblMyVariable)
Dim lngElementAddress as Long
Dim lngArrayOfLongs(9) as Long
' following will get address of 4th element
lngElementAddress = VarPtr(lngArrayOfLongs(3))
局限:这个函数不能用于取得数组的地址
VarPtrArray
当数组在Visual Basic中存为SAFEARRAYs结构时,要取得SAFEARRAYs结构的地址,你要使用VarPtrArray 函数,下面分别是vb5、vb6的定义:
Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" _
(Var() as Any) As Long
Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" _
(Var() as Any) As Long
Dim lngSafeArrayAddress as Long
Dim lngArrayOfLongs(9) as Long
lngSafeArrayAddress = VarPtrArray(lngArrayOfLongs())
局限:VarPtrArray函数不能用于取得一个字符串数组的地址,因为vb会对字符串进行UNICODE/ANSI转换,如果对字符串数组使用VarPtrArray,将会得到一个临时ANSI数组拷贝的地址。
StrPtr
取得字符串第一个字符的地址
Dim lngCharAddress as Long
Dim strMyVariable as String
strMyVariable = "Some String"
lngCharAddress = StrPtr(strMyVariable)
当你在API调用中需要传递一个UNICODE字符串指针时可以使用这个函数
VarPtrStringArray
VarPtrStringArray用于取得字符串数组的地址。为了避免VB 进行UNICODE/ANSI转换,
它的声明必须在类型库中定义
你也可以使用midl编译器将下面的.odl文件编译成你自己的类型库 :)
如果是
Visual Basic 5.0,建立一个文本文件名为VB6ptrlib.odl,内容如下:
#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);
}
}
如果是
Visual Basic 5.0,建立一个文本文件名为VB5ptrlib.odl,内容如下:
#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);
}
}
在命令行使用MIDL编译器编译.ODL文件建立一个VB5或VB6的类型库(.TLB)
MIDL /t VB6ptrlib.odl
MIDL /t VB5ptrlib.odl
为了使用VarPtrStringArray你需要引用这个类型库
例子:
Dim MyArrayOfStrings(2) As String
Dim AddressOfArray As Long
MyArrayOfStrings(0)="AAA"
MyArrayOfStrings(1)="BBB"
AddressOfArray = VarPtrStringArray ( MyArrayOfStrings() )
ObjPtr
ObjPtr返回一个object变量的地址
objCollection.Add MyObj1, CStr(ObjPtr(MyObj1))
...
objCollection.Remove CStr(ObjPtr(MyObj1))
相关推荐
Visual Basic .NET is released shortly after the tenth anniversary of the first version of VB. The original language that changed the landscape of computing has lasted for 10 years and has enabled more...
Microsoft Visual Basic 2010 Step by Step 576 pages Publisher: Microsoft Press; 1 edition (May 7, 2010) Language: English ISBN-10: 0735626693 ISBN-13: 978-0735626690 Your hands-on, step-by-...
you′ll find that this book quickly gets you up to speed on what you need to know to get the most from Visual Basic 2010.Visual Basic 2010 offers a great deal of functionality in both tools and ...
Creating Visual Basic .NET Projects..............................................................3 Demonstration: Creating a Visual Basic .NET Project....................................16 Using ...
Teach yourself the essential tools and techniques for Visual Basic® 2010-one step at a time. No matter what your skill level, you'll find the practical guidance and examples you need to start ...
-Very easy to use Microsoft Visual Studio plug-in, automatic identification of various keywords, system functions, member variables, automatically gives the input prompt, automatically corrects ...
设置Windows环境变量工具 NVM - eNvironment Variables Manager 1.可以设置用户变量、系统变量,非常方便。 2.可以把环境变量导出成XML文件,不过不能导入 类似的工具有EnvMan,RapidEE。 此工具的源代码可以在...
Calculus_of_Several_Variables.pdf
V.M. Zolotarev的Modern theory of Summation of Random Variables,找了好久没有电子版,这个版本内部也有Hidden Page, 有需要的凑合着用吧!
Effect of Different Variables on Overall Vertical Jump Height
System_Variables_en系统变量,Expert Documentation System Variables For KUKA System Software 8.1, 8.2 and 8.3
Get a List of Windows Variables
Iterative Solution of Nonlinear Equations in Several Variables provides a survey of the theoretical results on systems of nonlinear equations in finite dimension and the major iterative methods for ...
声明个变量(Declaringavariable)58安全应急响应中心变量可以具有不同的作。有些变量是free,它们的值直接影响使它们的表达式的值,或者使它们的
这是一本面向学生的教材,涵盖了复杂变量的标准一年级研究生课程。 包括所有问题的解决方案。
0137 在Visual C++集成开发环境中定制自己的工具栏 70 0138 向Visual C++开发环境中添加插件 71 3.2 编译程序 72 0139 头文件的预编译 72 0140 Debug版本与Release版本的区别 72 0141 如何编译Unicode...
Grafna Dashboard 中,支持 `variables` 变量, 以下拉列表的形式展示在左上角,用户可选择其内容,下面的 Panel 中的对象查询中增加 $xxx 即可实现对象的切换, 比较方便。 问题: 常规实现没有问题,但对于下拉列表...
KUKA机器人系统变量手册