一、Unicode
vb6是以unicode为基础的,即双字节表示一字符。
对于中文,两个字节中,高字节是大于128的字节。
strconv(s,vbfromunicode) 表示是转为双字节与单节节混编,如果显示该串则是乱字符。
要想重新显示“正确”的字符,就得再次转为Unicode或用字节数组显示。
二、参数
1、vbNullString
Private Sub Command1_Click() Print Typename(vbNullString) 'string Print vbNullString = "" 'TrueEnd Sub
值为0的字符串,类同“”,但一般用于外部过程。在外部过程中会变成0&(长整形)
上面是INI文件结构。三部分组成,由节(section),关键字(Key),值(String或Value)组成。
'仅对win.ini文件有效Private Declare Function WriteProfileString lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String,ByVal lpszKeyname As String,ByVal lpszString As String) As LongPrivate Sub Command1_Click() WriteProfileString "IniText","Key01","AnyValue" '写入值AnyValue WriteProfileString "IniText","" '清除key01的值 WriteProfileString "IniText",vbNullString '删除key01 WriteProfileString "IniText",vbNullString,vbNullString '删除IniText节End Sub
上面""与vbNullString的功效就出来了
'不加Private时,只能专对win.ini'加了Private时,可以对自己的定义的Ini文件,文件名就是后面的lpfilename参数'lpApplicationname就是Section'lpKeyname就是Key,lpDefault是缺省值,lpReturnedString是取得值。nSize定义字串最大长度。Private Declare Function WritePrivateProfileString _ lib "kernel32" _ Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String,ByVal lpKeyname As Any,ByVal lpString As Any,ByVal lpfilename As String) As LongPrivate Declare Function GetPrivateProfileString _ lib "kernel32" _ Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize As Long,ByVal lpfilename As String) As Long
同样,返回值为0不成功,非0成功。
上面是 *** 作字串, *** 作整数是:WritePrivateProfileInt
GetPrivateProfileInt
*** 作节是:写入或读取节中所有关键字。
Private Declare Function WritePrivateProfileSection lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppname As String,ByVal lpString As String,ByVal lpfilename As String) As Long
Private Declare Function GetPrivateProfileSection lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppname As String,ByVal lpfilename As String) As Long
关键字之间以chr(0)间隔,最后以chr(0)结束
即:在节中按“key1=value"+chr(0)形式,把所有关键字串起来,最后以chr(0)结束,写入iNI文件中。
上面参数Any,看似可以任何类型,但如果我们传入是Variant将会出错,因为这个任意对于这个API来说它只会认识两种 字串与长整形。
'限制鼠标在矩形框内活动'矩形框是从左上角坐标到右下角坐标(自定义类型RECT)Private Declare Function ClipCursor lib "user32" (lpRect As Any) As LongPrivate Type RECT left As Long top As Long Right As Long Bottom As LongEnd TypePrivate Sub Command1_Click() Dim r As RECT r.left = 3 r.top = 3 r.Right = 300 r.Bottom = 300 ClipCursor r End SubPrivate Sub Command2_Click() ClipCursor ByVal 0& '不能是0&,因为API是传址,若0&则传的是0处地址,此时应传0值来取消鼠标限制,故前加byvalEnd Sub
三、内容复制
rtlmovememory 内存复制,就是从源块内存复制到目的块内存。
它是一个可重叠的复制,即目的和源是可以有重叠的,如源块内存A到D可以复制到目的块内存C和F
重叠复制不会影响目的内在的“变形”,那么它是怎么做的呢?,它会不会是把A-D先复制到下面黑块(另一区域)再把另一区域再复制到C到F呢?
答案是否定的。当源地址小于目的地址时,它会从源块从高到低地复制到目的块。若源块大于目的块的地址,就会从源块从低地址到高地址复制到目的块中。
这个不用纠结,因为这个被封装到了RtlMoveMemory中,它自动会处理。
C:
语法 VOID RtlMoveMemory(VOID UNAliGNED *Destination,//指向移动目的地址的指针。
const VOID UNAliGNED *Source,//指向要复制的内存地址的指针。
SIZE_T Length //指定要复制的字节数。
);
VB:
Declare Sub RtlMoveMemory lib "kernel32"(Dest as any,Src as any,byval Length as long)
注意:API对库名的大小写不敏感,对参数名也不敏感,但是对传递参数方式和返回值是严格的。
下面最后一个参数是传值,是不能乱写的
Option Explicit'复制内存块Private Declare Sub RtlMoveMemory _ lib "kernel32" (Dest As Any,_ Src As Any,_ ByVal Length As Long)'下面声明是错误的,会当掉VB程序。因为最后一个参数用的是址传递,API会认为是传递的地址而不是值'Private Declare Sub RtlMoveMemory _' lib "kernel32" (Dest As Any,_' Src As Any,_' Length As Long)'Private Type RECT left As Long top As Long Right As Long Bottom As LongEnd TypePrivate Sub Command1_Click() Dim r1 As RECT,r2 As RECT r1.left = 30 r1.top = 40 r1.Right = 500 r1.Bottom = 600 RtlMoveMemory r2,r1,16 '复制,16字节大小 Text1.Text = r2.BottomEnd Sub
下面是复制数据、字节,等 *** 作:
Option Explicit'注意这里变了一下形参,可以看出对API没有影响Private Declare Sub RtlMoveMemory _ lib "kernel32" (d As Any,s As Any,ByVal size As Long)Private Sub Command1_Click() '数组的复制 Dim a(10) As Long,b(10) As Long Dim i As Integer For i = 0 To 10 a(i) = i Next i RtlMoveMemory b(0),a(0),Len(a(1)) * 11 For i = 0 To 10 List1.AddItem b(i) Next iEnd SubPrivate Sub Command2_Click() '字符的复制 Dim s1 As String,s2 As String s1 = "中英mixed" RtlMoveMemory s2,s1,LenB(s1) '这里不能用len,因为计算内存大小用字节,不是计算“字符”数 Print s2End SubPrivate Sub Command3_Click() '字节提取 Dim a As Long,b(3) As Byte a = &H11223344 '16进制,这样表示可清晰看出内存中高低字节的值 RtlMoveMemory b(0),a,4 Print Hex(b(0)) & "," & Hex(b(1)) & "," & Hex(b(2)) & "," & Hex(b(3)) '44,33,22,11 可以看到低字节在索引0处,高字节在索引3处,和数据索引大小一一对应 '这个功能对于我们破解软件做注册机,或者做外挂,是绝佳利器,不用什么转换,提取快,又清晰 '后面的字节长度大小还可以自定,这样可以复制自己想要的部分,做大数运算是个不错的选择End Sub总结
以上是内存溢出为你收集整理的VB与API学习笔记(2)Unicode与参数全部内容,希望文章能够帮你解决VB与API学习笔记(2)Unicode与参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)