Vineet.Chandigarh
Vineet.Chandigarh

Reputation: 17

Getting console size after Initialize the output

I am creating a cross platform application and for doing that I am opening header source code files of GCC/Mingw and copied the definitions from there to my header files as and when required. The Project upto now running correctly but now struck with the error and the code is

console.c

     #include<console.h>
     HANDLE write_Console


int get_console_size(){
    if( ( write_Console = CreateFile( "CONOUT$", 
                  GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | 
                  FILE_SHARE_WRITE, 0L, OPEN_EXISTING, 
                  FILE_ATTRIBUTE_NORMAL, 0L) ) == (HANDLE) -1 
             ) {
        printf("opened");
    }
    }


void console_settings_init(){

    //For writing on the console
    write_Console = GetStdHandle(STD_OUTPUT_HANDLE);

    //For Reading from Console
    read_Console = GetStdHandle(STD_INPUT_HANDLE);

    // Set up the required window size:
    SMALL_RECT windowSize = {0, 0, CONSOLE_WIDTH-1, CONSOLE_HEIGHT-1};

    // Change the console window size:
    SetConsoleWindowInfo(write_Console, TRUE, &windowSize);


    // Create a COORD to hold the buffer size:
    COORD bufferSize = {CONSOLE_WIDTH, CONSOLE_HEIGHT};

    // Change the internal buffer size:
    SetConsoleScreenBufferSize(write_Console, bufferSize);

    recheck_console():
}

*If, I don't write the function get_console_size then it is compiled very well*

But after adding get_console_size it produces the following error with CreateFile function as __cdecl:

 console.c:(.text+0x4a): undefined reference to `_CreateFile'

But when I tried to compile is with __declspec(dllimport) it produces the following error:

 console.c:(.text+0x4a): undefined reference to `__imp__CreateFile'

GCC Compiling Command used:

 gcc -DHAVE_CONFIG_H -I. -I..  -I../include    
     -nostdinc -Wall -fno-builtin -Wno-pragmas -Werror -MT 
      console.o -MD -MP -MF .deps/console.Tpo -c -o console.o console.c
      mv -f .deps/console.Tpo .deps/console.Po

After searching the Google I found that it is inbuilt function in Kernel32.dll.

Now, I simply want to verify that console size is properly set or not?

Can use any other method or any other things to do so?

What are _imp_CreateFile and `_CreateFile functions and where they are defined?

How can I link windows Kernel32.dll with GCC Project?

Upvotes: 0

Views: 740

Answers (1)

user152949
user152949

Reputation:

On Windows then I think what you are trying to do is explained here. Basically,the linked article calls AllocConsole to get the handle to a new console and then GetConsoleScreenBufferInfo to get metadata about the console like its width and height.

Upvotes: 1

Related Questions