1

I need to copy one file to windir, So I do:

TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);

to get the windir.

char newLocation[]="text.txt"; // how to add the windir here?
BOOL stats=0;
CopyFile(filename, newLocation, stats);

My problem is, I want to add the windir value before the text.txt in char newLocation[].

Any ideas?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
RGS
  • 4,062
  • 4
  • 31
  • 67
  • For the last 10+ years, UAC has prevented standard users from copying files to the Windows or Program Files directory. It started being discouraged in XP, and was totally prohibited when Vista was released. How could you have missed this news (that's been written about here at least 1000 times previously, with every Windows-related programming language tag in existence AFAIK)? – Ken White Jun 02 '16 at 22:44
  • yeah, it needs administrator permissions to work... so user can decide if he wants it to work or not. – RGS Jun 02 '16 at 22:48

1 Answers1

1

Did you try concatenating the strings, like this?

#include <stdlib.h>
#include <string.h>

char* concat(char *s1, char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    //in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

If that won't work, give wcscat() a shot!


Sources:

  1. How do I concatenate two strings in C?
  2. What is difference between TCHAR and WCHAR?
  3. C++ Combine 2 Tchar
Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    exactly! concatenate windir variable before the "text.txt" – RGS Jun 02 '16 at 22:37
  • my fault! It gives me an error: [Error] invalid conversion from 'void*' to 'char*' [-fpermissive] – RGS Jun 02 '16 at 22:41
  • this line: char *result = malloc(strlen(s1)+strlen(s2)+1); – RGS Jun 02 '16 at 22:43
  • Hmm then you should cast the result of `malloc` like this: `char *result = (char*) malloc(strlen(s1)+strlen(s2)+1);` and let me know! – gsamaras Jun 02 '16 at 22:45