-1

Whenever I call strcpy to make "SendEmail.exe " + log[1024] into a LCSTR I get a runtime violation exception.

I tried debugging and it hit the line:

strcpy(CmdParams, ""+log[1024]);

char log[1024];
...
if (strlen(log) > 49)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    memset(&pi, 0, sizeof(pi));
    LPSTR CmdParams;
    strcpy(CmdParams, ""+log[1024]);
    CreateProcessA(NULL, CmdParams, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
}

What should have happened was that strcpy should have copied the values into CmdParams and the next line should have been executed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
dxdxxdd
  • 17
  • 1
  • 4
    The expression `"SendEmail.exe " + log[1024]` will *not* concatenate the two strings. Instead it will add the contents of element number 1025 of the array to the pointer that the string literal results in. If you want simple string-concatenation in C++ use `std::string`. If you program in C then use another array of big enough size and use `strcpy` and `strcat`. – Some programmer dude Jun 09 '19 at 19:15
  • And no matter if you program in C or C++ (two *very* different languages, with two very different solutions to this problem, so in the future please pick the language you're actually programming in), some even basic understanding of the language should have told you that this is wrong. So please take a few steps back, and go back to your text-books and reread the chapters about string literals, array and pointers and pointer-arithmetic. – Some programmer dude Jun 09 '19 at 19:17
  • 1
    Oh, and not to mention that `CmdParams` is an uninitialized pointer. So quite a few instances of *undefined behavior* in that little code. – Some programmer dude Jun 09 '19 at 19:18
  • @Someprogrammerdude how would I concatenate the whole of log[1024] onto CmdParams in C? – dxdxxdd Jun 09 '19 at 19:37
  • and log[1024] is outside of the array... – B. Go Jun 09 '19 at 20:03
  • Why are you even doing this? Just pass `log` to `CreateProcessA`. It's already the right type. – Jonathan Potter Jun 09 '19 at 20:54
  • Not the main problem, but consider using a name other than `log`. There's a function by that name in the standard library, and you could run into problems if you add `#include ` and/or define it at file scope. – Keith Thompson Jun 10 '19 at 01:35

1 Answers1

2

CmdParams is a pointer. You are not allocating any memory for it to point at, or even initializing it at all. That is why you are getting the Access Violation on strcpy().

Try something more like this:

char log[1024];
...
int len = strlen(log);
if (len > 49)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);
    memset(&pi, 0, sizeof(pi));

    LPSTR CmdParams = (LPSTR) malloc(14 + len + 1);
    if (CmdParams)
    {
        strcpy(CmdParams, "SendEmail.exe ");
        strcat(CmdParams, log);
        /* or:
        strcpy(CmdParams, "SendEmail.exe ");
        strcpy(CmdParams+14, log);
        */
        /* or:
        sprintf(CmdParams, "SendEmail.exe %s", log);
        */
        CreateProcessA(NULL, CmdParams, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
        free(CmdParams);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770