Whats wrong here !

Using VS2005 and c++

My compiler defaults to MessageBoxW so I have to use the following syntax:

MessageBox(NULL, (LPCWSTR)L"Hello!", (LPCWSTR)L"Greetings", MB_OK);

I assume the above is correct. It works.

now

I declare a PSTR variable as below:

PSTR FunctionName = (PSTR) (something ...)
there is no problem getting the function name in the program.
I can see the name when I step thru the code.

but when I cast it like this I get garbage for the function name.

MessageBoxA(NULL, (LPCWSTR)FunctionName, (LPCWSTR)L"SetHook", MB_OK);

I am confused about changing from unicode to ascii.

If the statement 'PSTR FunctionName = ' declares FunctionName as a pointer to an ascii string
why cant I cast that strng to LPCWSTR.

What am I doing wrong?

If you have plain old ascii text and you want to use the W function you can do the following using the _T() macro.

MessageBox(NULL, _T("Hello"), _T("Greetings"), MB_OK);


or you can simply call the A version of the function:
MessageBoxA(NULL, "Hello", "Greetings", MB_OK);
And no - you cannot simply cast a plain old ascii string to a LPCWSTR the way you are trying to do (well the cast will go through) but the result will not make sense - as you have found out.
Thank you very much for your reply.

OK now I understand about MessageBoxA vs MessageBox.

My problem remains that I want to use MessageBox to debug a worker thread.
Sometimes I would like to see a string and other times a pointer address.

so if the variable of interest (VOI) is declared elsewhere in the program as

(1) PSTR VOI ; do I use MessageBoxA(NULL , (PSTR) VOI,"Greetings",MB_OK)

and if VOI is declared

(2) LPWSTR VOI; do I use MessageBox(NULL , (LPWSTR) VOI,"Greetings",MB_OK)

and if VOI is declared

(3) char *VOI; do I use do I use MessageBoxA(NULL , (PSTR) VOI,"Greetings",MB_OK) to see the string?

and when VOI is declared

(4) char *VOI and I would like to see the address of VOI in the message box how should I do that.

(5) LPCVOID VOI; and I would like to see the string pointed to by VOI in the messageBox?

I am having problems knowing how to show the string and how to show the pointer address.

Thanks in advance.
A bit more about windows string pointers.
I have counted 11 different type of string pointers in windows as follows;
1
2
3
4
5
6
7
8
9
10
11
    PWSTR       //always wide string
    PCWSTR      //always wide string
    LPWSTR      //Always wide string
    LPCWSTR     //always wide string
    PSTR        //always 8 bit
    PCSTR       //Always 8 bit
    LPSTR       //Always 8 bit
    LPCSTR      //always 8 bit
    PTSTR       //LPWSTR if UNICODE defined, LPSTR if UNICODE not defined
    PCTSTR      //LPCWSTR if UNICODE defined, LPCTSTR if UNICODE not defined
    LPTSTR      //LPWSTR if UNICODE defined, LPSTR if UNICODE not defined 

If you are using string literals you simply use the _T macro and that will take care of the Ascii (A) or Wide (W) functions versions.

For Example MessageBox(NULL, _T("Hello"), _T("Andy"), MB_OK);

if for some reason the pointer to your text has been already been declared as PSTR or LPWSTR then you use MessageBoxA or MessageBoxW:
1
2
3
4
    PSTR pstr="Andy";//just ordinary text for  MessageBoxA
    LPWSTR lpwstr = L"Michael";//L means make wide string - needed for MessageBoxW
    MessageBoxA(NULL, pstr, "GuestGulkan", MB_OK);//just ordinary text for MessageBoxA
    MessageBoxW(NULL, lpwstr, L"GuestGulkan", MB_OK); //L means make wide string - needed for MessageBoxW 


If you are using char* or wchar_t (like the address of buffers, or arrays) then use MessageBoxA or MessageBoxW as appropriate:

1
2
3
4
5
6
7
8
       //8 bit char pointers/buffer    
    char myArray[]= "Testing, testing";
    MessageBoxA(NULL, myArray, "GuestGulkan", MB_OK);

    //wide char pointerss/buffer
    wchar_t myArray2[] = L"Testing2, Testing2";//L tells compiler to create a widestring
    wchar_t *myName = L"GuestGulkan";
    MessageBoxW(NULL, myArray2, myName, MB_OK); 


As for printing out an address. Here is an example
1
2
3
4
5
6
7
    char buffer[256]={0};
    int *p;
    int A = 3;
    p = &A;
    
   sprintf_s(buffer , 255, "Address of variable A is: Hex :%x (Decimal: %d)",p,&A);//p and &A are the same thing
    MessageBoxA(NULL, buffer, "GuestGulkan", MB_OK);



I get the felling I have confused you even more..
Thanks again.
You have not confused me.
Far from it.
This too has been printed for reference.

One final question:
I know the 'sprintf' function.
what is sprintf_s ? why _s is added?
There are a number of c runtime functions that take a user supplied pointer to buffer and do copy to/copy from opertions on that buffer.
These are functions like printf, scanf, sprintf......and so on.
These functions never did any checks on the supplied buffer and in this day and age it is considered a high security risk.
In the Microsoft products, MS has introduced new versions of these functions - they have the same name nut with _s at the end. This means (enhanced) security.
The old functions are still there, but when you use them, the compiler will warn you about them and suggest that you use the secure versions instead.
Topic archived. No new replies allowed.