Console Screen Hides after implementing a API function from (windows.h)in C - c

I am new to C language but having experience in higher level language such as java, python.....etc.
I am trying to resize the console window in Windows OS. Using the "SetConsoleWindowInfo and SetConsoleScreenBufferSize" function from "windows.h" header file.
But while I try to run that code the console window hides in the taskbar and nothing is happening. While I try to run the code in vsCode the integrated terminal prints nothing.
Here is my code
void setConsole(short consolePos_X, short consolePos_Y, short consoleWidth, short consoleHeight){
HANDLE wConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT consoleWinSize = {consolePos_X, consolePos_Y, consolePos_X+consoleWidth, consolePos_Y+consoleHeight};
SetConsoleWindowInfo(wConsoleHandle, TRUE, &consoleWinSize);
COORD buffSize = {100, 50};
SetConsoleScreenBufferSize(wConsoleHandle, buffSize);
SetConsoleTitle("Snake Game");
I think the problem is with setting buffersize but I am not sure.
Hope I will get Solution.
Thanks in Advance...!

Change your code as follow:
HANDLE wConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (wConsoleHandle == INVALID_HANDLE_VALUE) {
fprintf(stderr, "GetStdHandle failed with error %d\n", GetLastError());
return;
}
There should be no error here, unless your application has no console allocated.
And also change as follow:
if (!SetConsoleWindowInfo(wConsoleHandle, TRUE, &consoleWinSize)) {
fprintf(stderr, "SetConsoleWindowInfo failed with error %d\n", GetLastError());
return;
}
It is likely that the error is 87 (Invalid parameter). Check the values, especially the consoleWinSize. If it is, then look at this and this.

Hey I Found The Solution.
The problem is that the "ConsoleScreenBufferSize" should not be less than the "ConsoleWindowSize" (More info here). But I have used a "BufferSize" which is less than the "ConsoleSize". So the problem aries.
Solution for this problem is just to use the "BufferSize" Greater than or equal to the "ConsoleSize".
As follows:
COORD coord = {consoleWidth, consoleHeight};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), &coord);
And this just works fine.
If you have anydoubts fell free to comment here.
I will try to answer in my free time
Hope this is useful..!

Related

Monitor flashing when running a Windows SendInput API

Well, I certainly should go to python since I did several functions of this type, keyboard event and mouse event, but decide to try to learn the windows api.
My goal is to know when button 1 of the mouse is pressed.
I created this file in a very beginner way, it returns in mouseData only 0.
The curious thing is that whenever I run it, it flashes my monitor at short intervals in blinks, but between 1 second with it off. Very strange that, execution is not viable.
Could someone help me understand and try to execute to see if it is only here.
Code:
int main()
{
DWORD mouseData = 0;
MOUSEINPUT tagMouse;
tagMouse.dx = 0;
tagMouse.dy = 0;
tagMouse.mouseData = mouseData;
tagMouse.dwFlags = MOUSEEVENTF_XDOWN;
tagMouse.dwExtraInfo = 0;
INPUT tagInput;
tagInput.type = INPUT_MOUSE;
tagInput.mi = tagMouse;
while (true) {
if (GetAsyncKeyState(VK_DELETE)) break;
SendInput(1, &tagInput, sizeof(INPUT));
printf("KEYWORD: %d\n", mouseData);
Sleep(500);
}
system("pause");
return 0;
}
I can reproduce your reported 'symptoms' - and the effect is really brutal!
Now, while I cannot offer a full explanation, I can offer a fix! You have an uninitialized field in your tagMouse structure (the time member, which is a time-stamp used by the system). Setting this to zero (which tells the system to generate its own time-stamp) fixes the problem. So, just add this line to your other initializer statements:
//...
tagMouse.dwExtraInfo = 0;
tagMouse.time = 0; // Adding this line fixes it!
//...
Note: I, too, would appreciate a fuller explanation; however, an uninitialized field, to me, smells like undefined behaviour! I have tried a variety of other values (i.e. not zero) for the time field but haven't yet found one that works.
The discussion here on devblogs may help. This quote seems relevant:
And who knows what sort of havoc that will create if a program checks
the timestamps and notices that they are either from the future or
have traveled back in time.

How to do automatic OpenGL error checking using GLEW?

