I think the problem may be here:
#ifdef UNICODE
WCHAR tname[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, name, -1, tname, sizeof(tname) / sizeof(tname[0]));
#else
const char *tname = name;
#endif
Are you defining UNICODE or no?
If you're not, I'd suggest commenting out:
// WCHAR...
// MultiByte..