Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Met problems with my first win32 program

Met problems with my first win32 program

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelptutorialquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bloodwinner
    wrote on last edited by
    #1

    code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.

    R M 2 Replies Last reply
    0
    • B bloodwinner

      code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      The linker thinks this is a console program whose entry point is the main function. You need to tell it that it is a windows program. In the compiler options, preprocess definitions section you will probably see a macro "_CONSOLE" defined. You need to remove that and add in the macro _WINDOWS there.

      B 1 Reply Last reply
      0
      • R Rick York

        The linker thinks this is a console program whose entry point is the main function. You need to tell it that it is a windows program. In the compiler options, preprocess definitions section you will probably see a macro "_CONSOLE" defined. You need to remove that and add in the macro _WINDOWS there.

        B Offline
        B Offline
        bloodwinner
        wrote on last edited by
        #3

        when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.

        R J M 3 Replies Last reply
        0
        • B bloodwinner

          when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          There is obviously more to it than just the macro and I'm not certain what they are exactly. What I would do is rename the folder your project is in to Lesson1_Old or something like that, create a new project, and then copy the old code files that you changed into the new project's directory (do NOT copy any project files - only .cpp, h, or .rc files) That should be very easy and take only a minute or two. Sorry I couldn't be more helpful. BTW - it could be informative to compare the new project files generated with the old ones that do not work.

          T 1 Reply Last reply
          0
          • R Rick York

            There is obviously more to it than just the macro and I'm not certain what they are exactly. What I would do is rename the folder your project is in to Lesson1_Old or something like that, create a new project, and then copy the old code files that you changed into the new project's directory (do NOT copy any project files - only .cpp, h, or .rc files) That should be very easy and take only a minute or two. Sorry I couldn't be more helpful. BTW - it could be informative to compare the new project files generated with the old ones that do not work.

            T Offline
            T Offline
            TylerD75
            wrote on last edited by
            #5

            I'm just a beginner my self, and in the process of writing my first application. So this might be a shot in the dark, so to speak... But are you trying to display a msgbox in a console application? I was under the impression that that would be more or less impossible (unless you program the actual code for displaying such a thing). If you intend to write something in a console application, shouldn't you just print it to the console? Console::WriteLine(L"Hello World!"); If you want to use a msgbox, I think you either have to make a Win32 Application or a MFC application? This is because in an empty Console Application you don't have window handling etc... Someone please correct me if I'm wrong? TylerD75

            R 1 Reply Last reply
            0
            • T TylerD75

              I'm just a beginner my self, and in the process of writing my first application. So this might be a shot in the dark, so to speak... But are you trying to display a msgbox in a console application? I was under the impression that that would be more or less impossible (unless you program the actual code for displaying such a thing). If you intend to write something in a console application, shouldn't you just print it to the console? Console::WriteLine(L"Hello World!"); If you want to use a msgbox, I think you either have to make a Win32 Application or a MFC application? This is because in an empty Console Application you don't have window handling etc... Someone please correct me if I'm wrong? TylerD75

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              no, the original poster is trying make a windows app but the linker thinks he wants a console app.

              T 1 Reply Last reply
              0
              • B bloodwinner

                when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.

                J Offline
                J Offline
                Justin Tay
                wrote on last edited by
                #7

                Go to your project's properties. Configuration Properties > Linker > System > SubSystem Change from Console to Windows.

                1 Reply Last reply
                0
                • R Rick York

                  no, the original poster is trying make a windows app but the linker thinks he wants a console app.

                  T Offline
                  T Offline
                  TylerD75
                  wrote on last edited by
                  #8

                  If that is the case, how come he writes this? Quote: when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work. From this I think he's trying to convert a console application into a Win32 Application, without adding the window handling that is automatically added when creating a new Win32 Application Project. This seem a bit strange to me, but as I stated before: I'm a newbee! Cheers, TylerD75

                  1 Reply Last reply
                  0
                  • B bloodwinner

                    when I include this C++ source file into a "Win32 Application" project, it works; when include it into a "Win32 Console Application" project, it fails; my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application. I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    The entry point of a console app is main() not WinMain(). Change it to main() (with the right prototype, of course).

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    1 Reply Last reply
                    0
                    • B bloodwinner

                      code: // Windows Programming Tutorial Series #include "stdafx.h" #include int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MessageBox (NULL, "Hello World! This is my first WIN32 program", "Lesson 1", MB_OK); return 0; } 2 errors: Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/a.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. a.exe - 2 error(s), 0 warning(s) Who can make some suggestions please? I guess it may be I did not include the lib well.

                      M Offline
                      M Offline
                      mostafa_pasha
                      wrote on last edited by
                      #10

                      if you wanna program for win32 application you must drive to functions itself. 1)WinMain 2)WinPorc try this code :(thanks for Ivor Horton Books. you can download source code & see chapter 7 ) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS WindowClass; // Structure to hold our window's attributes static char szAppName[] = "OFWin"; // Define window class name HWND hWnd; // Window handle MSG msg; // Windows message structure // Redraw the window if the size changes WindowClass.style = CS_HREDRAW | CS_VREDRAW; // Define our procedure for message handling WindowClass.lpfnWndProc = WindowProc; WindowClass.cbClsExtra = 0; // No extra bytes after the window class WindowClass.cbWndExtra = 0; // structure or the window instance WindowClass.hInstance = hInstance; // Application instance handle // Set default application icon WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION); // Set window cursor to be the standard arrow WindowClass.hCursor = LoadCursor(0, IDC_ARROW); // Set gray brush for background color WindowClass.hbrBackground = static_cast(GetStockObject(GRAY_BRUSH)); WindowClass.lpszMenuName = 0; // No menu, so no menu resource name WindowClass.lpszClassName = szAppName; // Set class name // Now register our window class RegisterClass(&WindowClass); // Now we can create the window hWnd = CreateWindow( szAppName, // the window class name "A Basic Window the Hard Way", // The window title WS_OVERLAPPEDWINDOW, // Window style as overlapped CW_USEDEFAULT, // Default screen position of upper left CW_USEDEFAULT, // corner of our window as x,y... CW_USEDEFAULT, // Default window size CW_USEDEFAULT, // .... 0, // No parent window 0, // No menu hInstance, // Program Instance handle 0 // No window creation data ); ShowWindow(hWnd, nCmdShow); // Display the window UpdateWindow(hWnd); // Cause window client area to be drawn // The message loop while(GetMessage(&msg, 0, 0, 0) == TRUE)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups