前几天刚刚发了一篇 pb调用.net组件的实践
但是遇到了汉字乱吗的问题。经过测试,在pb9中调用不会出现乱码。但是由于原来的项目是用pb7写的。全部转移到pb9上的工作量是很大的。所以不能离开pb7的环境。最后采用了汉字转换成byte数据的方式解决了这个问题。
类代码:
Imports
System.Math
<
ComClass(CodeQuery.ClassId,CodeQuery.InterfaceId,CodeQuery.EventsId)
>
_

Public
ClassCodeQuery
ClassCodeQuery


COMGUID#Region"COMGUID"
'这些GUID提供此类的COM标识
'及其COM接口。若更改它们,则现有的
'客户端将不再能访问此类。
PublicConstClassIdAsString="225d6048-672b-42c0-a623-6688596592b0"
PublicConstInterfaceIdAsString="4812e4bd-0aa8-4716-a306-6d774d7cdc72"
PublicConstEventsIdAsString="a47bfff0-24ae-45ee-809a-2664efcd1777"
#EndRegion

'可创建的COM类必须具有一个不带参数的PublicSubNew()
'否则,将不会在
'COM注册表中注册此类,且无法通过
'CreateObject创建此类。

PublicSubNew()SubNew()
MyBase.New()
EndSub


PublicFunctiongetCodeList()FunctiongetCodeList()AsSortedList
DimslAsSortedList
DimcrcAsNewCrcDbConnection.CrcDbConnection
crc.ConnDatabase()
sl=crc.GetCodeSortList
Returnsl
EndFunction
'''<summary>
'''返回字符串
'''</summary>
'''<returns>返回拼接成字符串的编码信息</returns>
'''<remarks>key;value|key;value|……</remarks>

PublicFunctiongetCodeString()FunctiongetCodeString()AsString
DimslAsSortedList
sl=getCodeList()
DimstrCodeAsNewSystem.Text.StringBuilder
IfNotslIsNothingThen
DimsItemAsSystem.Collections.DictionaryEntry
ForEachsItemInsl
strCode.Append(sItem.Key)
strCode.Append(";")
strCode.Append(sItem.Value)
strCode.Append("|")
Next
EndIf
ReturnIIf(strCode.Length>0,strCode.Remove(strCode.Length-1,1).ToString,"")
EndFunction
'''<summary>
'''返回数组
'''</summary>
'''<returns>返回生成的数组</returns>
'''<remarks>二维数组储存key/value对</remarks>

PublicFunctiongetCodeArray()FunctiongetCodeArray()AsString(,)
DimarrCode(,)AsString
DimiAsLong=0
DimslAsSortedList
sl=getCodeList()
ReDimarrCode(sl.Count,2)
IfNotslIsNothingThen
DimsItemAsSystem.Collections.DictionaryEntry
ForEachsItemInsl
arrCode(i,0)=sItem.Key
arrCode(i,1)=sItem.Value
i+=1
Next
EndIf
ReturnarrCode
EndFunction
'''<summary>
'''返回编码
'''</summary>
'''<returns>返回选择编码</returns>
'''<remarks></remarks>

PublicFunctiongetCode()FunctiongetCode()AsString
DimarrCodeAsString=""
DimiAsLong=0
DimslAsSortedList
sl=getCodeList()
IfNotslIsNothingThen
arrCode=sl.GetByIndex(sl.IndexOfKey("代码"))
EndIf
ReturnarrCode
EndFunction
'''<summary>
'''返回asc
'''</summary>
'''<returns>返回asc编码串</returns>
'''<remarks></remarks>

