Home > Back-end >  Using the win32 API in vs code
Using the win32 API in vs code

Time:03-24

I've started the process in bringing my program ideas to life. My problem is i cannot seem to get the api to work and run.

I am using visual studio code and the mingw x64 compiler.

i am simply running the sample code the from Microsoft on how to build a direct2d program,

reference

and the base window class from managing application state,

reference 2

only difference is that i got lazy and copy and pasted the basewidow class instead of including it like they did with basewin.h

The Problem:

when i run the sample code i get this error,

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

i did some searching and found out that the api is divided into Ansi and Unicode, so i tried out the Unicode version of the api entry point "winMain" instead of "wwinMain" and changed "pwstr" to "pstr", when i run it i then get this output in the terminal,

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);

I did more searching and saw somewhere that my mingw crt compiler doesnt support unicode or wide characters or somethind and must use WinMain, so i kept WinMain as the entry point and defined UNICODE and _UNICODE which is already defined in the cpp properties json folder, and i get this in the terminal

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

I dont know much but i went as far as changing everything that deals with strings to either ansi and unicode versions to remove the need for conversion but generally i get the same result.

I do not understand how code from the microsoft site can fail and how a compiler that is made for win32 doesnt support what the os supports.

i literally cant run sample code lol, please educate me

generally everything i tried was changing to the versions of the functions that are ansi, trying to use Unicode anyway although appaerently it is incompattible with my compiler by using different macros and defines provided by microsoft, and checking the vscode json and build settings, which are

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"
        }



    ]
}

CodePudding user response:

The D2D1CreateFactory can't be found because you aren't linking D2d1.lib as specified in https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-d2d1createfactory. Add -ld2d1 argument to command line. Maybe you need to use also -L directive to specify directory.

For MSVC compiler pragma directive can be used (which probably doesn't work for mingw).

#pragma comment(lib, "d2d1")

EDITED

Added "-ld2d1", just before "-o",.

        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-ld2d1",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
  • Related