在 vs 代码中使用 win32 API
Using the win32 API in vs code
我已经开始将我的计划想法付诸实践。我的问题是我似乎无法让 api 工作并且 运行.
我正在使用 visual studio 代码和 mingw x64 编译器。
我只是 运行使用 Microsoft 中关于如何构建 direct2d 程序的示例代码,
和管理应用程序状态的基础 window class,
唯一的区别是我懒惰并复制并粘贴了 basewidow class 而不是像 basewin.h
那样包含它
问题:
当我 运行 示例代码时出现此错误,
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccti919g.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv]+0x2b): undefined reference to `D2D1CreateFactory'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
我做了一些搜索,发现 api 分为 Ansi 和 Unicode,所以我尝试了 api 入口点“winMain”而不是“wwinMain”的 Unicode 版本并将“pwstr”更改为“pstr”,当我 运行 然后我在终端中得到这个输出,
In instantiation of 'BOOL BaseWindow<DERIVED_TYPE>::Create(PCWSTR, DWORD, DWORD, int, int, int, int, HWND, HMENU) [with DERIVED_TYPE = MainWindow; BOOL = int; PCWSTR = const wchar_t*; DWORD = long unsigned int; HWND = HWND__*; HMENU = HMENU__*]':
myprogram.cpp:202:20: required from here
myprogram.cpp:54:37: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'} in assignment
54 | wc.lpszClassName = ClassName();
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
myprogram.cpp:59:33: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'}
59 | dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
In file included from C:/msys64/mingw64/include/windows.h:72,
from myprogram.cpp:1:
C:/msys64/mingw64/include/winuser.h:2203:65: note: initializing argument 2 of 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'
2203 | WINUSERAPI HWND WINAPI CreateWindowExA(DWORD dwExStyle,LPCSTR lpClassName,LPCSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam);
我做了更多搜索,在某处看到我的 mingw crt 编译器不支持 unicode 或宽字符或其他东西,必须使用 WinMain,所以我将 WinMain 作为入口点并定义了 UNICODE 和 _UNICODE,它们已经在 cpp 中定义properties json 文件夹,我在终端中得到这个
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccT85reo.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv]+0x2b): undefined reference to `D2D1CreateFactory'
collect2.exe: error: ld returned 1 exit status
我知道的不多,但我将处理字符串的所有内容更改为 ansi 和 unicode 版本以消除转换的需要,但通常我得到相同的结果。
我不明白来自 microsoft 站点的代码为何会失败,以及为 win32 制作的编译器为何不支持 os 支持的内容。
我真的不会运行示例代码哈哈,请教我
一般来说,我尝试的所有操作都是更改为 ansi 函数的版本,无论如何都尝试使用 Unicode,尽管通过使用不同的 macros 和 micr[= 提供的定义,它显然与我的编译器不兼容64=]oft,并检查 vscode json 和构建设置,它们是
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\msys64\mingw64\bin\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\msys64\mingw64\bin\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\msys64\mingw64\bin\g++.exe"
}
]
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\msys64\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
无法找到 D2D1CreateFactory
,因为您没有按照 https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-d2d1createfactory 中指定的方式链接 D2d1.lib
。
将 -ld2d1
参数添加到命令行。也许您还需要使用 -L
指令来指定目录。
对于 MSVC 编译器,可以使用 pragma
指令(可能不适用于 mingw
)。
#pragma comment(lib, "d2d1")
已编辑
在 "-o",
之前添加 "-ld2d1",
。
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-ld2d1",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
我已经开始将我的计划想法付诸实践。我的问题是我似乎无法让 api 工作并且 运行.
我正在使用 visual studio 代码和 mingw x64 编译器。
我只是 运行使用 Microsoft 中关于如何构建 direct2d 程序的示例代码,
和管理应用程序状态的基础 window class,
唯一的区别是我懒惰并复制并粘贴了 basewidow class 而不是像 basewin.h
那样包含它问题:
当我 运行 示例代码时出现此错误,
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccti919g.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv]+0x2b): undefined reference to `D2D1CreateFactory'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
我做了一些搜索,发现 api 分为 Ansi 和 Unicode,所以我尝试了 api 入口点“winMain”而不是“wwinMain”的 Unicode 版本并将“pwstr”更改为“pstr”,当我 运行 然后我在终端中得到这个输出,
In instantiation of 'BOOL BaseWindow<DERIVED_TYPE>::Create(PCWSTR, DWORD, DWORD, int, int, int, int, HWND, HMENU) [with DERIVED_TYPE = MainWindow; BOOL = int; PCWSTR = const wchar_t*; DWORD = long unsigned int; HWND = HWND__*; HMENU = HMENU__*]':
myprogram.cpp:202:20: required from here
myprogram.cpp:54:37: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'} in assignment
54 | wc.lpszClassName = ClassName();
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
myprogram.cpp:59:33: error: cannot convert 'PCWSTR' {aka 'const wchar_t*'} to 'LPCSTR' {aka 'const char*'}
59 | dwExStyle, ClassName(), lpWindowName, dwStyle, x, y,
| ~~~~~~~~~^~
| |
| PCWSTR {aka const wchar_t*}
In file included from C:/msys64/mingw64/include/windows.h:72,
from myprogram.cpp:1:
C:/msys64/mingw64/include/winuser.h:2203:65: note: initializing argument 2 of 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'
2203 | WINUSERAPI HWND WINAPI CreateWindowExA(DWORD dwExStyle,LPCSTR lpClassName,LPCSTR lpWindowName,DWORD dwStyle,int X,int Y,int nWidth,int nHeight,HWND hWndParent,HMENU hMenu,HINSTANCE hInstance,LPVOID lpParam);
我做了更多搜索,在某处看到我的 mingw crt 编译器不支持 unicode 或宽字符或其他东西,必须使用 WinMain,所以我将 WinMain 作为入口点并定义了 UNICODE 和 _UNICODE,它们已经在 cpp 中定义properties json 文件夹,我在终端中得到这个
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\uub17\AppData\Local\Temp\ccT85reo.o:myprogram.cpp:(.text$_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv[_Z17D2D1CreateFactory17D2D1_FACTORY_TYPERK5_GUIDPPv]+0x2b): undefined reference to `D2D1CreateFactory'
collect2.exe: error: ld returned 1 exit status
我知道的不多,但我将处理字符串的所有内容更改为 ansi 和 unicode 版本以消除转换的需要,但通常我得到相同的结果。
我不明白来自 microsoft 站点的代码为何会失败,以及为 win32 制作的编译器为何不支持 os 支持的内容。
我真的不会运行示例代码哈哈,请教我
一般来说,我尝试的所有操作都是更改为 ansi 函数的版本,无论如何都尝试使用 Unicode,尽管通过使用不同的 macros 和 micr[= 提供的定义,它显然与我的编译器不兼容64=]oft,并检查 vscode json 和构建设置,它们是
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\msys64\mingw64\bin\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\msys64\mingw64\bin\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\msys64\mingw64\bin\g++.exe"
}
]
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\msys64\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
无法找到 D2D1CreateFactory
,因为您没有按照 https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-d2d1createfactory 中指定的方式链接 D2d1.lib
。
将 -ld2d1
参数添加到命令行。也许您还需要使用 -L
指令来指定目录。
对于 MSVC 编译器,可以使用 pragma
指令(可能不适用于 mingw
)。
#pragma comment(lib, "d2d1")
已编辑
在 "-o",
之前添加 "-ld2d1",
。
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-ld2d1",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],