2013-03-05 64 views
0

我正在创建一个跨平台应用程序,并且正在打开GCC/Mingw的头文件源代码文件,并在需要时将定义从那里复制到我的头文件中。现在高达正确运行,但现在与错误和代码击中该项目初始化输出后获取控制台大小

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(): 
} 

*如果我不写函数get_console_size则编译很好*

但添加后get_console_size其与CreateFile函数产生以下错误作为__cdecl:

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

但是,当我试图编译与__declspec (dllimport的),它产生以下错误:

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

GCC编译使用的命令:

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 

搜索谷歌后,我发现,这是Kernel32.dll中内置功能。

现在,我只想验证控制台大小是否设置正确?

可以使用任何其他方法或任何其他的东西来做到这一点?

什么是_ imp _CreateFile和_CreateFile函数以及它们在哪里定义?

如何将windows Kernel32.dll与GCC Project链接?

+0

将'-lkernel32'添加到您的构建命令应该照顾与kernel32的链接。顺便说一句,你的'get_console_size'函数看起来不会显式返回任何东西,即使它被声明为返回一个'int'。 – Michael 2013-03-05 12:20:25

+0

'我打开GCC/Mingw的头文件源代码文件,并在需要时将定义从那里复制到我的头文件中,这不是一个好主意。 – 2013-03-05 16:16:07

回答

1

在Windows上,我认为你想要做的是解释here。基本上,链接的文章调用AllocConsole以获得新控制台的句柄,然后GetConsoleScreenBufferInfo获取有关控制台的元数据,如宽度和高度。