看看对你有帮助吗!
http://www.csdn.net/develop/read_article.asp?id=12675
http://www.csdn.net/develop/read_article.asp?id=12675
解决方案 »
- 关于启动窗体假死的问题
- VB如何控制VPN连接,进行拔号和更改连接IP?!
- ie.busy的错误问题,老是提示说Method 'busy' of object 'IWebBrowser2' failed,帮帮忙!!!!
- 初学: 如何截取屏幕上某一区域的图象?
- 像vb自己的工具栏的窗体怎么做啊!好象它可以随便拖放的?同时也可以成为mdi窗体,也可以在菜单栏下面
- 有谁知道如何在进行查询的同时,实现数据库数据更新?(SQL2000+ADO)
- 求:real producer控件的重要的属性,事件说明及其相应的值
- 请问如何让vb 控制其它程序的窗口取大最小化
- 请教中国农历的算法。
- 请教专家如何在VB中调用html格式的help文件?
- 在数据表中插入一条记录
- 背景音乐
比如:
BOOL 函数名(char* lpBuffer);申明成:
Declare Function 函数名 Lib dll名(ByVal lpBuffer As String) As Long
调用时:
dim a as string *255 (假设255足够)
dim b as long
b = 函数名 (a)
如果想真正明白VB中的指针
还是应该看一下AdamBear的文章
然后再去读内存就ok.
Dim L as Long F = 4
L = VarPtr(F) F is the name of the variable, and L, after calling VarPtr, contains the memory location of the variable. You can edit the value if you want. USING A POINTER There are many ways to use a pointer. One way is to hand it over to a Win32 API call such as CopyMemory. This is the Declare statement for CopyMemory: Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Destination and Source can either be a variable (use ByRef) or a memory location (use ByVal). For example: CopyMemory ByVal TML, ByVal SML, 2 'Or CopyMemory NewNum, Num, 2 Here, TML and SML are pointers, and NewNum and Num are the variables they point to, respectively. Note that you use ByVal when using pointers, and ByRef (default) when using variable names. You can even mix the two, like this: CopyMemory ByVal TML, Num, 2 CopyMemory NewNum, ByVal SML, 2 Use the first one if you know the variable name of the source, and use the second one if you know the variable name of the destination. The Length argument specifies the number of bytes that you want to be transferred. I use 2, because there are two bytes in an integer (the last time I checked). I would really appreciate it if someone sends me a table listing the amount of bytes each data type in VB takes up. EXAMPLE My demo program is finally out of the infirmary. It took about a minute to make, and 1hour, 59 minutes to debug. All it does is prompt you for a number, then it uses CopyMemory to copy the value to another variable using variable pointers. Check it out! It is quite simple, and I know it isn't the best example around. I was originally going to make a sorting program, but it proved too difficult to debug. So this is what I used instead. The first step I took was create the project structure, which consists of a project (duh), a form, and a module. The code module contains the Win32 API call to CopyMemory. The form consists of a few labels; I won't cover them here because you can check the code. I'll then dissect the code for the form one block at a time. Private Sub cmdTest_Click() Please don't tell me you don't know what this does! Dim Num As Integer 'Source Variable
Dim NewNum As Integer 'Target variable
Dim SML As Long 'Source Memory Location
Dim TML As Long 'Target Memory Location I declared the variables. We're nearing the meat of the program! 'Error Handling
If Not IsNumeric(txtVal.Text) Then
MsgBox "X not numeric!"
Exit Sub
End If
If Val(txtVal.Text) > 32767 Then
MsgBox "X must be less than 32767!"
Exit Sub
End If
If Val(txtVal.Text) < -32768 Then
MsgBox "X must be greater than -32768!"
Exit Sub
End If 'Assign values
Num = Val(txtVal.Text)
NewNum = 0 The preceding was some error checking, to ensure that X is within the boundaries of an integer, and then after that, I assigned X to that value. 'Get Memory Locations
SML = VarPtr(Num)
TML = VarPtr(NewNum)
'CopyMemory
CopyMemory NewNum, ByVal SML, 2 'Or CopyMemory NewNum, Num, 2 This code is the heart of the program. It simply assigns SML and TML the memory locatiosn of Num and NewNum. Then, it uses CopyMemory to copy data from Num to NewNum. I could have used the code inside the comments, but I wanted to demonstrate what you could do with variable pointers. 'Display Results
lblMemLoc.Caption = "Source Memory Location: " & Str(SML)
lblNewMemLoc.Caption = "Target Memory Location: " & Str(TML)
lblNewVal.Caption = "Value of target after CopyMemory: " & Str(NewNum) After all that, I simply displayed the results. And that's that! CONCLUSION Pointers used to belong only in the C++ realm. But with the discovery of VarPtr, and the use of CopyMemory, Visual Basic now has this feature. I hope I have documented this feature well. But pointers can be hard to manage, if you're not careful. So harness this power well... Good luck! ====================================== Tom Quantum
Please leave comments! I appreciate any kind of feedback. And if you find this useful, I would appreciate it if you vote for it. That way, I will make better articles in the future. 以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2003-1-7 20:35:58
软件版本: 1.0.816
软件作者: Shawls
个人主页: Http://Shawls.Yeah.Net
E-Mail: [email protected]
QQ: 9181729