I was recently trying to implement automatic error checking after each OpenGL function call. I considered wrapping each OpenGL function in a caller like this:
CheckForErrors(glCreateBuffers(1, &VBO));
But I saw that GLEW already uses its own function wrapper:
#define GLEW_GET_FUN(x) x
So I decided to edit it instead of writting my own function wrapper:
#ifndef GLEW_GET_FUN
#ifdef DEBUG
#define GLEW_GET_FUN(x) while (glGetError() != GL_NO_ERROR);\
x; {\
GLenum error = glGetError();\
if (error != GL_NO_ERROR) {\
printf("[GLEW]: OpenGL error(s) occured while calling %s in %s (line %s):", #x, __FILE__, __LINE__);\
do printf(" %d", error); while (error = glGetError());\
printf("\n");\
__debugbreak();\
}
#else
#define GLEW_GET_FUN(x) x
#endif
#endif
Unfortunately, this doesn't compile. For example this function call:
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
Gets changed to this by the preprocessor:
GLuint vertexShaderID = while (glGetError() != GL_NO_ERROR); __glewCreateShader; { GLenum error = glGetError(); if (error != 0) { printf("[GLEW]: OpenGL error(s) occured while calling %s in %s (line %s):", "__glewCreateShader", "main.cpp", 51); do printf(" %d", error); while (error = glGetError()); printf("\n"); __debugbreak(); }(GL_VERTEX_SHADER);
There are 2 problems here:
The statement starts with a while loop, so it cannot return the value.
The parentheses with function parameters are placed after the whole thing and not right after the function call.
I don't know how to overcome those problems and I will appreciate help.
Notes
I am aware of the glDebugMessageCallback() function, but it is only availble in OpenGL 4.3+ which is a rather new and partially insupported yet version.
I cannot remove the while loop at the beginning, because I have to clear all errors before calling the function (unless there is a diffrent way to do this).
I am trying to do something like this, but without using a separate function wrapper.
I don't know how to overcome those problems
You can't. What you want to do is simply not viable in the way you want to do it. You cannot turn an expression (which is what a function call is) into a statement (or rather, a series of statements) and have that work everywhere. It will only work in circumstances where the expression is used as a statement.
If you are unwilling to just regularly insert error checking code into your application, and are unable to use the modern debug messaging API, then the standard solution is to use an external tool to find and report errors. RenderDoc can detect OpenGL errors, for example. It allows you to log every OpenGL call and can report errors anytime they occur.
As Nicol Bolas said, it is impossible to do it the way I originally wanted, but I will describe why this is the case and what can be done instead.
The Problem
GLEW wraps only the name of the function with GLEW_GET_FUN(), so function parameters will always be placed after the end of the define as they are not included in it:
//In code:
glGenBuffers(1, &VBO);
//After preprocessing:
{stuff generated by GLEW_GET_FUN}(1, &VBO);
Preprocessing isn't very inteligent so it will just put the function parameters at the end.
Other Solutions
As described in the question, one could use glDebugMessageCallback() if availble.
Wrap each function with a custom wrapper. Not automatic at all, but if someone is interested here is a great tutorial on how to make one.

mysterious crash after load_bitmap from Allegro

I am new to Allegro. We have to use it in our study.
I have a problem with my code, which should load a bitmap and print it.
#include <allegro.h>
int main( void )
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);
BITMAP *Bild;
if( (Bild=load_bitmap("Spielfeld_Rand.bmp", NULL) ) == NULL )
{
allegro_message( "Error" );
return 1;
}
while( !key[KEY_ESC])
{
draw_sprite(screen, Bild, 0,0);
}
destroy_bitmap(Bild);
return 0;
}
END_OF_MAIN()
The Code chrashes. I do not see any error message, my screen turns black and i can't do anything. I also tried to enter the full path of the picture, but it wont help.
But if i remove the if arount the load_bitmap, the program aborts and return to the sceen.
Can anyone help me with this mysterious crash?
Thanks alot.
set_gfx_mode will change your screen resolution to 640x480 and show a black screen.
The manual says not to use allegro_message in graphics mode. It is probably been called and is locking up the program.
In text mode, allegro_message will put up a dialog box with "Error" in it. The program then won't exit until the ok is selected.
You should also call allegro_exit before exiting or your screen will be left at 640x480 resolution.

Issues with LB_GETCURSEL

I am having some issues with lb_getcursel and what it returns (if it does even return anything)
heres my message handler...
case IDT_TESTLIST1:
if(HIWORD(wParam) == LBN_DBLCLK) {
int ret = 0;
double TimeOut = 60.0;
int Lng = 1;
unsigned char Param[255] = {0};
unsigned char Port1 = port1;
int iCurSel = SendDlgItemMessage(hwnd,IDT_TESTLIST1,LB_GETCURSEL,0.0);
ret = PSB30_Open(Port1,16);
ret = PSB30_SendOrder(Port1,test1[iCurSel].testNumber, &Param[0],&Lng,&TimeOut);
ret = PSB30_Close(Port1);
}
break;
I am using Visual Studio 2010 and whenever i run the program iCurSel doesn't look like it even gets assigned a value, defaults to 0, when i step into the case statement, not all variables are visible in the autos section, when i add a watch to iCurSel i get a CXX0017: Error message.
hwnd is the handle to my main window and is correct
any help would be appreciated
Cheers
i find it funny that none of my variables in the message are showing anything by hovering over them
That's because they don't exist. Your program cannot compile, it has an error. SendDlgItemMessage() takes 5 arguments, you pass 4. The last one got morphed into a floating point value by a typo.
Clearly you'll need to pay attention to compile error messages. And change a setting so this cannot happen again. Tools + Options, Projects and Solution, Build and Run. Change the "On Run, when build or deployment error occurs" setting to "Do not launch".

C_Login fails in PKCS11 in C

Simple issue, but i don't know how to unlock USB Token(epass2003) ,I have try to read PKCS 11 but have no idea how to implement C_Login function for execution in c ,when i am using command line tool (Linux)to do that token is working perfectly fine but with c its not working I have used user type as CKU_USER, Can anyone have knowledge about this, please help
you have to check the return values from the PKCS functions to see if there has been any errors. Try this way and see what happen. If the return code from C_login() is CKR_PIN_LOCKED, then it is clear that you should unlock your card.
CK_RV ret;
ret = C_OpenSession(slot, CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session);
if (ret != CKR_OK){
error_message(ret);
return;
}
readPIN("Intro PIN: ", pin, 4);
ret = (f_C_Login)(hSession,CKU_USER, (unsigned char *) pin,strlen(pin));
if (ret != CKR_OK){
closeSessions(slot);
error_message(ret);
return;
}
A token can get locked due to a certain number of failed login (for TrustKey it is 10). There are provider specific utilities to unlock tokens. You could check Feitian site. There is some pointer to this kind of problem in Gooze forum (though not exactly). Your problem looks quite like a token lock problem.

Resources