0

I am having troubles writing a C++ program that can run a command in cmd. I would like to have my C++ program run the following command adb install NativeAndroidTest.apk When I run this command in cmd, it works if I am in the same directory as NativeAndroidTest.apk, but not when I am in a different directory. That's ok, but no matter what I do, I can't get my C++ program to run the command.

The sample code:

#include <Windows>

int main() {

    system("cd C:\\Users\\brenden\\Documents\\Visual Studio 2017\\Projects\\NativeAndroidTest\\NativeAndroidTest\\NativeAndroidTest.Packaging\\ARM\Release");
    system("adb install NativeAndroidTest.apk");


    return 0;
}

I fixed the problem by changing the code to

_wchdir((wchar_t*)"C:\\Users\\brenden\\Documents\\Visual Studio 2017\\Projects\\NativeAndroidTest\\NativeAndroidTest\\NativeAndroidTest.Packaging\\ARM\\Release");
system("adb install NativeAndroidTest.apk");

Thanks for the help. If someone adds a proper answer I'll mark it as correct so you get points.

I'd like to mention, just in case someone else reads this, trying to cast a string to a wchar pointer is a bad Idea, and my program only worked because of a fluke (I had the apk stored in the program's working directory).

you should convert the string to a wchar pointer with a function like this

wstring Commands::convertStringToWstring(const std::string &input) {
   wchar_t* buffer = new wchar_t[input.size() * 2 + 2];
   swprintf(buffer, L"%S", input.c_str());
   std::wstring returnWstring = buffer;
   delete[] buffer;
   return returnWstring;
}
  • 3
    Are you calling that via the `system()` function? – πάντα ῥεῖ Feb 02 '17 at 10:05
  • I did try using the system function system("adb install NativeAndroidTest.apk"); I couldn't seem to get it to work, even when I placed NativeAndroidTest.apk in C: –  Feb 02 '17 at 10:08
  • 1
    What have you that failed? – Martin Bonner supports Monica Feb 02 '17 at 10:08
  • have you tried switching the working directory using cd command? – Muhammad Nizami Feb 02 '17 at 10:08
  • 1
    use `chdir`. or simply use full paths. – cshu Feb 02 '17 at 10:09
  • 1
    Your program needs to run in the same working directory where `NativeAndroidTest.apk` exists, or you have to pass the full path of that file. – πάντα ῥεῖ Feb 02 '17 at 10:09
  • How are you compiling the C++? How are you executing it? If you are using Visual Studio, you probably want to set the current directory in which to execute the program. – Martin Bonner supports Monica Feb 02 '17 at 10:10
  • 2
    @MuhammadNizami `system("cd ...")` doesn't work. – πάντα ῥεῖ Feb 02 '17 at 10:11
  • When I run the command normally in cmd, and supply the full path I get Invalid APK file: error, but if I run the command in the same directory as the apk file it works perfectly –  Feb 02 '17 at 10:12
  • 3
    @holycatcrusher As mentioned `system("cd ...")` doesn't work. Take a look [here](http://stackoverflow.com/questions/3485166/change-the-current-working-directory-in-c) how to change your programs current working directory. – πάντα ῥεῖ Feb 02 '17 at 10:25
  • 4
    The system() function call starts a shell (cmd if you're on Windows) and runs the command that you specify within it. Each call to system() will start a separate shell. The "cd" command only applies to the shell that it's run in, not to the program that you call it from. That means that the "cd" command in the first call to system() will have no effect on the second call to system(). As others have commented, you should either call chdir() from your program before calling system("adb install ..."), or specify the full path in the "adb install". – sheltond Feb 02 '17 at 10:27
  • @holycatcrusher "_supply the full path I get Invalid APK file_": Did you remember to enclose the path in double-quotes? Something like: `adb install "C:\\Users\\brenden\\Documents\\Visual Studio 2017\\Projects\\NativeAndroidTest\\NativeAndroidTest\\NativeAndroidTest.Packaging\\ARM\Release\NativeAndroidTest.apk"` – TripeHound Feb 02 '17 at 10:29
  • @πάντα ῥεῖ you were right, I had to use _wchdir. I fixed the problem. –  Feb 02 '17 at 10:35
  • Why are you using _wchdir instead of _chdir? In any case, you are creating a wide string literal incorrectly. – Klitos Kyriacou Feb 02 '17 at 10:40
  • @Klitos Kyriacou because it was the one that auto complete suggested. After checking, I don't think _chdir is in the Windows.h header. –  Feb 02 '17 at 10:41
  • You can't convert from char* to wchar_t* via C-style cast. Using wide chars is really simple, however, just add an L in front of the quote, like L"C:\\...". – z32a7ul Feb 02 '17 at 10:43
  • 1
    If you are using windows, you may also try CreateProcess. – z32a7ul Feb 02 '17 at 10:44

0 Answers0