SetWindowText Error ;(

This is a discussion on SetWindowText Error ;( within the Windows Programming forums, part of the Platform Specific Boards category; I have basically copied word for word the below code: Code: BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) { HANDLE hFile; BOOL ...

  1. #1
    NickName-Here
    Guest

    SetWindowText Error ;(

    I have basically copied word for word the below code:

    Code:
    BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
    {
        HANDLE hFile;
        BOOL bSuccess = FALSE;
        hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
            OPEN_EXISTING, 0, NULL);
        if(hFile != INVALID_HANDLE_VALUE)
        {
            DWORD dwFileSize;
            dwFileSize = GetFileSize(hFile, NULL);
            if(dwFileSize != 0xFFFFFFFF)
            {
                LPSTR pszFileText;
                pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
                if(pszFileText != NULL)
                {
                    DWORD dwRead;
                    if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
                    {
                        pszFileText[dwFileSize] = 0;
                        if(SetWindowText(hEdit, pszFileText))
                            bSuccess = TRUE;
                    }
                    GlobalFree(pszFileText);
                }
            }
            CloseHandle(hFile);
        }
        return bSuccess;
    }
    When i try to compile i get this error:

    Code:
    error C2440: '=' : cannot convert from 'void *' to 'char *'
            Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Error executing cl.exe.
    How is this wrong??
    Or is there some setting i have wrong??

    Thanks

  2. #2
    NickName-Here
    Guest
    Sorry..

    I will also add that the error is line 163.

    which is the code:

    Code:
    pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Try changing the code line:
    Code:
    pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
    into this:
    Code:
    pszFileText = (char *) GlobalAlloc(GPTR, dwFileSize + 1);
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    You're getting it on this line, correct?

    pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);

    The reason is that your code is valid C code, but you're compiling it as C++ code, and it's not valid C++. The valid C++ line would be:

    pszFileText = static_cast<LPSTR>(GlobalAlloc(GPTR, dwFileSize + 1));

    or the equivalent line in the older (and less recommended) C-style format:

    pszFileText = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);

    If you are trying to compile this as C code, you need to tell the compiler that, though. C code will not always compile under C++, so you need to choose one language or the other, and make the compiler do it. Right now, your compiler is compiling in C++ mode, and the code is C code. No changes are needed if you switch to compiling C.

    Edit: Damn, 2 posts when I was writing this...

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Cat, you absolutly corret about the C mode issuse, but:

    The valid C++ line would be:

    pszFileText = static_cast<LPSTR>(GlobalAlloc(GPTR, dwFileSize + 1));

    or the equivalent line in the older (and less recommended) C-style format:

    pszFileText = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);
    Why is it less recommended to use (LPSTR)?
    Last edited by Aidman; 07-19-2003 at 07:01 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    (<cast to type>) is the old method of casting. The new casting operators provide more flexibility & control over the casting process and also make the code more readable.

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, there are 3 distinct different types of casts that can be accomplished by (typename). The compiler chooses which one is appropriate, and it might not be the one you think you want. There are 4 C++ casts (static_cast, dynamic_cast, reinterpret_cast, and const_cast) and when using this, you tell the compiler which one you want, and the compiler tells you if it's a legal cast.

    Besides the fact that dynamic_cast has no other form, it's safer to use the new over the old -- it allows the compiler to do more checking, and it lets you know that you're doing exactly what you think you're doing. You won't accidently get a static_cast when trying to do a reinterpret_cast, for example.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Is it possible to use that casting method for function pointers, more preccessly casting from a variable pointer to a function pointer?

    sorry NickName-Here for changing the subject
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Any cast that could be done with (typename) can be done with static_cast, reinterpret_cast, or const_cast (or a combination of more than one of the above).

    Depending on your compiler, reinterpret_cast could convert between pointers to variables and functions at will; exactly how reinterpret_cast works is up to implementations to decide (thus code which needs it is less portable than code which uses only static_cast, dynamic_cast, and const_cast).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21