Thread: Using String Arrays & Pointers in a Win32 App

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile Using String Arrays & Pointers in a Win32 App

    Hello

    I wish to know how to write a string array in a Win32 Application, not a console application.
    In a console app, I would use the following code:

    Code:
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
      std::string conversation[2] = {"hi","bye"};
      cout<< conversation[0] <<endl;
      cout<< conversation[1] <<endl;
    }
    
    This would first show "hi" on one line, and "bye" on the other.
    0 would point to "hi", 1 would point to "bye".


    In the Win32 app, however, I have trouble using this method.
    Looking at the menu portion of WndProc :

    Code:
    
    switch (message)
    {
      case WM_COMMAND:
      {
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        switch (wmId)
        {
          case IDM_ABOUT:
         	::MessageBox(hWnd , _T("Message") , _T("Title") , MB_OK);
         	break;
          case IDM_EXIT:
         	DestroyWindow(hWnd);
         	break;
        }
    
    Focusing on the About section of the menu,
    if I do it this way, using the _T("") macro, it does work.
    When I try using the same string array & pointer method as I do in console programming, I get errors.
    My goal is to have something along the lines of:

    Code:
    
    switch (message)
    {
      case WM_COMMAND:
      {
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        std::string conversation[2] = {"hi" , "bye"};
        switch (wmId)
        {
          case IDM_ABOUT:
         	::MessageBox(hWnd , conversation[0] , conversation[1] , MB_OK);
         	break;
          case IDM_EXIT:
         	DestroyWindow(hWnd);
         	break;
        }
    
    I've tried using the wchar_t datatype instead. It works when I use it alone, such as in:

    Code:
    
    switch (message)
    {
      case WM_COMMAND:
      {
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        wchar_t conversation[] = L"Hi";
        switch (wmId)
        {
          case IDM_ABOUT:
         	::MessageBox(hWnd , conversation , _T("Title") , MB_OK);
         	break;
          case IDM_EXIT:
         	DestroyWindow(hWnd);
         	break;
        }
    
    However, I don't know how to make wchar_t arrays.
    When I try to make something along the lines of an array:

    Code:
    
    switch (message)
    {
      case WM_COMMAND:
      {
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        wchar_t conversation[] = {L"Hi",L"Bye"};
        switch (wmId)
        {
          case IDM_ABOUT:
         	::MessageBox(hWnd , conversation[0] , _T("Title") , MB_OK);
         	break;
          case IDM_EXIT:
         	DestroyWindow(hWnd);
         	break;
        }
    
    I get the following error message:
    error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'wchar_t' to 'LPCWSTR'

    Does anyone know how to use strings as one would in a console application?
    Compiler is Visual C++ 2008 Express Edition
    Thanks in advance

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try

    Code:
    wchar_t conversation[2][] = {L"Hi",L"Bye"};
    
    
    //or
    
    str::wstring conversation[] = {L"Hi",L"Bye"};
    
    //etc
    
    ::MessageBox(hWnd , conversation[0].c_str()  , _T("Title") , MB_OK);

    wchar_t is a short int. It is used to store a single character.

    wchar_t conversation[] ="1234567" or wchar_t conversation[8] both create a single array of shorts (8 long).

    L"Hi",L"Bye" requires two arrays of wchar_t

    I would suggest looking at text formating functions like _snwprintf()

    (and if you require UNICODE).
    Last edited by novacain; 02-22-2010 at 02:00 AM. Reason: Clarity
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile

    By having the L before "Hi" and "Bye", I get this error:
    error C2117: 'conversation' : array bounds overflow
    When I removed the L's, that error was gone.
    I think this has to do with the amount of bits being used to represent this string - I know Unicode offers much much more than ANSI - which would explain it.
    I'm going for Unicode as much as I can for this reason.

    When I wrote it as:
    wchar_t conversation[2][] = {"Hi","Bye"};
    I got:
    error C2087: 'conversation' : missing subscript

    I then changed it to
    wchar_t conversation[][2] = {"Hi","Bye"};
    to see if it was notational. It removed the error.
    The only error I have now is:
    error C2440: 'initializing' : cannot convert from 'const char [3]' to 'wchar_t'
    along with
    There is no context in which this conversion is possible
    (How encouraging...)

    and the last error I get is
    error C2228: left of '.c_str' must have class/struct/union


    Does anyone know how to otherwise index strings in a Win32 App?
    I will always try to solve my own problems as much as I can - but I can only do so much with logic when it's a language/notational issue - I either know it or I don't

    Thank you everyone for your input

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    I'll also look into the documentation as much as I can when it comes to the "error C####" issues.

    Edit:

    After some research, I discovered that including the following information helps remove some bugs:
    Code:
    ::MessageBoxW(hWnd , (LPCWSTR)_T("Hi") , (LPCWSTR)_T("Bye") , MB_OK);
    I believe LPCWSTR refers to Unicode (superior character set) and LPCSTR refers to ANSI (inferior character set).
    By using LPCWSTR I am getting Japanese text though lol.

    I'm still playing around with the code to see how I can include sets of strings with indexed elements too.
    If I find anything new I'll update this.
    Last edited by CPlus; 02-22-2010 at 05:02 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I do something along these lines... I also use the multibyte set instead of pure unicode.
    Code:
    std::string conversation[2] = {"hi","bye"};
    
    switch (message)
    {
      case WM_COMMAND:
      {
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        switch (wmId)
        {
          case IDM_ABOUT:
         	::MessageBox(hWnd , conversation[0].c_str(), conversation[1].c_str() , MB_OK);
         	break;
          case IDM_EXIT:
         	DestroyWindow(hWnd);
         	break;
        }

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Sorry I was providing examples to help you find the error, not full corrections to your code.

    Your error is that you need an array of strings (or a multi dimensional array of char/wchar) but are only declaring a single string (or single array).

    Code:
    char          szBuff[64] = {0}; //a char array capable of holding one string
    char          szArray[32][64]={0};//an array of char arrays capable of holding 32 strings
    string        sArray[32]//an array of strings
    LPCWSTR == Long (far) pointer to a wide character string. Near and far pointers have not been used since WIN16 and is simply a pointer to a particuar data type. (in CE OSs LPCWSTR etc are macros but in other OSs far / near are blank defines)

    When using the string.c_str() you have made some other error or failed to properly reference a pointer to a string (ie used a '.' instead of a '->' etc).
    I don't use wide strings (as I have no need for the extended character set unless required by the OS) and have no issues.
    Last edited by novacain; 02-22-2010 at 11:38 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile

    Thank you everyone.

    My first try was to use std::string before trying the array of character arrays.
    The reason it didn't work is because I didn't know about .c_str() .
    I was also "using namespace std;" which works for consoles but not in windows apparently.


  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by CPlus View Post
    After some research, I discovered that including the following information helps remove some bugs:
    Code:
    ::MessageBoxW(hWnd , (LPCWSTR)_T("Hi") , (LPCWSTR)_T("Bye") , MB_OK);
    I believe LPCWSTR refers to Unicode (superior character set) and LPCSTR refers to ANSI (inferior character set).
    By using LPCWSTR I am getting Japanese text though lol.

    I'm still playing around with the code to see how I can include sets of strings with indexed elements too.
    If I find anything new I'll update this.
    Never do this. You should never cast string literals unless you know what you're doing and you're not.
    It will make it silently succeed when it should not. If you use the API properly, it will compile without errors.
    The _T macro will make your string literals into Unicode or ANSI as needed, depending on your project settings.

    Quote Originally Posted by CPlus View Post
    Thank you everyone.

    My first try was to use std::string before trying the array of character arrays.
    The reason it didn't work is because I didn't know about .c_str() .
    I was also "using namespace std;" which works for consoles but not in windows apparently.

    Using namespace does work in Windows applications. In case it doesn't, you're going to have to be more specific.
    Also note that you get errors because MessageBox expects a char or wchar_t array, not std::string or std::wstring, so obviously this doesn't work.
    The .cstr() member function returns a const char* or const wchar_t* pointing to its internal array char/wchar_t array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Yeah the main problem is that I'm still learning right now.
    Silent successes are bound to happen, but I'm acquiring new bits of useful information which helps me piece information together more coherently.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There are very few differences between console mode apps and Win32 apps aside from the apparent Win32 API calls and Win32 data types that will be in the Win32 app. The main function looks a bit different and the assembly startup code will be looking for a different signature for main in Win32 and console mode.

    From a purely C++ standpoint though there are nearly no differences between these two types of applications.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making an address book in C++ with arrays inside of structures?
    By boblablabla in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2009, 10:18 PM
  2. Input on arrays and pointers
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 05-01-2008, 01:57 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

Tags for this Thread