WinMain()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hunderpanzer
    New Member
    • Jul 2007
    • 60

    WinMain()

    Hey guys. I'm trying to learn some windows programming, but i'm getting some errors with the code:


    [CODE=cpp]#include <windows.h> // include the basic windows header file

    // the entry point for any Windows program
    int WINAPI WinMain(HINSTAN CE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nShowCmd)
    {
    // create a "Hello World" message box using MessageBox()
    MessageBox(NULL ,
    L"Hello World!",
    L"Just another Hello World program!");
    MB_ICONEXCLAMAT ION | MB_OK);

    // return 0 to Windows
    return 0;
    }
    [/CODE]

    The errors I get (using Dev-C++) are


    In function `int WinMain(HINSTAN CE__*, HINSTANCE__*, CHAR*, int)':

    cannot convert `const wchar_t*' to `const CHAR*' for argument `2' to `int MessageBoxA(HWN D__*, const CHAR*, const CHAR*, UINT)'



    Alright, I've been using c++ in DOS (i think) so I need some guidance here.

    Thanks in advance.
  • Cucumber
    New Member
    • Sep 2007
    • 90

    #2
    When you use the L literal before a string in your code, the compiler understands you are willing to create an Unicode string (wchar_t *) rather than an ANSI string (char *).

    The function MessageBox, which is secretly defined as MessageBoxA inside the windows headers in this case, must receive ANSI strings as parameters, not Unicode strings. Thats why the compiler complains.

    If you want your code to compile either remove the L literals to use ANSI strings or tell the compiler you are creating an Unicode Win32 application using

    #define UNICODE 1
    before including the windows.h header.

    When defining such preprocessing variable, the windows header will secretly define the function MessageBox as MessageBoxW, such function receive Unicode strings as parameters.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by Cucumber
      #define UNICODE 1
      before including the windows.h header.
      You should not do this. To undefine it, you have to change your code. Instead there is a character set property setting for your project. Set that propertry to the correct value.

      Originally posted by Cusumber
      When defining such preprocessing variable, the windows header will secretly define the function MessageBox as MessageBoxW, such function receive Unicode strings as parameters.
      There is no secret definition. MessageBox is a macro. Based on the character set, the macro resolves to MessageBoxA or MessageBoxW. Your code with the L in front of the literal will not work with MessageBoxA. Further, the L specifes a string of wchar_t and these characters are not Unicode characters unless they contain Unicode values.

      Microsoft provides a series of mappings called the TCHAR mappings that you should be using. These macros correctly switch between ASCII and Unicode without your having to change your code.

      You #include <tchar.h>

      The message bis code should be:
      [code=c]
      MessageBox(NULL ,
      TEXT("Hello World!"),
      TEXT("Just another Hello World program!"),
      MB_ICONEXCLAMAT ION | MB_OK);
      [/code]

      Read this. Be sure to follow the links.

      Comment

      • Cucumber
        New Member
        • Sep 2007
        • 90

        #4
        I know its not a secret definition, just wanted to keep the explanation clear without going into the macro declaration details.

        Comment

        • Hunderpanzer
          New Member
          • Jul 2007
          • 60

          #5
          Arghh.


          I still can't get it to work.


          I reading an online DirectX tutorial, but nothing makes sense or works when I type it in.


          WHY is DirectX sooo complicated ?!

          Comment

          • oler1s
            Recognized Expert Contributor
            • Aug 2007
            • 671

            #6
            DirectX uses COM. If you aren’t familiar with the paradigms COM introduces, either because you simply haven’t seen COM in any form, or just don’t have that much experience, then DirectX will be overwhelming initially.

            Comment

            • Hunderpanzer
              New Member
              • Jul 2007
              • 60

              #7
              Originally posted by oler1s
              DirectX uses COM. If you aren’t familiar with the paradigms COM introduces, either because you simply haven’t seen COM in any form, or just don’t have that much experience, then DirectX will be overwhelming initially.

              It's extremely overwhelming. And yeah, I haven't seen COM before.

              How do I get more experience with COM ? Online tutorials ? Books ?

              Comment

              • Cucumber
                New Member
                • Sep 2007
                • 90

                #8
                COM+ Programming: A Practical Guide Using Visual C++ and ATL
                Pradeep Tapadiya
                Publisher: Prentice Hall PTR

                That book is the best COM book I have come across.
                Yes, I know the tittle says "COM+", but the whole first part of the book is dedicated to explain the COM basis (what is it and why was designed the way it is).

                Comment

                • Hunderpanzer
                  New Member
                  • Jul 2007
                  • 60

                  #9
                  Originally posted by Cucumber
                  COM+ Programming: A Practical Guide Using Visual C++ and ATL
                  Pradeep Tapadiya
                  Publisher: Prentice Hall PTR

                  That book is the best COM book I have come across.
                  Yes, I know the tittle says "COM+", but the whole first part of the book is dedicated to explain the COM basis (what is it and why was designed the way it is).


                  I see. Thanks a lot for your help.

                  Comment

                  • Ninjaamos
                    New Member
                    • Dec 2008
                    • 2

                    #10
                    I don't know if it's proper because I am fairly new to C++ as well as COM but the method I used was simply to remove the L (literal?) declaration.
                    So instead of:
                    MessageBox(NULL ,
                    L"Hello World!",
                    L"Just another Hello World program!");
                    MB_ICONEXCLAMAT ION | MB_OK);
                    Just use:
                    MessageBox(NULL ,
                    "Hello World!",
                    "Just another Hello World program!");
                    MB_ICONEXCLAMAT ION | MB_OK);
                    Once again, not sure if it's proper but it is functional.

                    Comment

                    • Ninjaamos
                      New Member
                      • Dec 2008
                      • 2

                      #11
                      As a side note I was having the same problem exactly until I read some of the replies here, so thank you all.

                      Comment

                      • weaknessforcats
                        Recognized Expert Expert
                        • Mar 2007
                        • 9214

                        #12
                        No one so far on this thread has mentioned TCHAR.

                        TCHAR is a macro that switches betrween char and wchar_t based on the character set used by the compiler. When you set Visual Studio.NET to "Use Multi-byte character set", TCHAR is a char. When you set Visual Studio.NET to "Use Unicode character set", TCHAR is a wchar_t.

                        Using an L in front of a literal makes the literal a wchar_t but this does not support Unicode. Use the TEXT or _T macros.

                        MessageBox is itself a macro that swtiches between MessageBoxA and MessageBoxW.

                        I really suggest tou research TCHAR in MSDN before writing a lot fo code.

                        Comment

                        Working...