Author Topic: Why difference in Win32 project on C::B and VS Express 2010?  (Read 6787 times)

Offline neo1691

  • Multiple posting newcomer
  • *
  • Posts: 68
Why difference in Win32 project on C::B and VS Express 2010?
« on: December 22, 2011, 02:04:57 pm »
Hey folks.
Sorry this may sound silly.
I wanted to compile a win32 gui project using MS VS Compiler.

So i downloaded VS compiler, and in C::B created a new project with that compiler.
So when i completed the formalities of setting up the project, i got this code in main.cpp

Code
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

#include "resource.h"

HINSTANCE hInst;

BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            /*
             * TODO: Add code to initialize the dialog.
             */
            return TRUE;

        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                /*
                 * TODO: Add more control ID's, when needed.
                 */
                case IDC_BTN_QUIT:
                    EndDialog(hwndDlg, 0);
                    return TRUE;

                case IDC_BTN_TEST:
                    MessageBox(hwndDlg, "You clicked \"Test\" button!", "Information", MB_ICONINFORMATION);
                    return TRUE;
            }
    }

    return FALSE;
}


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    hInst = hInstance;

    // The user interface is a modal dialog box
    return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
}

But that was different from what i was referring to.
Then i did the same in MS VS compiler and got what i was looking for.

Code
// Sample.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Sample.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

  // TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_SAMPLE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SAMPLE));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SAMPLE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SAMPLE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

I am more interested in the Wndproc() function.
You will notice the difference in the two!!

So how i can i get the second code in Code::Blocks???

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #1 on: December 22, 2011, 02:28:14 pm »
So how i can i get the second code in Code::Blocks???
Copy and paste?! :o
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

Offline neo1691

  • Multiple posting newcomer
  • *
  • Posts: 68
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #2 on: December 22, 2011, 03:14:27 pm »
So how i can i get the second code in Code::Blocks???
Copy and paste?! :o

thats a temperory solution and may gibe errors related to header files :-\

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #3 on: December 22, 2011, 03:25:52 pm »
thats a temperory solution and may gibe errors related to header files :-\
I guess I don't really get what you mean. If you want to to develop for Win32 under C::B you'll need to setup a project accordingly. This includes (surely) compiler and linker settings as required (like include paths). Then you can do whatever you want inside your Win32 code, including copying portion you need from the source you mentioned. Why not?!
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

Offline neo1691

  • Multiple posting newcomer
  • *
  • Posts: 68
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #4 on: December 22, 2011, 03:32:32 pm »
Will surely try what you say and if i encounter any problems then i woll report.
Thanks

Offline neo1691

  • Multiple posting newcomer
  • *
  • Posts: 68
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #5 on: December 23, 2011, 09:47:04 am »
I copied the same code in C::B that worked in visual studio 2010 express. first error was that it could not find kernel.lib. I fixed that by adding the lib in the linker settings. Next error was cannot find rc.exe so i added the path containing rc.exe in additional paths in compiler settings.

Now it says this

||=== hello windows using cb, Debug ===|
resource.h|1|fatal error RC1015: cannot open include file 'windows.h'.|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


But the same code compiled well in MS VS 10

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #6 on: December 23, 2011, 09:50:39 am »
I copied the same code in C::B that worked in visual studio 2010 express. first error was that it could not find kernel.lib. I fixed that by adding the lib in the linker settings. Next error was cannot find rc.exe so i added the path containing rc.exe in additional paths in compiler settings.

Now it says this

||=== hello windows using cb, Debug ===|
resource.h|1|fatal error RC1015: cannot open include file 'windows.h'.|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


But the same code compiled well in MS VS 10
It should also compile, if you use msvc10 as compiler in VC::B, but will in almost any cases fail, if you use (e.g.) MinGW as compiler.

Offline neo1691

  • Multiple posting newcomer
  • *
  • Posts: 68
Re: Why difference in Win32 project on C::B and VS Express 2010?
« Reply #7 on: December 23, 2011, 09:53:49 am »
I copied the same code in C::B that worked in visual studio 2010 express. first error was that it could not find kernel.lib. I fixed that by adding the lib in the linker settings. Next error was cannot find rc.exe so i added the path containing rc.exe in additional paths in compiler settings.

Now it says this

||=== hello windows using cb, Debug ===|
resource.h|1|fatal error RC1015: cannot open include file 'windows.h'.|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


But the same code compiled well in MS VS 10
It should also compile, if you use msvc10 as compiler in VC::B, but will in almost any cases fail, if you use (e.g.) MinGW as compiler.

I have both MinGW and MS VS 10.
This time my compiler is set to MS VS 10 (in c::b)
It compiled a simple Hello world program, however it had given one warning.

But now i want to compile a win32 gui project and it gives this error..

PS: The default compiler is VS 10 and the compiler used for the project is also VS 10