none
I must be an idiot

    Question

  • Hello, I'm hoping I can get some help from a Visual Studio expert, because I think that's where my problem lies, but I'm not sure.  Here's the dilly:

    Trying to use SetCurrentDirectory/FindFirstFile/etc.
    On a Windows XP machine running VS 2003 this works without a hitch.
    On another Windows XP machine (w/ a bunch of security ____ on it) running VS 2005, I get no love.
    I need it to work on the 2nd machine b/c it also needs to run on an x64 box, and VS 2005 conveniently comes w/ an x64 compiler.

    One interesting not is that when I do GetCurrentDirectory on the VS2003 box, it gives the whole "C:\Doc...\User\My Doc...\Projects\..."
    On the VS2005 machine, it just says "c".

    Any takers?
    Wednesday, December 03, 2008 7:28 PM

Answers

  • See here for einaros' FAQ on Unicode vs MBCS/SBCS.

    Unicode makes TCHAR become wchar_t, which is two bytes, while MBCS (multi-byte character set) and SBCS (single-byte character set) make TCHAR become char. See here for MSDN documentation on this.

    In the code you've posted, you have some glaring errors related to TCHAR usage. You use TCHAR, but some function calls are char specific (printf specifically). I assume you put in the casts (TCHAR* and LPCSTR) to avoid compilation errors, but this is not the proper fix -- the proper fix is to use the TEXT or _T macros so string literals are of the proper TCHAR type, and to use character setting agnostic functions instead of char specific functions. See here for MSDN documentation on this.

    Windows uses Unicode internally, so ideally your code should be written in such a way that your app will work when built for Unicode.

    EDIT: Also see here for some highly relevant documentation on LPCSTR vs LPCWSTR vs LPCTSTR.
    • Edited by ildjarn Wednesday, December 03, 2008 8:21 PM typo
    • Proposed as answer by Mozok Wednesday, December 03, 2008 8:21 PM
    • Marked as answer by jogold Wednesday, December 03, 2008 8:26 PM
    Wednesday, December 03, 2008 8:15 PM

All replies

  • Sounds like a UNICODE vs MBCS/SBCS issue -- specifically, like you're treating some wchar_t data as char.

    Try going into your project settings and changing the character set from unicode to multi-byte.
    Wednesday, December 03, 2008 7:33 PM
  • More code needed to examine your problem. Can you post some of it?
    Wednesday, December 03, 2008 7:35 PM
  • WTF??  ildjarn, I tried your suggestion and it worked.  Holy cow.  Do you mind explaining to me what MBCS/SBCS is?
    If you couldn't tell, I'm new to the Windows API

    And for grins, here is the code in all its simplistic glory.

    1#include <stdio.h> 
    2#include <windows.h> 
    3 
    4#define BUFSIZE MAX_PATH 
    5 
    6 
    7int main(int argc) 
    8
    9    WIN32_FIND_DATA FindFileData; 
    10 
    11    TCHAR* path = (TCHAR*) "c:\\path"; 
    12 
    13    printf("Path: %s\n", path); 
    14 
    15    TCHAR Buffer[BUFSIZE]; 
    16    GetCurrentDirectory(BUFSIZE, Buffer); 
    17    printf("Current Directory %s\n", Buffer); 
    18 
    19    printf("Set Current Dir %d\n", SetCurrentDirectory(path)); 
    20 
    21    GetCurrentDirectory(BUFSIZE, Buffer); 
    22    printf("Current Directory %s\n", Buffer); 
    23 
    24    return 0; 
    25 
    26    LPCSTR file = (LPCSTR) "c:\\path\\file.inf"; 
    27 
    28    if (FindFirstFile( file,&FindFileData)==INVALID_HANDLE_VALUE)    { 
    29        printf("File not found: %s\n", file); 
    30        printf ("FindFirstFile failed (%d)\n", GetLastError()); 
    31        return 2; // Install Failure 
    32    } else { 
    33        printf("File found!\n"); 
    34    } 
    35 
    36    return 0; 
    37



    Wednesday, December 03, 2008 8:04 PM
  • See here for einaros' FAQ on Unicode vs MBCS/SBCS.

    Unicode makes TCHAR become wchar_t, which is two bytes, while MBCS (multi-byte character set) and SBCS (single-byte character set) make TCHAR become char. See here for MSDN documentation on this.

    In the code you've posted, you have some glaring errors related to TCHAR usage. You use TCHAR, but some function calls are char specific (printf specifically). I assume you put in the casts (TCHAR* and LPCSTR) to avoid compilation errors, but this is not the proper fix -- the proper fix is to use the TEXT or _T macros so string literals are of the proper TCHAR type, and to use character setting agnostic functions instead of char specific functions. See here for MSDN documentation on this.

    Windows uses Unicode internally, so ideally your code should be written in such a way that your app will work when built for Unicode.

    EDIT: Also see here for some highly relevant documentation on LPCSTR vs LPCWSTR vs LPCTSTR.
    • Edited by ildjarn Wednesday, December 03, 2008 8:21 PM typo
    • Proposed as answer by Mozok Wednesday, December 03, 2008 8:21 PM
    • Marked as answer by jogold Wednesday, December 03, 2008 8:26 PM
    Wednesday, December 03, 2008 8:15 PM
  • Thanks for the education and helpful links!
    Wednesday, December 03, 2008 8:26 PM