- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在尝试让 Glew 和 opengl 3.2 在 win32 上下文中使用代码块 (minGW)。我找到了一个不错的小教程 here
因为我一直在努力弄清楚在代码块中编译 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;
}
(对不起文字墙)还有其他文件,但所有错误似乎都来自这里。如果需要,我会发布其余部分。
我还没有真正看到任何与此类似的错误,所以我进行了一些谷歌搜索,发现该错误是由于编译器未设置为多字节(VS2010 中的设置)引起的。我环顾四周,但在代码块中找不到任何此类设置。这段代码只能在 VS 中使用还是我错过了什么?我担心这可能与我的链接有关,因为我过去对此有很多问题。任何帮助将不胜感激。
最佳答案
将 CreateWindowEx
更改为 CreateWindowExW
或在包含任何 header 之前定义宏 UNICODE
。
关于c++ - 无法将 'LPCWSTR {aka const wchar_t*}' 转换为 'LPCSTR {aka const char*},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11813767/
error C2440: 'initializing': cannot convert from 'TCHAR [260]' to 'std::basic_string,std::allocator>
首先。我想说,我不是 C++ 程序员。outlook 字符串在 visual studio 2015 中生成错误,并显示标题中的消息。 HWND windowHandle = (HWND)FindPr
我正在尝试检查一个 wchar_t * 是否等于另一个 wchar_t * 不区分大小写。有人可以把我放在正确的位置吗? 这是我尝试过的: wchar_t *vectored[80] = { ...
这是我的代码: wchar_t wbuffer[512]; wchar_t* wc = (wchar_t*) malloc(buffer_size); int buflen = 0;
如何正确转换这种方式? VARIANT varIndex; CString csIndex; //Index BSTR csIndex = (LPCSTR)(_bstr
我将以下代码作为我正在编写的字符串类中的私有(private)函数(它是从 wstring_convert “借来的”,并由我进行了一些修改): template std::string my_st
我有这样的东西: vector elements; 其中包含: 083938F8 083938FA 083938FC 083938FE 08393900 08393902 08393904 我想将它们
字符的签名不规范。因此,有signed char和unsigned char类型。因此,使用单个字符的函数必须使用既可以包含有符号字符又可以包含无符号字符的参数类型(此 类型选择为int),因为如果参
我在 Visual Studio C++ 2013 Community Edition 中遇到了一些奇怪的事情,这可能是编译器错误,也可能是我正在编写在没有警告的情况下进行编译的无效代码。 考虑以下片
我无法理解为什么某个隐式转换没有按我预期的那样工作。我有以下类(class) ref class ManagedWStringArrayWrapper { wchar_t** m_pointe
我有一个名为 DirToEmpty 的变量,它保存临时目录的路径。 我有另一个名为 TempBufDir 的变量,它将保持与 DirToEmpty 相同的值,但反斜杠被转义。 预期行为示例: wcha
我需要在附加 '\n' 后将多字节字符发送到套接字,但我拥有的是一个 const wchar_t*。我如何转换它? 最佳答案 如果您的问题是如何实际操作常量的内容,请考虑 const_cast . 关
我正在尝试使用语言环境方面将 wstring 转换为字符串,但我遇到了以下错误: test_facet.cpp: In function ‘int main()’: test_facet.cpp:14
我正在尝试使用 C++ 更改文件的属性我究竟做错了什么 ?代码完全拿下 https://learn.microsoft.com/en-us/windows/win32/stg/writeread-sa
我尝试将以下字符串数组传递给函数: const wchar_t *testTokens[] = { L"function", L"(",
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我尝试将阿拉伯字符存储为 wchar_t,然后将其打印到控制台,但无法使其工作。 我已将控制台设置为 UTF-8,如果我只是将阿拉伯字母输入控制台,它们就会正确显示(尽管未加入)。 我根据( How
我想存储一个包含扩展 ascii 表中字符的字符串,并打印它们。我尝试过: wchar_t wp[] = L"Росси́йская Акаде́мия Нау́к "; printf("%S", w
在C++ Visual Studio 2017中使用main函数是否错误如下: int main(int argc, wchar_t* argv[]) 因为我的程序可以接收特殊字符。 最佳答案 请阅读
我有这个运算符重载器。我的程序在创建新的 wchar_t 数组时崩溃。 myObject &operator += (const myObject &s) { wchar_t *cat = w
我是一名优秀的程序员,十分优秀!