News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

Strange error (win32 API)

Started by seel, February 01, 2007, 10:15:39 PM

Previous topic - Next topic

seel

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...

Pecan

#1
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.





seel

Full code of mdi_unit.c: http://pastebin.ca/337163


The functions where the errors are in

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

Biplab

I think there's a cast problem. Change the following lines-
LPSTR pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
To
LPSTR pszFileText = (LPSTR) GlobalAlloc(GPTR, dwFileSize + 1);

and

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

TDragon

Quote from: Biplab on February 02, 2007, 06:03:06 PM
I think there's a cast problem.
QFT. The original code demonstrates "C++-style casting" (functional notation) -- valid in C++ but not C.
[url="https://jmeubank.github.io/tdm-gcc/"]https://jmeubank.github.io/tdm-gcc/[/url] - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Biplab

Quote from: TDragon on February 02, 2007, 06:10:07 PM
Quote from: Biplab on February 02, 2007, 06:03:06 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

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

...

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

   InitCommonControls();

   g_hInst = hInstance;

....

Biplab

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

You sir are now officialy god. :lol: Thanks for the help. ;)

Biplab

Quote from: seel on February 02, 2007, 06:47:15 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)

Quote from: seel on February 02, 2007, 06:47:15 PM
Thanks for the help. ;)

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

MortenMacFly

Quote from: seel on February 02, 2007, 06:36:11 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: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]