Pelles C forum

C language => Windows questions => Topic started by: halsten on November 15, 2008, 07:19:17 AM

Title: Weird String Truncation
Post by: halsten on November 15, 2008, 07:19:17 AM
Hello, I have the following code snippet, when the function "GetHOSTSFilePath" is called it should return the the location of the HOSTS file. However, in my case the string is truncated (no idea why).


char *GetWindowsPath() {
char szWindowsPath[MAX_PATH];

GetWindowsDirectory(szWindowsPath, sizeof(szWindowsPath));

return szWindowsPath;
}

char *GetHOSTSFilePath() {
char szBuffer[MAX_PATH];

wsprintf(szBuffer, "%s\\system32\\drivers\\etc\\hosts", GetWindowsPath());

return szBuffer;
}


Any ideas could be helpful. Thanks in advance.

Regards,
halsten
Title: Re: Weird String Truncation
Post by: JohnF on November 15, 2008, 08:31:49 AM
Your char buffers will be destroyed when the app leaves the functions, try static.

static char szWindowsPath[MAX_PATH];

and

static char szBuffer[MAX_PATH];

John
Title: Re: Weird String Truncation
Post by: halsten on November 15, 2008, 10:49:49 AM
Thanks a lot, that worked like a charm.