Bob
Bob

Reputation: 13

LPCTSTR concatenation with StringCchCat

Well, I am very new at c++, so I decided to do something simple after a few projects that gave me a basic understanding of c++ (no, the hello world one doesn't count), I just want to make a folder on %appdata% and I figured out I just needed to do this

void MakeFolder()
{
LPCTSTR appdata = getenv("APPDATA");
char *appchar = getenv("APPDATA");
size_t sizeApp = sizeof(appchar) + 8;
LPCTSTR folder = "/Folder";
StringCchCat(appdata, sizeApp, folder);
CreateDirectory (appdata,NULL);
}

But it says "StringCchCat: identifier not found", I have included STDDEF.h already, and the error code doesn't change! but I am not sure the code itself would work anyway...

Upvotes: 1

Views: 3004

Answers (2)

DigitalGhost
DigitalGhost

Reputation: 793

In general, you need to include Strsafe.h to use StringCchCat

See http://msdn.microsoft.com/en-us/library/windows/desktop/ms647518(v=vs.85).aspx

The problem with using StringCchCat the way you did is the source buffer (appdata) might not be big enough to hold appdaa and folder. You could make life a lot easier for yourself by converting the LPCSTR to a std::string and adding the strings together (see How do I convert from LPCTSTR to std::string? ), then calling string.c_str() if you need a const char*.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355337

StringCchCat is declared in <strsafe.h>; you need to include that header.

But... since you are new to C++, don't mess with C strings. Use std::string:

char const* const raw_appdata = getenv("APPDATA");
if (raw_appdata == nullptr)
{
    // Handle error
}

std::string const appdata = raw_appdata;
std::string const folder  = appdata + "\\Folder";

if (CreateDirectory(appdata.c_str(), nullptr) == FALSE)
{
    // Handle error
}

Upvotes: 4

Related Questions