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;
}