Author Topic: Strange error (win32 API)  (Read 11020 times)

seel

  • Guest
Strange error (win32 API)
« on: February 01, 2007, 10:15:39 pm »
Okay, i tried to compile the MDIapp example that comes with dev-cpp just to test windows.h in codeblocks. It compiles flawless in dev-cpp but not in codeblocks, i even copied the files and put them in a .cbp and i get this strange error:
(mdi_unit.c is the main file)
mdi_unit.c: In function `LoadFile':
mdi_unit.c:37: error: syntax error before "LPSTR"
mdi_unit.c: In function `SaveFile':
mdi_unit.c:69: error: syntax error before "LPSTR"

And here are the lines that don't work (they both look pretty much the same,except other variable name):
LPSTR pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));

What's up with that LPSTR? If you need to see more of the code (and don't have dev-cpp installed) i could upload the files for you...

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2875
Re: Strange error (win32 API)
« Reply #1 on: February 01, 2007, 11:54:00 pm »
How in this world are we suppose to answer such a question without seeing the code or the project .cbp?

It says the err occured "before" LPSTR.

How about clicking on the code icon above (when posting a msg) and inserting some text before and after the error. Also turn on full compiler logging.

   //-- Full Compile Logging --
   Main Menu->Settings->Compiler and Debugger->"Other"->Compiler logging = "Full command line".

Paste the relevant results here.




« Last Edit: February 01, 2007, 11:56:41 pm by Pecan »

seel

  • Guest
Re: Strange error (win32 API)
« Reply #2 on: February 02, 2007, 05:05:21 pm »
Full code of mdi_unit.c: http://pastebin.ca/337163


The functions where the errors are in
Code
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
   HANDLE hFile;
   BOOL bSuccess = FALSE;

   hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
   if(hFile != INVALID_HANDLE_VALUE)
   {
      DWORD dwFileSize;
      dwFileSize = GetFileSize(hFile, NULL);
      if(dwFileSize != 0xFFFFFFFF)
      {
         LPSTR pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
         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;
}

BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
   HANDLE hFile;
   BOOL bSuccess = FALSE;

   hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
      CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
   if(hFile != INVALID_HANDLE_VALUE)
   {
      DWORD dwTextLength;
      dwTextLength = GetWindowTextLength(hEdit);
      if(dwTextLength > 0)// No need to bother if there's no text.
      {
         LPSTR pszText=LPSTR(GlobalAlloc(GPTR, dwTextLength + 1));
         if(pszText != NULL)
         {
            if(GetWindowText(hEdit, pszText, dwTextLength + 1))
            {
               DWORD dwWritten;
               if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
                  bSuccess = TRUE;
            }
            GlobalFree(pszText);
         }
      }
      CloseHandle(hFile);
   }
   return bSuccess;
}


These are all build messages i get and i turned on full command line:

Project   : Win32 Application
Compiler  : GNU GCC Compiler (called directly)
Directory : C:\Program\CodeBlocks\PROJEKT\dslua sdk\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-gcc.exe       -IC:\Program\CodeBlocks\include -c mdi_unit.c -o .objs\mdi_unit.o
mdi_unit.c: In function `LoadFile':
mdi_unit.c:37: error: syntax error before "LPSTR"
mdi_unit.c: In function `SaveFile':
mdi_unit.c:68: error: syntax error before "LPSTR"
Process terminated with status 1 (0 minutes, 2 seconds)
2 errors, 0 warnings

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Strange error (win32 API)
« Reply #3 on: February 02, 2007, 06:03:06 pm »
I think there's a cast problem. Change the following lines-
Code
LPSTR pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
To
Code
LPSTR pszFileText = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);

and

Code
LPSTR pszText=LPSTR(GlobalAlloc(GPTR, dwTextLength + 1));
To
Code
LPSTR pszText= (LPSTR) GlobalAlloc(GPTR, dwTextLength + 1);
Be a part of the solution, not a part of the problem.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Strange error (win32 API)
« Reply #4 on: February 02, 2007, 06:10:07 pm »
I think there's a cast problem.
QFT. The original code demonstrates "C++-style casting" (functional notation) -- valid in C++ but not C.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Strange error (win32 API)
« Reply #5 on: February 02, 2007, 06:22:18 pm »
I think there's a cast problem.
QFT. The original code demonstrates "C++-style casting" (functional notation) -- valid in C++ but not C.

Two possible solutions.
* Change the file extension to cpp.
or
* Add -x c++ as compiler option.
Be a part of the solution, not a part of the problem.

seel

  • Guest
Re: Strange error (win32 API)
« Reply #6 on: February 02, 2007, 06:36:11 pm »
Oh, it was that simple eh? doh :lol:

Well, now i get another error...

Project   : Win32 Application
Compiler  : GNU GCC Compiler (called directly)
Directory : C:\Program\CodeBlocks\PROJEKT\dslua sdk\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-g++.exe       -IC:\Program\CodeBlocks\include -c mdi_unit.cpp -o .objs\mdi_unit.o
mingw32-g++.exe    -LC:\Program\CodeBlocks\lib -o "C:\Program\CodeBlocks\PROJEKT\dslua sdk\DSLuaSDK.exe" .objs\mdi_unit.o  .objs\mdi_res.res       -lgdi32 -luser32 -lkernel32  -mwindows
.objs\mdi_unit.o:mdi_unit.cpp:(.text+0x2d0): undefined reference to `InitCommonControls@0'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 12 seconds)
0 errors, 0 warnings

And here is where the function is located in the code
Code
...

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
   LPSTR lpszCmdParam, int nCmdShow)
{
   MSG  Msg;
   WNDCLASSEX WndClassEx;

   InitCommonControls();

   g_hInst = hInstance;

....

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Strange error (win32 API)
« Reply #7 on: February 02, 2007, 06:41:11 pm »
Goto Project > Build Options menu. Then goto Linker tab. Add comctl32 to the Link libraries.
Be a part of the solution, not a part of the problem.

seel

  • Guest
Re: Strange error (win32 API)
« Reply #8 on: February 02, 2007, 06:47:15 pm »
You sir are now officialy god. :lol: Thanks for the help. ;)

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: Strange error (win32 API)
« Reply #9 on: February 02, 2007, 06:51:08 pm »
You sir are now officialy god. :lol:

Don't elevate me to God. I won't be able to sleep tonight. ;) (It's time to sleep for me)

Thanks for the help. ;)

You are most Welcome.  :)
Be a part of the solution, not a part of the problem.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: Strange error (win32 API)
« Reply #10 on: February 02, 2007, 08:45:03 pm »
.objs\mdi_unit.o:mdi_unit.cpp:(.text+0x2d0): undefined reference to `InitCommonControls@0'
For the future: If you google on stuff like that you will usually catch a million pages that describe what to do. If stuff like this happens (and you are obviously developing for Win32), you can also refer to the MSDN. There the InitCommonControls method is described next to the information what *header files* (the compiler) and what *library files* (the linker) to be used with it.
Look for example here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/common/functions/initcommoncontrols.asp
I hope this will help at some point sooner or later... ;-)
With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