[Qt4] Using Qt to create and execute vbscript
QString iconPath = QDesktopServices::storageLocation(QDesktopServices::PicturesLocation);
iconPath.append("\\").append(IPCameraList[configIPCameraIdx].modelName).append(".ico");
/* 00 check file exist and remove */
QFile lfile(pathLink);
if (lfile.exists()){
qDebug() << "remove .lnk file !!";
lfile.remove();
}
/* 01 create vbscript file */
// 01-01 open file for write
bool startprocess = false;
bool removefile = false;
QFile sfile(QCoreApplication::applicationDirPath()+"//shortcut.vbs");
if(!sfile.open(QIODevice::WriteOnly)){
qDebug() << "file open error!!";
}
// 01-02 writing file content and close finish
QTextStream wrcontent(&sfile);
wrcontent << "Set oWS = WScript.CreateObject(\"WScript.Shell\")\n";
wrcontent << "sLinkFileLocation = \"" << pathLink << "\"\n";
wrcontent << "browserLink = \"%windir%\\explorer.exe \"\n";
wrcontent << "webLink = \"\"\"https://"<< portal.right(portal.length()-7) << "device#" <<IPCameraList[configIPCameraIdx].myNo << "?lang=" << lang << "\"\"\"\n";
wrcontent << "Set oLink = oWS.CreateShortcut(sLinkFileLocation)\n";
wrcontent << " oLink.TargetPath = browserLink\n";
wrcontent << " oLink.Arguments = webLink\n";
wrcontent << " oLink.IconLocation = \""<< iconPath << "\"\n";
wrcontent << " oLink.Save\n";
wrcontent << "Wscript.Quit 0\n";
sfile.close();
if (sfile.exists()){
startprocess=true;
qDebug()<< "create vbscript file success !!";
}else{
qDebug()<< "create vbscript file fail !!";
}
/* 02 run qprocess */
// 02-01 create procees execute vbscript
QProcess process;
QString script ="wscript";
QStringList args;
args << QString(QCoreApplication::applicationDirPath()+ "//shortcut.vbs");
// 02-02 procees wait while file writing
do{
qDebug() << "start procees !!";
process.execute(script, args);
removefile = true;
break;
}while(startprocess);
/* 03 remove vbscript file */
do{
qDebug() << "delete vbscript file !!";
QFile dfile(QCoreApplication::applicationDirPath()+"//shortcut.vbs");
//dfile.remove();
break;
}while(removefile);
QProcess process2;
QString script2 = QString(QCoreApplication::applicationDirPath()+ "//run.bat");
process2.execute(script2);
沒有留言:
張貼留言