I Have a Problem in "char *" & "LPCWSTR"

Started by
5 comments, last by nfries88 7 years, 11 months ago

Hi, I'm studying OpenGL Programming in http://nehe.gamedev.net/

I'm in Tutorial #0 right now. (http://nehe.gamedev.net/tutorial/creating_an_opengl_window_(win32)/13001/)

I read the whole page, and coded all source code in my Visual Studio 2015.

I have a problem in all strings.

For example,

MessageBox(NULL, "Release Of DC And RC Failed", "SHUTDOWN ERROR", MB_OK | MB_ICONINFORMATION);

there I have 2 errors in between \" respectively.

I found the solution that include tchar.h header file and modify the code like :

MessageBox(NULL, _T("Release Of DC And RC Failed"), _T("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION);

then, there are no red underline now, but I still have a trouble.

in the code:

if (!(hWnd = CreateWindowEx(dwExStyle, // Extended Style For The Window
"OpenGL", // Class Name
title, // Window Title
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN | // Required Window Style
dwStyle, // Selected Window Style
0, 0, // Window Position
WindowRect.right - WindowRect.left, // Calculate Adjusted Window Width
WindowRect.bottom - WindowRect.top, // Calculate Adjusted Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Don't Pass Anything To WM_CREATE
here the title is the string variable.
I changed it to :
_T(title)
but as you think, it was not the solution.
how can I get through this problem? Please HELP me.
Advertisement
The W in LPCWSTR stands for "Wide" and usually if you see errors about that it means you're mixing Unicode and Multibyte strings in your program due to incorrect settings or incorrect string literals.

You can either try making everything consistently Unicode (put L prefixes on all of your string literals or the _T macro like you've already tried) or consistently Multibyte (open your project properties and change "Character Set" to "Use Multibyte Character Set".

If that doesn't help then the problem is much deeper and you might need to post more code.

New VS2015 C++ windows projects default to Unicode, I believe. Older tutorials like NeHe probably assume the older multibyte default.

I have to really, really recommend you learn modern OpenGL. NeHe is very out of date with anything modern and little of it will translate over. Look for OpenGL 3 or 4 tutorials, there are plenty online.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

I have to really, really recommend you learn modern OpenGL. NeHe is very out of date with anything modern and little of it will translate over. Look for OpenGL 3 or 4 tutorials, there are plenty online.

As Mike said, you'd better learn modern OpenGL instead of the old fixed pipeline like on NeHe.

Have a look at http://www.learnopengl.com/.

EDIT:

Using WinAPI directly is also not the nicest way to do it, since its platform specific. I'd suggest to use something like GLFW (or SDL/Allegro/SFML but they are higher level), but its your choice.

EDIT 2:

Can I ask why downvote? Maybe its not what the OP asked but its important for him to know.

Hey, have a look at my Telegram channel about programming: www.telegram.me/theprogrammingart

Thanks to Everybody! As you guys suggested, I should learn modern OpenGL haha.

Anyway, I solved my problem not modifying the codes, I changed my project property so not using character set in Unicode, but Multibyte. Who must study old OpenGL in some reasons, this would help you guys~

Incidentally, if you want to represent either LPSTR and LPWSTR depending on compiler settings you can use LPTSTR.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Most Win32 functions that take a string argument actually have two different versions for two different string types - ANSI strings and WIDE strings.

ANSI strings use some sort of multi-byte encoding (or just plain old ASCII) and resolve to char*. WIDE strings use UTF-16 encoding and resolve to wchar_t *.

For example, there is no actual MessageBox function. It's actually a trick that the Windows headers play - when there are ANSI and WIDE versions of a function, the windows headers automatically choose the right function for MessageBox to resolve to, based on your project settings. It looks something like this:



#ifdef _UNICODE

#define MessageBox MessageBoxW

#else

#define MessageBox MessageBoxA

#endif

Usually I just use the ANSI version, unless I have reason to believe I'll translate my project into a language other than English. Working with WIDE strings is more work. :)

This topic is closed to new replies.

Advertisement