寫unicode控件時發現的傳字串參數的問題:
問題描述:
unicode的ocx,屬性參數text,類型:bstr。
控件的源碼(vc中)
afx_msg void settext(lpctstr lpsztext)
vb調用1:
dim strtest as string
strtest = text1.text ‘text1文本框為空
if not isnull(strtest) then
testocx2221.stringtest = strtest
end if
結果:
settext中lpsztext是null。
vb調用2:
dim strtest as string
strtest = ""
if not isnull(strtest) then
testocx2221.stringtest = strtest
end if
結果:
settext中lpsztext不為null,得到指向空串的有效指針。
因此:
上述調用1時:
當控件的處理程序直接調用了cstring的 == 或 !=,那就有問題了,因為cstring這兩個操作符不支持null,會出錯。也就是調用了不支空指針的操作。
好吧,還是采用保險的做法吧: 加多一句:
if(lpsztext==null) lpsztext = _t("");
很奇怪:
不知道vb的兩種調用為啥會傳出不同的值。沒明白。
至于為什么ansi不會有問題,可能是因為vb調用ansi的控件時,需要對字串進行內部的轉換(由unicode轉ansi),而去除了可能為空指針的情況。
新聞熱點
疑難解答