Compile Error in Visual C++ 2005 express

This is a discussion on Compile Error in Visual C++ 2005 express within the C++ Programming forums, part of the General Programming Boards category; Hi there; I entered a simple C++ program from a book on video games, and I'm getting a compile error. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    Compile Error in Visual C++ 2005 express

    Hi there;

    I entered a simple C++ program from a book on video games, and I'm getting a compile error. You can see the compiler error message down below, along with the text of the program.

    Does anyone know what the problem is? I should note that the author used MS Visual C++ 2003, whereas I'm using the 2005 Express edition, so I'm guessing there might be something different between the two versions. Also, I compiled this in Dev C++ and it worked fine. However, I'm trying to improve my Express skills, so I'd love to learn how to fix this.

    Thanks,
    FremontKnight

    ************************************************** ****
    This is the code of the program:
    Code:
     
    //#define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    //#include <windowsx.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    	LPSTR lpCmdLine, int nShowCmd)
    {
        MessageBox(NULL, "Motoko Kusanagi has hacked your system!",
            "Public Security Section 9", MB_OK | MB_ICONEXCLAMATION);
    
        return 0;
    
    }

    Here is the compiler feedback:
    ************************************************** **
    ------ Build started: Project: Chapter 2, Configuration: Debug Win32 ------
    >Compiling...
    >Chapter 2 Program.cpp
    >c:\documents and settings\paul\my documents\visual studio 2005\projects\chapter 2\chapter 2\chapter 2 program.cpp(14) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [40]' to 'LPCWSTR'
    > Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    >Build log was saved at "file://c:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Chapter 2\Chapter 2\Debug\BuildLog.htm"
    >Chapter 2 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    You're compiling with UNICODE enabled.

    Each "string" in your program should be written as TEXT("string")
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Another way to fix this is to press Alt F7, which brings up a box. Then go to Configuration Properties -> General, and the third box from the bottom (Character Set), change that from 'Use Unicode Character Set' which is the default to 'Use Multi-Byte Character Set'. That should fix it so you don't have any of those errors, although you have to do that for each project.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Thanks for the suggestions, Salem & mikeman118.
    When do either of these, I get a new error. Your thoughts?

    Thank you!


    Code:
    #include <windows.h>
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    	LPSTR lpCmdLine, int nShowCmd)
    {
        MessageBox(NULL, TEXT("Motoko Kusanagi has hacked your system!"),
            TEXT("Public Security Section 9"), MB_OK | MB_ICONEXCLAMATION);
        return 0;
    }
    ***************************************
    Compiling...
    1>Chap2 Code.cpp
    1>Linking...
    1>Chap2 Code.obj : error LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _WinMain@16
    1>C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Chapter 2 Program\Debug\Chapter 2 Program.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Chapter 2 Program\Chapter 2 Program\Debug\BuildLog.htm"
    1>Chapter 2 Program - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    are you building a console or graphical application?

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    It's supposed to be a pop-up warning box, so I guess you would call it a graphical application.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I think robwhit meant what typeof project did you create? There are different types when you create a VC++ project.

    The error suggests you aren't linking to the library that has the MessageBox implementation. I would think a Windows Application project would automatically link to that, but maybe not.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    I selected Win32 Console Application as the project template. As options, I chose Windows as an application type, and selected empty project.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    I think for WinMain you need to select a Win32 Application not a Win32 Console Application.

    To make that code work as is in your project you'd probably have to do:

    Code:
    #include <windows.h>
    
    int main()
    {
        MessageBox(NULL, TEXT("Motoko Kusanagi has hacked your system!"),
            TEXT("Public Security Section 9"), MB_OK | MB_ICONEXCLAMATION);
        return 0;
    }
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    SDK?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ 2005 Express - Resources?
    By ulillillia in forum Tech Board
    Replies: 4
    Last Post: 04-11-2007, 03:03 PM
  2. Visual c++ 2005 express edition to run .c files
    By the_fall_guy in forum C Programming
    Replies: 4
    Last Post: 04-05-2007, 12:33 PM
  3. Visual C++ 2005 Express Edition: Breakpoints not working
    By Bird Killer in forum C++ Programming
    Replies: 7
    Last Post: 03-31-2006, 09:38 AM
  4. Visual C++ 2005 Express Edition
    By BestGameMovie in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2005, 01:49 PM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 03:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21