さらにおまけで入力した文字列でGoogle検索をするプログラムを付属させています。
(こいつのためにToUTF8()を作ることになりました)
コード: 全て選択
#console
TypeDef UINT = DWORD
TypeDef BOOL = Long
/*
Declare Function MultiByteToWideChar Lib "kernel32" (CodePage As UINT, dwFlags As DWORD, pMultiByteStr As BytePtr,
cchMultiByte As Long, pWideCharStr As *WCHAR, cchWideChar As Long) As Long
*/
Declare Function WideCharToMultiByte Lib "kernel32" (CodePage As UINT, dwFlags As DWORD, pWideByteStr As *WCHAR,
cchWideByte As Long, pMultiCharStr As BytePtr,cchMultiChar As Long, pDefaultChar As BytePtr, pUsedDefaultChar As *BOOL) As Long
Function URLEncode(pFrom As BytePtr) As String
Dim i = 0 As DWord
While pFrom <> 0
If IsCharAlphaNumeric(pFrom) Or InStr(1, "'.-*()_", Chr$(pFrom)) Then
URLEncode = URLEncode + Chr$(pFrom)
Else
URLEncode = URLEncode + "%" + Hex$(pFrom)
End If
i = i + 1
Wend
End Function
Function ToUTF8(pFrom As BytePtr) As String
Dim Length As Long, BufSize As Long
Length = lstrlen(pFrom)
BufSize = (Length + 1) * SizeOf(WCHAR)
Dim pWStr As *WCHAR
pWStr = malloc(BufSize)
Dim WStrLength As Long
WStrLength = MultiByteToWideChar(CP_ACP, 0, pFrom, -1, pWStr, BufSize)
Dim UTF8StrBufSize As Long
UTF8StrBufSize = WideCharToMultiByte(CP_ACP, 0, pWStr, WStrLength, NULL, 0, NULL, NULL)
ToUTF8 = ZeroString(UTF8StrBufSize - 1)
WideCharToMultiByte(CP_ACP, 0, pWStr, WStrLength, ToUTF8, UTF8StrBufSize, NULL, NULL)
End Function
Dim str As String, strUTF8 As String, strEncoded As String, strURL As String
Input str
strUTF8 = ToUTF8(str)
strEncoded = URLEncode(StrPtr(strUTF8))
Print strEncoded
strURL = "http://www.google.co.jp/search?q=" + strEncoded
ShellExecute(0, NULL, StrPtr(strURL), NULL, NULL, SW_SHOW)