-1

OS: Windows 8.1 64bit | IDE: Visual Studio 2018

https://pastebin.com/6Lh6kABe -- if you need proper formatted code.

I'm developing a small command line tool to take screenshots using ADB. (Detail, ADB will be included in the same directory as the app; currently not there though.)

After fixing over 30 errors in my code that stopped the build process, now I'm here. An hour went by and I am not able to fix it, so I decided to ask you on here.

Here is the code:

    // ADBSS.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
//

#include <pch.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <tchar.h>

using namespace std;

int main(int argc, char** argv) {
    std::string filename;
    filename = "a";
    SetConsoleTitle(_T("*-_ ADB Screenshooter _-*"));
    std::cout << "+---------------------------------+" << endl;
    std::cout << "|ADB Screenshooter   [v1.0]       |" << endl;
    std::cout << "|Take screenshots from your device|" << endl;
    std::cout << "|with a simple CLI tool.          |" << endl;
    std::cout << "+---------------------------------+" << endl;
    cout << "Welcome to ADB Screenshooter." << endl;
    cout << "Please input the filename: (The screenshot will be saved with that name)" << endl;
    cout << "DO NOT INCLUDE ANY SPACES IN THE FILENAME. Use only letters." << endl;
    cin >> filename;
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Taking the screenshot _-*"));
    cout << "Trust your computer now if you haven't before." << endl;
    system("adb shell screencap -p /sdcard/ADBScreenshooter/" + filename.c_str() + ".png");
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Copying to PC! _-*"));
    cout << "The file will now be copied to the location from where you run ADB Screenshooter." << endl;
    system("adb pull /sdcard/" + filename.c_str() + ".png");
    Sleep(4);
    system("cls");
    SetConsoleTitle(_T("*-_ Done! _-*"));
    cout << "Everything is done! Thanks for using ADBSS. Press any key to finish." << endl;
    system("pause>nul");
    return 0;
}

Current errors are:

Ważność Kod Opis    Projekt Plik    Wiersz  Stan pominięcia
Błąd    C2110   "+": cannot add two pointers    ADBSS

Line 29 and 34.

  • 1
    `filename.c_str() ` -> `filename` in your `system` calls and let the C++ standard library work its magic. – Bathsheba Sep 20 '18 at 13:14
  • @Bathsheba `system` expects a `const char*` so it will still get an error. – NathanOliver Sep 20 '18 at 13:15
  • @NathanOliver: You'd put a c_str() around the temporary, as I know you know ;-) – Bathsheba Sep 20 '18 at 13:16
  • Note that `system` with user input should be avoided to avoid exploit similar to [Exploits of a Mom](https://xkcd.com/327/) – Jarod42 Sep 20 '18 at 13:21
  • Just to avoid confusing situations, you might want to filter the file name to remove any "funny" special characters, to be safe everything except `a-z A-Z 0-9 . _ -` to be safe (or dig out exact list of special chars for *cmd.exe* commands). – hyde Sep 20 '18 at 13:24
  • extra `\"` might allow to handle space: `system("adb shell screencap -p \"/sdcard/ADBScreenshooter/" + filename + ".png\"");`. – Jarod42 Sep 20 '18 at 13:24
  • I'll bet you could remove 95% of that code and still show the problem. – Pete Becker Sep 20 '18 at 15:56
  • If you consider one of the answers "correct", click the checkmark for that answer. This marks the question as "answered". Or clarify which part of your question remained unanswered. – DevSolar Sep 23 '18 at 10:04

2 Answers2

3

All your calls of the form

system("string1" + filename.c_str() + "string2");

need to be replaced with

system(("string1" + filename + "string2").c_str());

"string1" is a const char[] literal that decays to const char* when + is applied. filename.c_str() is also a const char* pointer. The compiler issues a diagnostic when you attempt to add two pointers, as that's meaningless.

Writing it the way I have forces + to be the overloaded + operator of the std::string class, which effects a concatenation.

My writing c_str() at the end extracts the data buffer from the anonymous temporary std::string, which is valid for the lifetime of the system function.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Aaaand guess what, [it worked!](http://despacito.solutions/iNy1oBWtT3a030Q.png) Gonna add this as the answer as soon as I can (6 minutes left right now) – Falco Każań II Sep 20 '18 at 13:21
  • 2
    `decltype(auto) system(const std::string& s) { return system(s.c_str()); }` to simplify call sites to `system("string1" + filename + "string2")`. – Jarod42 Sep 20 '18 at 13:28
2

A string literal is of type char const[] (which will decay to char const *). The return type of c_str() is char const *. operator+() is defined for std::string, but not for char pointers. You cannot add two pointers.

You can fix this by setting up the command in a std::string, and then calling system( s.c_str() ) instead of putting together the command in-line:

std::string s( "adb shell screencap -p /sdcard/ADBScreenshooter/" );
s += filename;
s += ".png";

std::system( s.c_str() );
DevSolar
  • 67,862
  • 21
  • 134
  • 209