c - (C) 不兼容的指针类型 [LPCWSTR]

标签 c winapi types

当我尝试使用 unicode LPCWSTR 类型时,我收到到处都是不兼容的指针类型

无论我做什么,我都完全卡住了,试图寻找答案很多次,但仍然没有希望!

我的代码:

#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Mouse.h"
#include "Keyboard.h"
//#define APP_WindowClassName "MOUSE_CLICKER"
//#define APP_WindowTitle "Mouse Clicker"

LRESULT CALLBACK app_WindowProcedure (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProcW(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.

    WNDCLASSEXW main_WindowClass = { };
    main_WindowClass.cbSize = sizeof(WNDCLASSEXA);
    main_WindowClass.cbClsExtra = 0;
    main_WindowClass.cbWndExtra = 0;
    main_WindowClass.hInstance = hInstance;
    main_WindowClass.lpfnWndProc = app_WindowProcedure;
    main_WindowClass.lpszClassName = TEXT("MOUSE_CLICKER");
    main_WindowClass.lpszMenuName = NULL;
    main_WindowClass.hbrBackground  = (HBRUSH) (TEXT(COLOR_BACKGROUND));
    main_WindowClass.hCursor = LoadCursorW (NULL, TEXT(IDC_ARROW));
    main_WindowClass.hIcon = LoadIconW(NULL, TEXT(IDI_APPLICATION));
    main_WindowClass.hIconSm = LoadIconW(NULL, TEXT(IDI_APPLICATION));
    main_WindowClass.style = CS_DBLCLKS;

    //CS_HREDRAW | CS_VREDRAW a


    if (RegisterClassExW(&main_WindowClass) == 0) {
        printf ("[CRITICAL] main_WindowClass cannot be registered!");
        return -1;
    }

    HWND main_WindowHandle = CreateWindowExW (0, TEXT("MOUSE_CLICKER"), TEXT("MouseClicker"), WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);

    if (main_WindowHandle == NULL) {
        return -1;
    }

    ShowWindow(main_WindowHandle, nCmdShow);

    printf("Unicode: %d", IsWindowUnicode(main_WindowHandle));

    MSG ProcessingMessage;
    while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
        TranslateMessage(&ProcessingMessage);
        DispatchMessage(&ProcessingMessage);
    }
    return ProcessingMessage.wParam;

}

我的构建日志图片链接:

warnings

PS:我是 C 语言的初学者(仍在学习),关于我做错了什么的易于理解的描述性信息会很好。

PS2:为避免混淆这是纯 C,不是 C++

解决方案代码:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include "Mouse.h"
#include "Keyboard.h"

LRESULT CALLBACK app_WindowProcedure (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProcW(hwnd, uMsg, wParam, lParam);

    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.

    WNDCLASSEXW main_WindowClass = { };
    main_WindowClass.cbSize = sizeof(WNDCLASSEXA);
    main_WindowClass.cbClsExtra = 0;
    main_WindowClass.cbWndExtra = 0;
    main_WindowClass.hInstance = hInstance;
    main_WindowClass.lpfnWndProc = app_WindowProcedure;
    main_WindowClass.lpszClassName = L"MOUSE_CLICKER";
    main_WindowClass.lpszMenuName = NULL;
    main_WindowClass.hbrBackground  = (HBRUSH) (COLOR_BACKGROUND);
    main_WindowClass.hCursor = LoadCursorW (NULL, (LPCWSTR) IDC_ARROW);
    main_WindowClass.hIcon = LoadIconW(NULL, (LPCWSTR) IDI_APPLICATION);
    main_WindowClass.hIconSm = LoadIconW(NULL, (LPCWSTR) IDI_APPLICATION);
    main_WindowClass.style = CS_DBLCLKS;

    //CS_HREDRAW | CS_VREDRAW a


    if (RegisterClassExW(&main_WindowClass) == 0) {
        printf ("[CRITICAL] main_WindowClass cannot be registered!");
        return -1;
    }

    HWND main_WindowHandle = CreateWindowExW (0, L"MOUSE_CLICKER", L"MouseClicker", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);

    if (main_WindowHandle == NULL) {
        return -1;
    }

    ShowWindow(main_WindowHandle, nCmdShow);

    printf("Unicode: %d", IsWindowUnicode(main_WindowHandle));

    MSG ProcessingMessage;
    while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
        TranslateMessage(&ProcessingMessage);
        DispatchMessage(&ProcessingMessage);
    }
    return ProcessingMessage.wParam;

}

最佳答案

在您的项目中定义 UNICODE

根据是否定义了 UNICODETEXT 扩展为 LPSTRLPWSTR。您正在显式调用 *W 版本的 WinAPI 函数,但传递的是 LPSTR 而不是 LPWSTR。用 L 为字符串文字加上前缀应该可以正常工作。也许您将它用作 L("foo") - 它不会以这种方式工作。您需要使用 L"foo"

总的来说,如果您使用TEXT,您应该使用不带后缀的WinAPI 函数,这样代码在定义和不定义UNICODE 时都可以编译。如果您显式使用 *W 函数,请使用 L"" 字符串。

关于c - (C) 不兼容的指针类型 [LPCWSTR],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43746218/

相关文章:

scala - Scala 中路径依赖内部类型之间的关系

types - 在插件架构中扩展类型

c - 使用 linux 功能是否禁用 LD_PRELOAD

c++ - 将 Windows C++ 项目从 Qt4 移动到 5 会出现数百个看似不相关的编译错误

c# - 使用 C# 创建 .cat 文件

c++ - 需要一种有效的方法来处理 C++ 中的 ReadDirectoryChangesW

java - 在 Java 中,HashSet<Integer> = new HashSet(2) 和 HashSet<Integer> = new HashSet<Integer>(2) 之间有什么区别?

c - CPU 与使用 malloc 的 Wall time 测量值的奇怪差异

c - 从标准输入或多个文件参数中读取

c - 如何返回指向缓冲区中位置的指针?