win32+QT 实现exe中执行返回的网页内容重定向到txt文件,根据需求读取里面内容

来源:互联网 发布:java纽约大亨 编辑:程序博客网 时间:2024/05/24 03:38

1.    创建共享重定向文件,通过CreateFile创建的内核对象返回在子进程中执行的网页结果

        SECURITY_ATTRIBUTES sa = {sizeof(sa), NULL, TRUE};
SECURITY_ATTRIBUTES* psa = NULL;
DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
OSVERSIONINFO osVersion = {0};
osVersion.dwOSVersionInfoSize = sizeof(osVersion);
if (GetVersionEx(&osVersion))
{
if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
psa = &sa;
dwShareMode |= FILE_SHARE_DELETE;
}
}




//根据版本设置共享模式和安全属性
HANDLE hConsoleRedirect = CreateFile(
szFileDir,                                                                       // szFileDir 重定向输出的文件路径
GENERIC_WRITE,
dwShareMode,
psa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);


WCHAR order[1024];
swprintf(order, L"curl -d \"name44_submit=submit\" http://admin:888888@192.168.0.206/cgi-bin/vgaic.cgi --connect-timeout 2 --retry-max-time 2 --max-time 5");

        // 将curl命令行执行的网页内容返回 
STARTUPINFO si;
PROCESS_INFORMATION pi;


ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
//使用标准柄和显示窗口
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdOutput = hConsoleRedirect;//将文件作为标准输出句柄
si.wShowWindow = SW_HIDE; //隐藏控制台窗口  


ZeroMemory(&pi, sizeof(pi));
unsigned int k1 = GetTickCount();
// Start the child process.
if( !CreateProcess(NULL, // No module name (use command line).
order,  // Command line.
NULL,              // Process handle not inheritable.
NULL,          // Thread handle not inheritable.
TRUE,      // Set handle inheritance to FALSE.
NULL,  // No creation flags.
NULL,              // Use parent's environment block.
NULL,          // Use parent's starting directory.
&si,      // Pointer to STARTUPINFO structure.
&pi)  // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "CreateProcess failed\n");
}
else
{
WaitForSingleObject(pi.hProcess, INFINITE);


// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}


CloseHandle(hConsoleRedirect);


 2.  // 创建文件,读取重定向内容

   int nLineNum= 2;
// 读文件进行初始化预览
strDir = QCoreApplication::applicationDirPath();
strDir+="/encStatus.txt";
QFile encFile(strDir);
QString strContents;
if( encFile.exists())
{
if( encFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream txtOutStream(&encFile);
QTextCodec *codec=QTextCodec::codecForName("UTF-8");
txtOutStream.setCodec(codec); 
while( !txtOutStream.atEnd())
{
QString strNum;
strNum.setNum(nLineNum);
//strContents+=strNum;//
//strContents+=":  ";//
strContents+=txtOutStream.readLine();
strContents+="\n";


//nLineNum++;//
}
}
}
encFile.close();


0 0
原创粉丝点击