使用c ++中的系统调用UNRAR.exe

时间:2014-06-10 15:25:15

标签: c++ windows system

我试图在VC2010中运行一个非常简单的控制台应用程序,我无法使用系统函数成功将参数传递给unrar.exe。该应用程序旨在找到我忘记的RAR文件的密码。这是主文件:

#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
#include <direct.h>
#include "Password.h"
#include <windows.h>
using namespace std;


string RARPath  = "\"UnRAR.exe\"";
string FP2 = "\"C:\\Program Files\\WINRAR\\RU.rar\"";
string Access = "runas /user:NimaNikvand ";
string Destination = "\"C:\\Tester\\Extracted\"";

int main(int argc, char ** argv)
{

    for(int Passlength=2;Passlength<50;Passlength++)
    {
        Password pass(Passlength);
        for(int i=0;i<pow(pass.AlphaBeta.size()*1.0,Passlength);i++)
        {
            time_t t1 = clock();
            pass.IncrementalSweep();
            cout<<"Trying Password: "<<pass.GetPass()<<endl;;
            string Command =RARPath+" x"+" -p\""+pass.GetPass()+"\" "+FP2+" *.* "+Destination;
            cout<<Command<<endl;
            _chdir("C:\\Program Files\\WINRAR\\");
            int flag = system(Command.c_str());
            time_t t2 = clock();
            cout<<"SPEED: "<<(CLOCKS_PER_SEC)/(1.0*t2-1.0*t1)<<" PASSWORDS Per Second"<<endl;

        }
    }
    return 0;
}

一切正常,传递对象不断使用其类定义中的函数进行更新,但是当我运行最终构建时,我在命令中得到以下内容:“文件名,目录名或卷标语法不正确” 。是的,我已经在cmd中手动尝试了确切的Command格式,它完全正常。我不打算使用shellexecute或花哨的createprocess API,并且需要保持这个简单的控制台应用程序。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

简答:在_chdir()调用中删除目录名称的尾部斜杠:

_chdir("C:\\Program Files\\WinRAR");

解释:要弄清楚这些错误,检查errno值(#include)很有用。值22表示无效参数。

相关问题