This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

GlobalAlloc() question. Also Solution to Resources in mingw32


This is PROBABLY one of those answers that stares you in the face, it's just
hard to see after 24 hours of not sleeping. But I've been staring at this
for the past 12, so I'm going to give in, and risk flaming.

I've got a function that's supposed to load a file (below), and inside of
that is a call to GlobalAlloc(). I borrowed it from a tutorial, the same one
I seem to borrow the rest of my code from <wink>. Now...in the Tutorial it
works just fine. But the minute I port it in to MY version of the Tutorial
code (only the variable names have been changed, to protect the innocent).
In fact, NONE of the variable names in this function are global, so NONE of
them have been changed. It should work flawlessly. But I get the following
error.

winmain.cpp:43: ANSI C++ forbids implicit conversion from 'void *' in
initialization

BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
  BOOL bSuccess = FALSE;
  HANDLE hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ,
      (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, NULL, NULL);
  if(hFile != INVALID_HANDLE_VALUE) {
    DWORD dwFileSize = GetFileSize(hFile, (LPDWORD)NULL);
    if(dwFileSize != 0xFFFFFFFF) {
/*LINE 43*/
      LPSTR pszFileText = GlobalAlloc(GPTR, dwFileSize + 1);
/*LINE 43*/
      if(pszFileText != NULL) {
        DWORD dwRead;
        if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) {
          pszFileText[dwFileSize] = 0;  // Null terminator
          if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // It
worked!
        }
        GlobalFree(pszFileText);
      }
    }
    CloseHandle(hFile);
  }
  return bSuccess;
}

The only include file I have right now is <windows.h>, as it is in the
tutorial. What's the reasoning on a silly thing like this?

----

In answer to my own question earlier about resource files in GCC, it can be
done, I've since learned. Here are the commands.

windres.exe -i resourcefile.rc -o resourcefile.o
gcc.exe resourcefile.o main.cpp otherfile.cpp otherfile2.cpp etcetera.cpp -o
testprogram.exe -mwindows

Thanks for the help, everyone.

Aciel
aciel@speakeasy.net
www.rapedangel.net - RAN Free Speech Forum


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]