Search code examples
visual-c++mfcwindows-ce

How to Generate a text file in WinCE 6.0 application using MFC?


I am trying to create a text file through my code. The file is created but its extension is different(not .txt). Then on searching i came to know that this can be because of MFC runtimes.

I searched for the MFC Runtimes in my C:\program Files\Windows CE tools\SDK but i am not able to find the MFC folder there. What should i do? from where should i include MFC Runtimes?

here's the text file generation code which i am using:

 void CFormRight::OnBnClickedButtonTextfile()
 {
    CFile File;
    char cFileAddr[100] = {"My Device\\Label.txt"};
    File.Open((LPCTSTR)cFileAddr, CFile::modeCreate | CFile::typeBinary |CFile::modeWrite | CFile::shareDenyNone);
    File.Write("Hello World", 15);
    File.Close();       
 }

Solution

  • If you're just looking for the DLLs, try looking on your development machine here:

    %PROGRAM_FILES%\Microsoft Visual Studio 9.0\VC\ce\dll

    That gets you the actual libraries. Including them in the OS image can be done a variety of ways. Typically you'd add them to your platform or project BIB file.

    Even with all of that, though, I don't think it's going to solve your problem of a file extension. If you're creating a file and it shows up, just without an extension, it has nothing to do with MFC being there or not, it has to do with either your code, or the way you're determining there is no extension (is "hide file extensions" turned on in Explorer?). To solve that problem, we'd need to see code.

    ** Edit **

    Windows CE is heavily biased toward Unicode. Nearly all Win32 APIs only have the Unicode variant exposed, therefore your code should also lean toward Unicode, meaning string you pass around that will end up at API calls should be Unicode.

    Second, you should not ignore the compiler when it complains. The cast you have in there I bet was due to a compiler complaint, and it's just incorrect. If your original code was this (note the lack of cast on the first parameter):

    File.Open(cFileAddr, CFile::modeCreate | CFile::typeBinary |
                         CFile::modeWrite | CFile::shareDenyNone);
    

    Then you would get a compiler error:

    error C2664: 'CFile::Open' : cannot convert parameter 1 from 'char [100]' to 'LPCTSTR'

    That's because under Windows CE, Open is looking for a wide (Unicode) string. You have an ANSI string. You cannot convert from ANSI to Unicode through a simple direct cast like you did. Yes, the compiler will quit complaining, but it gives bad behavior. You can cast an int to a char[] too, but that doesn't mean it will work for the API.

    So change your code to use wide strings and all will work:

    CFile File;
    CFileException ex;
    wchar_t cFileAddr[100] = TEXT("My Device\\Label.txt");
    if(!File.Open(cFileAddr, CFile::modeCreate | CFile::typeBinary | 
                         CFile::modeWrite | CFile::shareDenyNone))
    {
        wchar_t error[1024];
        ex.GetErrorMessage(error, 1024);
        cout << "Error opening file: ";
        cout << error;
        return;
    }
    
    File.Write("ID Technologies", 15);
    File.Close();       
    

    Note the use of wchar_t, the initialization using the TEXT macro, and the lack of the cast in the Open call.

    EDIT 2

    I added error handling in the code above, but really you should learn to read the documentation and how to debug. This is a pretty basic usage scenario, and knowing how to look for errors is critical.