none
WCHAR?

    Question

  • Hello guys,

    Lets say I have a variable
    WCHAR something[MAX_PATH] = L"Something";

    Now, If I want to change variable to contain L"Something else" compiler wont let me.
    error C2440: '=' : cannot convert from 'const wchar_t [9]' to 'WCHAR'

    How can I change content of variable something[MAX_PATH]?
    Friday, November 28, 2008 9:16 AM

Answers

All replies

  • why don't you define it like that:
    wchar_t *something = new wchar_t[MAX_PATH];
    i don't think you'll get that problem using that
    btw another comment
    you can also #include <tchar.h>
    at your project, then it can be more comfortable instead putting L b4 every string 
    you can do this _T("string")
    Friday, November 28, 2008 9:29 AM
  • No, I need to have variable as WCHAR and not wchar_t.
    Friday, November 28, 2008 9:34 AM
  • the compiler treat them the same :|
    typedef wchar_t WCHAR
    Friday, November 28, 2008 9:42 AM
  • For some reason, wchar_t variable doesnt work...

    So, how could I do it WCHAR way?


    Friday, November 28, 2008 9:50 AM
  • another solution that i can think of is using multi-byte characer set
    and you won't need to use WCHAR at all (as far as i know)
    you can use char *something;
    Friday, November 28, 2008 9:55 AM
  • Try this way. It works.

    WCHAR something[MAX_PATH] = L"Something";

    WCHAR *p = something;

    p = L"Something else";

     


     

    Friday, November 28, 2008 9:55 AM
  • Didnt work:
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2040: 'p' : 'int' differs in levels of indirection from 'WCHAR *'
    error C2440: 'initializing' : cannot convert from 'const wchar_t [44]' to 'int'
    Friday, November 28, 2008 10:09 AM
  • can you post the code here?
    Friday, November 28, 2008 10:16 AM
  • WCHAR something[MAX_PATH] = L"something\\something.jpg";
    WCHAR *p = something[MAX_PATH];
    p = L"something\\else.jpg";

    If I have wchar_t, than many parts in code fail to compile, for example this one:
    case IDC_SOMETHING:
                ZeroMemory(&ofn, sizeof(ofn));

        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = NULL;
        ofn.lpstrFilter = L"something";
        ofn.lpstrFile = something;
        ofn.nMaxFile = MAX_PATH;
        ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
        ofn.lpstrDefExt = L"jpg";
       
        if(GetOpenFileName(&ofn))
        {
    Friday, November 28, 2008 10:24 AM
  • Why do you specify the size again when you assign the array to the pointer?

    WCHAR *p = something[MAX_PATH]; should be as below:

    WCHAR *p = something;

    Friday, November 28, 2008 10:37 AM
  • same errors with assigned size or not.
    Friday, November 28, 2008 11:14 AM
  • Do you have uniocode enabled in your project?

    If so then, OPENFILENAMEW will be be used else OPENFILENAMEA will be used for the variable 'ofn'

    Since you are using the WCHAR data type, check "ofn" is of which type.

    You need to use OPENFILENAMEW.

    Below is the code that compiled successfully:

    WCHAR something[MAX_PATH] = L"something\\something.jpg";
    WCHAR *p = something;
    p = L"something\\else.jpg";

    OPENFILENAMEW ofn;

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = L"something";
    ofn.lpstrFile = something;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = L"jpg";


    Note that i have use OPENFILENAMEW.
    Friday, November 28, 2008 11:34 AM
  • Its not that the only problem. I use some framework which supports WCHAR only, so its impossible for me to switch, unless changing 1000 WCHARs to wchar_t.

    I just want to know for any simple way how to change content of WCHAR.

    And yes, I use unicode.
    Friday, November 28, 2008 11:50 AM
  • I don't understand why you want to change WCHARs to wchar_t. It is because WCHAR is already defined as wchar_t in "winnt.h".  You shouldn't be having any problems here.

    Friday, November 28, 2008 12:11 PM
  • So How can I change contents of WCHAR something[MAX_PATH]?
    Friday, November 28, 2008 1:15 PM
  • Ive tried with 2x WCHAR as well:
    something[MAX_PATH] = somethingelse[MAX_PATH];

    It compiles well, but application crushes. I get tons of errors in debugger.

    I know that something's value can be changed, since it changes using "OpenFilename". I just dont know where is the trick!
    Friday, November 28, 2008 1:50 PM
  • Friday, November 28, 2008 2:15 PM
  • I cant believe this. I told 100 times I have to use WCHAR!

    I will search for help on other forums.
    Friday, November 28, 2008 2:47 PM
  •  
    WCHAR buf[100];  
    int len = swprintf( buf, 100, L"%s", L"Hello world" );;  
    wprintf(L"%s\n", buf); 

    Friday, November 28, 2008 3:56 PM
  • Ok, this did the job:
    wcscpy(something, L"somethingelse");

    Problem solved!

    • Marked as answer by jan1024188_1 Friday, November 28, 2008 4:03 PM
    Friday, November 28, 2008 4:03 PM
  • Also,

    Ive tried with 2x WCHAR as well:  
    something[MAX_PATH] = somethingelse[MAX_PATH];  
     
    It compiles well, but application crushes. I get tons of errors in debugger. 

    The reason this crashes is because by saying something like "something[MAX_PATH]", you are referencing the character (WCHAR), not the string (WCHAR* or WCHAR[]). And also, something[MAX_PATH] and somethingelse[MAX_PATH] are out of range as well!
    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    Friday, November 28, 2008 5:11 PM