【问题标题】:Calling UNRAR.exe using system in c++在 C++ 中使用系统调用 UNRAR.exe
【发布时间】:2023-03-10 18:38:01
【问题描述】:

我试图在 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;
}

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

非常感谢您的帮助。

【问题讨论】:

  • 我的水晶球说您的 32 位版本使用 c:\program files (x86)\winrar 作为默认目录。
  • 感谢您的回复汉斯。我告诉程序运行 {_chdir("C:\\Program Files\\WINRAR\\");} 中的程序。这很奇怪,因为我可以毫无问题地从 MATLAB 调用完全相同的命令。我没有安装 32 位版本的 RAR。

标签: c++ windows system


【解决方案1】:

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

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

说明:要找出此类错误,检查errno 值(#include)很有用。值为 22 表示参数无效。

【讨论】:

  • 感谢您的回复。这不是编译或链接器错误。该程序运行良好,除非控制台应用程序正在运行,否则我在 cmd 中得到以下答案:“文件名、目录名或卷标语法不正确”。我还尝试了以下方法: _chdir("C:\Program Files\WINRAR\\");没有成功
  • 我的意思是 _chdir("C:\\Program Files\\WINRAR");
猜你喜欢
  • 2016-02-09
  • 2012-09-23
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 2013-01-20
  • 2023-03-09
  • 1970-01-01
  • 2014-12-25
相关资源
最近更新 更多