我想在我的程序中加入Google搜索支持,也就是说,我想给出一个关键词,然后直接打开Google搜索此关键词的页面,我采用的是这样的方法:
加入我想搜索:源代码 下载
我发现手工在Google搜索时,IE地址栏里显示:
http://www.google.com/search?hl=zh-CN&q=%E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD&btnG=Google%E6%90%9C%E7%B4%A2&lr=在程序中用ShellExecute执行该字符串,可以打开正确的搜索页面,但是若换成:
http://www.google.com/search?hl=zh-CN&q=源代码+下载&btnG=Google%E6%90%9C%E7%B4%A2&lr=就不行了,可是我需要动态设置搜索关键词,但是我不知道如何将“源代码 下载”转换为“E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD”的形式,怎么办?请高手们指点一下迷津!谢谢!
加入我想搜索:源代码 下载
我发现手工在Google搜索时,IE地址栏里显示:
http://www.google.com/search?hl=zh-CN&q=%E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD&btnG=Google%E6%90%9C%E7%B4%A2&lr=在程序中用ShellExecute执行该字符串,可以打开正确的搜索页面,但是若换成:
http://www.google.com/search?hl=zh-CN&q=源代码+下载&btnG=Google%E6%90%9C%E7%B4%A2&lr=就不行了,可是我需要动态设置搜索关键词,但是我不知道如何将“源代码 下载”转换为“E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD”的形式,怎么办?请高手们指点一下迷津!谢谢!
解决方案 »
- vs2005生成的exe如何在其他机子上运行?
- vc++ vc#的的比较~~
- ado access recodset如何具体得到表中的数据 急 急 急 急 急 急 急 急 急 急 急 急
- 如何判断程序是自运行的还是用户启动的呢?
- 在非模式对话框中,为了使对话框可以处TAB等其他按键消息,必须在消息循环中加入的对话框消息处理函数是什么?
- 跪请高手赐教,如何在DIALOG中的控件FRAME上显示图象!!!!!!
- 有没有将普通字符串直接转到UTF8格式的函数?
- 我花了20 天没有 搞定 CEdit --- 有此功能吗?
- 求救!!!!!!
- SetWindowsHookEx 拦截不到消息
- recvfrom为何总是出现10022的错误?
- 如何判断DLL已注册?
char c[2] = "中"
WORD * pw = (WORD)c;
pw[0]就是内码值,然后转成16进制的字符串形势就可以了记得加%标记
现成的给你
static int ConvertCodePage(UINT uSrcCodePage, LPCSTR pcszIn, int iLen, UINT uDstCodePage, LPSTR &pszOut)
{
int iWideCharLen = ::MultiByteToWideChar(uSrcCodePage, 0, pcszIn, iLen, NULL, 0);
LPWSTR lpszWideChar = new WCHAR[iWideCharLen + 1]; memset(lpszWideChar, 0, (iWideCharLen + 1) * sizeof(WCHAR)); iWideCharLen = ::MultiByteToWideChar(uSrcCodePage, 0, pcszIn, iLen, lpszWideChar, iWideCharLen); if (uDstCodePage == 54936 && !::IsValidCodePage(54936))
{
uDstCodePage = 936;
} int iOutLen = ::WideCharToMultiByte(uDstCodePage, 0, lpszWideChar, iWideCharLen, NULL, 0, NULL, NULL); pszOut = new CHAR[iOutLen + 1];
memset(pszOut, 0, iOutLen + 1); iOutLen = ::WideCharToMultiByte(uDstCodePage, 0, lpszWideChar, iWideCharLen, pszOut, iOutLen, NULL, NULL); delete[] lpszWideChar; return iOutLen;
}// URL encoding function
static int EncodeURL(LPCSTR pcszIn, int iLen, LPSTR &pszOut)
{
CHAR szTemp[MAX_URL_LEN] = {'\0'};
int iOutLen = 0; for (int i = 0; i < iLen; i ++)
{
if (pcszIn[i] >= 'A' && pcszIn[i] <= 'Z') // A-Z
{
szTemp[iOutLen] = pcszIn[i];
iOutLen += 1;
}
else if (pcszIn[i] >= 'a' && pcszIn[i] <= 'z') // a-z
{
szTemp[iOutLen] = pcszIn[i];
iOutLen += 1;
}
else if (pcszIn[i] >= '0' && pcszIn[i] <= '9') // 0-9
{
szTemp[iOutLen] = pcszIn[i];
iOutLen += 1;
}
else if (
pcszIn[i] == '@'
|| pcszIn[i] == '*'
|| pcszIn[i] == '_'
|| pcszIn[i] == '-'
|| pcszIn[i] == '.')
{
szTemp[iOutLen] = pcszIn[i];
iOutLen += 1;
}
else if (pcszIn[i] == ' ')
{
szTemp[iOutLen] = '+';
iOutLen += 1;
}
else
{
sprintf(szTemp + iOutLen, "%%%02X", pcszIn[i] & 0xFF);
iOutLen += 3;
}
} szTemp[iOutLen] = '\0'; pszOut = new CHAR[iOutLen + 1];
memcpy(pszOut, szTemp, iOutLen + 1); return iOutLen;
}用法:
CString strURL = _T("http://www.google.com/search?hl=zh-CN&q=");
CString strKeywords = _T("游戏 下载");
LPSTR pszKeywordsUTF8 = NULL;
LPSTR pszKeywordsEncoded = NULL;
int iLen = 0;// Convert to UTF-8 keywords
iLen = ConvertCodePage(CP_ACP, strKeywords, strKeywords.GetLength(), CP_UTF8, pszKeywordsUTF8);if (NULL == pszKeywordsUTF8)
{
return;
}// Encode keywords
iLen = EncodeURL(pszKeywordsUTF8, iLen, pszKeywordsEncoded);delete[] pszKeywordsUTF8;if (NULL == pszKeywordsEncoded)
{
return;
}strURL += pszKeywordsEncoded;delete[] pszKeywordsEncoded;::ShellExecute(NULL, _T("open"), strURL, NULL, NULL, SW_SHOWNORMAL);