NO

Author Topic: Weird String Truncation  (Read 3718 times)

halsten

  • Guest
Weird String Truncation
« 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).

Code: [Select]
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

JohnF

  • Guest
Re: Weird String Truncation
« Reply #1 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

halsten

  • Guest
Re: Weird String Truncation
« Reply #2 on: November 15, 2008, 10:49:49 AM »
Thanks a lot, that worked like a charm.