PublicFunctiongetCodeStringASC()FunctiongetCodeStringASC()AsString
DimslAsSortedList
sl=getCodeList()
DimbyAsByte()
DimiAsLong
DimstrCodeAsNewSystem.Text.StringBuilder
IfNotslIsNothingThen
DimsItemAsSystem.Collections.DictionaryEntry
ForEachsItemInsl
by=System.Text.Encoding.Default.GetBytes(sItem.Key)
Fori=0Toby.GetUpperBound(0)
strCode.Append(by(i).ToString())
strCode.Append(":")
Next
strCode.Remove(strCode.Length-1,1)
strCode.Append(";")
by=System.Text.Encoding.Default.GetBytes(sItem.Value)
Fori=0Toby.GetUpperBound(0)
strCode.Append(by(i).ToString())
strCode.Append(":")
Next
strCode.Remove(strCode.Length-1,1)
strCode.Append("|")
Next
strCode.Remove(strCode.Length-1,1)
EndIf
ReturnstrCode.ToString
EndFunction
'''<summary>
'''返回asc数组
'''</summary>
'''<returns>返回生成的asc数组</returns>
'''<remarks>二维数组储存key/value对</remarks>

PublicFunctiongetCodeArrayAsc()FunctiongetCodeArrayAsc()AsString(,)
DimarrCode(,)AsString
DimiAsLong=0
DimslAsSortedList
sl=getCodeList()
DimbyAsByte()
DimjAsLong
DimstrCodeAsNewSystem.Text.StringBuilder
ReDimarrCode(sl.Count,2)
IfNotslIsNothingThen
DimsItemAsSystem.Collections.DictionaryEntry
ForEachsItemInsl
by=System.Text.Encoding.Default.GetBytes(sItem.Key)
Forj=0Toby.GetUpperBound(0)
strCode.Append(by(j).ToString())
strCode.Append(":")
Next
IfstrCode.Length>0ThenstrCode.Remove(strCode.Length-1,1)
arrCode(i,0)=strCode.ToString
strCode.Remove(0,strCode.Length)

by=System.Text.Encoding.Default.GetBytes(sItem.Value)
Forj=0Toby.GetUpperBound(0)
strCode.Append(by(j).ToString())
strCode.Append(":")
Next
IfstrCode.Length>0ThenstrCode.Remove(strCode.Length-1,1)
arrCode(i,1)=strCode.ToString
strCode.Remove(0,strCode.Length)
i+=1
Next
EndIf
ReturnarrCode
EndFunction
EndClass


请注意其中的
Public Function getCodeStringASC() As String 返回字符串
和
Public Function getCodeArrayAsc() As String(,) 返回数组
在 pb7 中的代码:
split 函数 public function long split (string str1, string sep, ref string arrR[])
long
lPos
=
1
long
lFind
string
arrNull[]
if
isnull
(str1)
or
isnull
(sep)
then
setnull(lPos)
returnlPos
end
if
arrR[]
=
arrNull[]
lPos
=
1
lFind
=
pos(upper(str1),upper(sep))
do
while
lFind
>
0
arrR[lPos]
=
left
(str1,lFind
-
1
)
str1
=
right
(str1,
len
(str1)
-
lFind
-
len
(sep)
+
1
)
lFind
=
pos(upper(str1),upper(sep))
lPos
=
lPos
+
1
loop
arrR[lPos]
=
str1
returnlPos
getcode1 函数 public function getcode1(ref string arrKey[], ref string arrValue[]) returns (none)
字符串方式
int
intValue
oleobjectobjOle
objOle
=
createOLEObject
intValue
=
objOle.connecttonewobject(
"
codeforcom.CodeQuery
"
)
if
intValue
=
0
then
string
v
int
i,j,k
v
=
objOle.getcodestringAsc()

string
oItem[]
string
oKey[]
string
oAsc[]
string
key
split
(v,
"
|
"
,refoItem)
for
i
=
1
to
upperbound(oItem)
split
(oItem[i],
"
;
"
,refoKey)
for
j
=
1
to
upperbound(oKey)
split
(oKey[j],
"
:
"
,refoAsc)
key
=
""
for
k
=
1
to
upperbound(oAsc)
key
=
key
+
char(
long
(oAsc[k]))
next
if
j
=
1
then
arrKey[i]
=
key
else
arrValue[i]
=
key
end
if
next