-3

I am trying to get a handle for the console window with the following:

#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>

#define NTDDI_WIN7 as 0x06010000
#define _WIN32_WINNT as 0x0500

int main(int argc, char *argv[]) {
    HWND self = GetConsoleWindow();
    /* some more code */
    return 0;
}

I followed the instructions from the GetConsoleWindow documentation and "Using the Windows Headers", but I still get:

undefined reference to `GetConsoleWindow'

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Linz
  • 9
  • 5
  • 2
    You probably have to use the defines before you include the `windows.h`. – Dirk Dec 06 '17 at 14:18
  • You do need the defines before including Windows.h, and `#define NTDDI_WIN7 as 0x06010000` is not correct syntax. https://ideone.com/DP1j6j – Retired Ninja Dec 06 '17 at 14:19
  • The linked documentation implies pretty clearly that the macros affect parsing of the headers, hence must be available before `#include`ing them. – underscore_d Dec 06 '17 at 14:20
  • 1
    You meant to define `NTDDI_VERSION`, not one of the constant values it can be defined as (i.e. `#define NTDDI_VERSION NTDDI_WIN7` ). Define this macro if you're targeting a specific service pack. Otherwise let the headers define it based on the value of `_WIN32_WINNT`. For the latter, unless you're actually targeting ancient Windows 2000 (i.e. `_WIN32_WINNT_WIN2K`, i.e. 0x0500), then you'll want to use a more recent version, or the default value from the SDK. Windows 2000 is just the minimum version required for `GetConsoleWindow` in the wincon.h header. – Eryk Sun Dec 06 '17 at 16:29
  • also: the "as" needs to be deleted in the `#define`s – Edward Clements Dec 06 '17 at 16:47
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Dec 06 '17 at 20:10

2 Answers2

2

You have to have kernel32.lib in your list of input libraries.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

remove gdi32.def kernel32.def msvcrt.def user32.def files from C:\tcc\lib; remove all .def files from C:\tcc\lib

#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <conio.h>

void main()
{
    HWND hwnd;   
    hwnd = GetConsoleWindow(); 
    HDC hdc;
    hdc = GetWindowDC(hwnd);
    
    printf("console hwnd:  %p\n", hwnd);
    printf("console hdc:  %p\n", hdc);
    
    HPEN hPenNull, hPenBlack, hPenRed, hPenGreen, hPenBlue;
    hPenNull=GetStockObject(NULL_PEN); 
    hPenBlack=CreatePen(PS_SOLID, 2, RGB(0,0,0)); 
    hPenRed=CreatePen(PS_SOLID, 2, RGB(255,0,0));
    hPenGreen=CreatePen(PS_SOLID, 2, RGB(0,255,0)); 
    hPenBlue=CreatePen(PS_SOLID, 2, RGB(0,0,255)); 

    HBRUSH hBrushNull, hBrushBlack, hBrushRed, hBrushGreen, hBrushBlue, hBrushYellow;
    hBrushNull=GetStockObject(NULL_BRUSH); 
    hBrushBlack=CreateSolidBrush(RGB(0,0,0)); 
    hBrushRed=CreateSolidBrush(RGB(255,0,0)); 
    hBrushYellow=CreateSolidBrush(RGB(255,255,0)); 
    hBrushGreen=CreateSolidBrush(RGB(0,255,0)); 
    hBrushBlue=CreateSolidBrush(RGB(0,0,255)); 
    
    SelectObject(hdc, hPenRed);
    SelectObject(hdc, hBrushYellow);
    Ellipse(hdc, 200,50,260,150);

    SelectObject(hdc, hPenNull);
    SelectObject(hdc, hBrushRed);
    Ellipse(hdc, 140, 80, 180, 120);
    
    SelectObject(hdc, hPenBlue);
    SelectObject(hdc, hBrushNull);
    Ellipse(hdc, 280, 50, 340, 150);
    
getch();
}

-L"C:\tcc\lib" -lkernel32 -luser32 -lgdi32 -Wl,-subsystem=console

enter image description here