重拾VB之三,二进制文件篇
重拾VB之三,二进制文件篇
PMP 关劲松
鬼使神差,09年12月入职的公司仍使用VB开发软件,虽然并非专职开发,但也不得不在事隔4年之后,重新使用VB。
读取、修改二进制文件仍是大部分编程语言的基本开发技能。可以提高处理文件效率,主要应用在通信、交换数据等方面。
'open filename$ for binary #filenumber'以二进制方式建立或者打开文件,然后再用
'Put #FileNumber, postion, inputdata '写入数据inputdata,
'Get #FileNumber, postion, outputdata '读出数据到outputvarible
#FileNumber, 打开的二进制文件流。
postion, 文件中的数据位置。
inputdata 输入数据缓冲。可以使用数组或字符串。
outputdata 输出数据缓冲。可以使用数组或字符串。
代码
Sub read()
Dim strFileName1, s As String
Dim aryContent(20) As Byte
strFileName1 = App.Path & "\test.hex"
Open strFileName1 For Binary As 1
Get #1, 120, aryContent() '从文件中120字节处取出长度20的数据到数组中。
msgbox(aryContent()) '以16进制的方式显示。
s = Space(20) '重定义字符串长度。
Get #1, 2514, s ' 从文件中120字节处取出长度20的数据到字符串中。
msgbox(s) '以字符的方式显示。
Close 1
End Sub
Sub write()
Dim strFileName1, s As String
Dim aryContent(20) As Byte
strFileName1 = App.Path & "\test.hex"
Open strFileName1 For Binary As 1
s = Space(20)
s = "teststtsestssts33333"
Put #1, 2486, s '在文件中2486字节处写入长度20的字符串数据。
Debug.Print s
aryContent(0) = &H4F '初始化数组
aryContent(1) = &H33
aryContent(2) = &HA1
aryContent(3) = &H42
aryContent(4) = &H8D
Put #1, 2514, aryContent() ''在文件中2514字节处写入长度20的十六进制数组数据。
Debug.Print aryContent()
Close 1
End Sub
相关文档:
重拾VB之二,毫秒篇
PMP 关劲松
鬼使神差,09年12月入职的公司仍使用VB开发软件,虽然并非专职开发,但也不得不在事隔4年之后,重新使用VB。
在vb中如何获得毫秒精度级别的时间?vb的时间函数不支持毫秒,需要利用windows的基本API,编写程序才能获取毫秒级 ......
Welcome to Microsoft Developer Support, Languages team blog! You will find a lot of language related troubleshooting resources here.
Troubleshooting PInvoke Related Issues
I am back with some more PInvoke Stuff. Recently I was working on a PInvoke issue which I found interesting ......
不能直接使用CopyMemoryStr,应该将字符串转为byte数组,然后使用CopyMemory
Property Get item() As String
If h = 0 Then ErrRaise ERROR_INVALID_DATA
'BugAssert p <> pNull
Dim c As Long, ptr0 As Long
Dim ab() As Byte
& ......
“自动点击按钮”小工具VB源码
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Pri ......
使用VC编写VB使用DLL
一、在函数定义前必须加上extern "c",_stdcall关键字。
extern "C" int _stdcall Sum(int x,int y)
{
return x+y;
}
二、DLL的.def文件中必须加上入口函数
EXPORTS
sample @1
  ......