Menu

Wide Character Strings

2008-03-26
2012-09-26
  • Fred Harris

    Fred Harris - 2008-03-26

    Wide chaacter strings confuse me. Below is about the simplest possible SDK style Win Api program that creates a window without even a "Hello, World". The only catch is I modified everything to use wide character strings (wchar_t) instead of char strings. I went through and changed the Api functions to their wide character counterparts by appending a 'W' to them, i.e.,

    CreateWindowEx() to CreateWindowExW,

    ect.

    It works fine except for one thing that has me puzzled. If you examine the program you'll see the window's caption should be "Form1", and indeed, this is the name of the class for the window class specified in WinMain(). However, when the simple window appears on the screen its caption if 'F', not 'Form1'. Now I fully can understand what's happening, I just don't know why. The second byte of the wide character English (ANSI) string is no doubt a NULL, and string output is halting there. But why? If you examine the program you'll see I used wchar_t everyehere. And I believe all the Api functions that take wide character strings have a 'W' appended to them?

    Here is the program. Too bad some kind of code formatting doesn't work here!

    /Simple Window/Form with No Child Windows/
    //http://sourceforge.net/forum/forum.php?forum_id=48211

    include <windows.h>

    LRESULT CALLBACK WindowProcedure(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    if(message==WM_CLOSE)
    {
    PostQuitMessage(0);
    return 0;
    }

    return DefWindowProc(hwnd, message, wParam, lParam);
    }

    int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrev,LPSTR lpszArg,int iCmdShow)
    {
    wchar_t szClass[8]=L"Form1";
    WNDCLASSEXW winclass;
    MSG messages;
    HWND hMain;

    winclass.hInstance=hIns;
    winclass.lpszClassName=szClass;
    winclass.lpfnWndProc=WindowProcedure;
    winclass.style=CS_HREDRAW | CS_VREDRAW;
    winclass.cbSize=sizeof(WNDCLASSEXW);
    winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    winclass.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor=LoadCursor(NULL,IDC_ARROW);
    winclass.lpszMenuName=NULL;
    winclass.cbClsExtra=0;
    winclass.cbWndExtra=0;
    winclass.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
    RegisterClassExW(&winclass);
    hMain=CreateWindowExW(0,szClass,szClass,WS_OVERLAPPEDWINDOW,100,100,400,350,0,0,hIns,0);
    ShowWindow(hMain,iCmdShow);
    while(GetMessage(&messages,NULL,0,0))
    {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
    }

    return messages.wParam;
    }

    One final thought! I know someone is going to tell me to use TEXT("Form1"). Its just so horible looking I can't hardly stand it.

     
    • Fred Harris

      Fred Harris - 2008-03-26

      Wow Musamitchell! That did it! Had me mystified. Thanks!

      I've a significant Win CE program I'd like to take a shot at converting to Win32, so I'd better figure out this stuff!

      Fred

       
    • Musa

      Musa - 2008-03-26

      Try using the wide version of the default window proc, CreateWindowEx propably uses a SendMessage to set the window caption text.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.