C++ 无法转换';LPCWSTR{aka const wchar_t*}';至';LPCSTR{aka const char*}

C++ 无法转换';LPCWSTR{aka const wchar_t*}';至';LPCSTR{aka const char*},c++,winapi,mingw,codeblocks,glew,C++,Winapi,Mingw,Codeblocks,Glew,我一直在尝试让Glew和OpenGL3.2在win32环境中使用代码块(minGW)。我找到了一个很好的小教程 由于我一直在试图弄清楚是否可以在代码块中编译glew,所以我想在编写教程之前先尝试一下源代码,看看它是否可行 在稍微调整代码之后,我尝试编译,并得到了几个我以前从未见过的错误。具体如下: |In function 'bool createWindow(LPCWSTR, int, int)':| |73|error: cannot convert 'LPCWSTR {aka const

我一直在尝试让Glew和OpenGL3.2在win32环境中使用代码块(minGW)。我找到了一个很好的小教程

由于我一直在试图弄清楚是否可以在代码块中编译glew,所以我想在编写教程之前先尝试一下源代码,看看它是否可行

在稍微调整代码之后,我尝试编译,并得到了几个我以前从未见过的错误。具体如下:

|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|
我的代码是

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;
}
包括
#包括
#ifndef GLEW_静态
#定义GLEW_静态
#endif//GLEW_静态
#包括
#包括
//使用名称空间std;
//
//int main()
//{

//cout我从来没有使用过minGW,所以请带上一大粒盐。(VS Express是免费使用的,顺便说一句。)


Unicode/Ascii决策主要由Unicode定义控制。因此,如果您定义Unicode 1,或者可能在编译命令行中传递Unicode 1,则很有可能解决您的问题。

CreateWindowEx
更改为
CreateWindowExW
,或者在包含任何he之前定义宏
Unicode
aders.

嘿,感谢您的帮助,它似乎完成了这项任务。我通过“mbstowcs_s(&convertedChars,wcstring,origsize,orig,_TRUNCATE);”收到了另一个错误这是对windows函数的调用吗?它说这是未声明的。@IPhantasmI:据我所知,
mbstowcs\u s
是VC++特有的。使用MinGW,只需使用。输入变量是否需要更改。我在猜测,因为从简单地将函数调用替换为您建议的调用,我会遇到很多错误。我没有太多经验ce使用windows api,所以我不太确定他是如何完成变量转换的。@IPhantasmI:
mbstowcs
是C标准的一部分,它不是windows特有的。包括
和更改,例如
mbstowcs(转换字符、wcstring、origsize、orig、\u TRUNCATE)
to
convertedChars=1+std::mbstowcs(wcstring,orig,newsize);
mbstowcs_
的文档是,因此您可以将其与
mbstowcs
进行对比。非常感谢您提供了巨大的帮助,并且不仅仅是回答这个问题。对于帮助,似乎一直是问题所在。