Hi, I've been doing basic programming with Java, and have recently started to familiarize with c++ so forgive me if this is a really noob question I'm using Microsoft Visual Studio 2008 and looking at code examples from the internet when I try to compile some code I usually get the errors for the line ofn.lpstrFilter = "Text Files (*.txt)\0*.ufc\0All Files (*.*)\0*.*\0"; error C2440: '=' : cannot convert from 'const char [56]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast which i can use the TEXT casting thing to overcome and for the line HWND hwnd = CreateWindow(sClassName,(LPCWSTR)TEXT(sAppName),WS_OVERLAPPED | WS_MINIMIZEBOX |WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_DLGFRAME | WS_BORDER, CW_USEDEFAULT,CW_USEDEFAULT,275,137,0,0,hinst,0); error C2440: '=' : cannot convert from 'const char [56]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast which i have not been able to solve yet. I was wondering why do I have to edit code which should already be working? I thought perhaps its to with using C code and adding it to a C++ project, but these examples were taken from C++ programs. Thx soong
There's a lot of casting involved in Windows programming, due to the generic nature of the interfaces. As long as you're certain the data pointed to is correct, there's nothing wrong with a cast. There's a lot less casting nowadays than there used to be, but casting from different string types to each other is still unfortunately quite common. Don't double cast though, as in (LPCWSTR)TEXT(szAppName). You should only ever need one cast. You don't say where the code comes from, but my guess is that it's either from older Windows programs where the rules aren't as strict or they're from non-Windows programs.