cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}
Asked Answered
N

2

7

I have been trying to get Glew and opengl 3.2 working with code blocks(minGW) in a win32 context. i found a nice little tutorial here

As i had been trying to work out if compiling glew in codeblocks was actually possible i wanted to try the source out before doing the tutorial to see if it would work.

after tweaking the code slightly I tried to compile and got several errors that i have never seen before. they are as follows

|In function 'bool createWindow(LPCWSTR, int, int)':|
|73|error: cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' in assignment|
|80|error: cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'|
|In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':|
|105|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|
|110|error: '_TRUNCATE' was not declared in this scope|
|110|error: 'mbstowcs_s' was not declared in this scope|

My code is

include <iostream>
#include <Windows.h>

#ifndef GLEW_STATIC
#define GLEW_STATIC
#endif //GLEW_STATIC



#include <GL/glew.h>
#include <GL/wglew.h>

//using namespace std;
//
//int main()
//{
//    cout << "Hello world!" << endl;
//    return 0;
//}


#include "opengl_3.h"

OpenGLContext openglContext; // Our OpenGL Context class

bool running = true; // Whether or not the application is currently running

HINSTANCE hInstance; // The HINSTANCE of this application
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Standard window callback

/**
    WndProc is a standard method used in Win32 programming for handling Window messages. Here we
    handle our window resizing and tell our OpenGLContext the new window size.
*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    switch (message) {
        case WM_SIZE: // If our window is resizing
        {
            openglContext.reshapeWindow(LOWORD(lParam), HIWORD(lParam)); // Send the new window size to our OpenGLContext
            break;
        }

        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
    }

    return DefWindowProc(hWnd, message, wParam, lParam);
}

/**
    createWindow is going to create our window using Windows API calls. It is then going to
    create our OpenGL context on the window and then show our window, making it visible.
*/
bool createWindow(LPCWSTR title, int width, int height) {
    WNDCLASS windowClass;
    HWND hWnd;
    DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;

    hInstance = GetModuleHandle(NULL);

    windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    windowClass.lpfnWndProc = (WNDPROC) WndProc;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = hInstance;
    windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    windowClass.hbrBackground = NULL;
    windowClass.lpszMenuName = NULL;
    windowClass.lpszClassName = title;

    if (!RegisterClass(&windowClass)) {
        return false;
    }

    hWnd = CreateWindowEx(dwExStyle, title, title, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, width, height, NULL, NULL, hInstance, NULL);

    openglContext.create30Context(hWnd); // Create our OpenGL context on the given window we just created

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    return true;
}

/**
    WinMain is the main entry point for Windows based applications as opposed to 'main' for console
    applications. Here we will make the calls to create our window, setup our scene and then
    perform our 'infinite' loop which processes messages and renders.
*/
int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR    lpCmdLine,
                     int       nCmdShow) {
    MSG msg;

    /**
        The following 6 lines of code do conversion between char arrays and LPCWSTR variables
        which are used in the Windows API.
    */
    char *orig = "OpenGL 3 Project"; // Our windows title
    size_t origsize = strlen(orig) + 1;
    const size_t newsize = 100;
    size_t convertedChars = 0;
    wchar_t wcstring[newsize];
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);

    createWindow(wcstring, 500, 500); // Create our OpenGL window

    openglContext.setupScene(); // Setup our OpenGL scene

    /**
        This is our main loop, it continues for as long as running is true
    */
    while (running)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // If we have a message to process, process it
            if (msg.message == WM_QUIT) {
                running = false; // Set running to false if we have a message to quit
            }
            else {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else { // If we don't have a message to process
            openglContext.renderScene(); // Render our scene (which also handles swapping of buffers)
        }
    }

    return (int) msg.wParam;
}

(Sorry about the wall of text) there are also other files but all the errors seem to becoming from here. ill post the rest if needed.

I haven't really seen any errors similar to this so i did some googling and found out that the error was caused by the compiler not being set to multi byte(a setting in VS2010). i did some looking around and couldnt find any such settings in codeblocks. is this code only usable in VS or have I missed something? im worried it might be something to do with my linking due to me having a lot of issues with this in the past. any help would be appreciated.

Ness answered 5/8, 2012 at 3:39 Comment(0)
O
10

Change CreateWindowEx to CreateWindowExW or define the macro UNICODE before including any headers.

Owl answered 5/8, 2012 at 4:56 Comment(5)
Hey thanks for the help it seems to have done the trick. im getting another error though "mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);" would this be a call to a windows function? it says it is undeclared.Ness
@IPhantasmI : As far as I know, mbstowcs_s is VC++-specific. With MinGW, just use std::mbstowcs instead.Owl
Would the input variables need changing. im guessing so as im getting a lot of errors from simply replacing the function call to what you suggested. I don't have much experience with the windows api so im not really sure exactly how he is accomplishing the variable conversion.Ness
@IPhantasmI : mbstowcs is part of the C standard, it is in no way Windows-specific. Include <cstdlib> and change e.g. mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); to convertedChars = 1 + std::mbstowcs(wcstring, orig, newsize);. The documentation for mbstowcs_s is here, so you can contrast it with mbstowcs.Owl
Thanks so much you have been a great help and have gone above and beyond just answering the question.Ness
C
2

I've never used minGW, so take this with a huge grain of salt. (VS Express is free to use, BTW.)

The Unicode/Ascii decision is largely controlled by the UNICODE define. So if you #define UNICODE 1, or possibly pass that in on your compile commandline, there's a good chance that would solve your problem.

Curdle answered 5/8, 2012 at 4:56 Comment(1)
ty for the help seems to have been the issues.Ness

© 2022 - 2024 — McMap. All rights reserved.