[RESOLVED] invalid parameter?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] invalid parameter?

  1. #1
    Join Date
    Apr 2009
    Posts
    605

    [RESOLVED] invalid parameter?

    i'm using these code for get and set the caret positions:
    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo;
    		GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE),&bufferInfo);
    with GetLastError(), i recive the "ERROR_INVALID_PARAMETER"...why????
    i belive that the handle is correct and the 2nd parameter is correctly used. so what is the problem?
    the SetConsoleScreenBufferInfoEx() funtion gives me the same error
    (yesterday i learned very... now i can detect the errors hehehe)

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,264

    Re: invalid parameter?

    Has the GetConsoleScreenBufferInfoEx failed? You are not testing its return value to find out. In general GetLastError() only returns a valid value when a function fails. Unless the documentation for a specific function says otherwise, the result of GetLastError() is not valid if a function succeeds!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2009
    Posts
    605

    Re: invalid parameter?

    Quote Originally Posted by 2kaud View Post
    Has the GetConsoleScreenBufferInfoEx failed? You are not testing its return value to find out. In general GetLastError() only returns a valid value when a function fails. Unless the documentation for a specific function says otherwise, the result of GetLastError() is not valid if a function succeeds!
    i recive the 87 number in both:

    Code:
    POINT GetCaretPosition()
    	{
    		POINT s;
    		GetCaretPos(&s);
    		return s; 
    	}
    
    	void SetCaretPosition(POINT s)
    	{	
    		
    		CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo;
    		GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE),&bufferInfo);
    		cout << GetLastError();
    		bufferInfo.dwCursorPosition.X=s.x;
    		bufferInfo.dwCursorPosition.Y=s.y; 
    		
    		SetConsoleScreenBufferInfoEx( GetStdHandle(STD_OUTPUT_HANDLE),&bufferInfo);
    		 cout << GetLastError();		 
    	}
    	__declspec ( property ( put = SetCaretPosition, get = GetCaretPosition ) ) POINT CaretPosition ;
    and heres how i use it:
    Code:
    #include "stdafx.h"
    #include "Console.h"
    #include "Thread.h"
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	Console a;
    	POINT b;
    	b.x =10;
    	b.y=20;
    	a.CaretPosition= b;
    	a.Write("hello"); 
        a.Read();	
        return 0;
    }
    and 2 warnings:
    1 - "warning C4244: '=' : conversion from 'LONG' to 'SHORT', possible loss of data";
    2 -"warning C4244: '=' : conversion from 'LONG' to 'SHORT', possible loss of data";

    in these 2 lines:
    Code:
    bufferInfo.dwCursorPosition.X=s.x;
    		bufferInfo.dwCursorPosition.Y=s.y;

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,264

    Re: invalid parameter?

    As I said in post #2, you are not checking the return value of the function. You need to code it something like this

    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo;
    
            if (!GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &bufferInfo)) {
    	    cout << "Error in GetConsoleScreenBufferInfoEx: " << GetLastError() << endl;
            }
    1 - "warning C4244: '=' : conversion from 'LONG' to 'SHORT', possible loss of data";
    2 -"warning C4244: '=' : conversion from 'LONG' to 'SHORT', possible loss of data";
    The x and y elements of POINT structure are defined as type long.
    The dwCursorPosition element of CONSOLE_SCREEN_BUFFER_INFOEX is defined as type COORD in which the X and Y elements are defined as type SHORT. Hence the compiler warning about conversion from long to short.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2009
    Posts
    605

    Re: invalid parameter?

    Quote Originally Posted by 2kaud View Post
    As I said in post #2, you are not checking the return value of the function. You need to code it something like this

    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo;
    
            if (!GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &bufferInfo)) {
    	    cout << "Error in GetConsoleScreenBufferInfoEx: " << GetLastError() << endl;
            }


    The x and y elements of POINT structure are defined as type long.
    The dwCursorPosition element of CONSOLE_SCREEN_BUFFER_INFOEX is defined as type COORD in which the X and Y elements are defined as type SHORT. Hence the compiler warning about conversion from long to short.
    i did now these and show me the same 87 number

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,274

    Re: invalid parameter?

    Quote Originally Posted by Cambalinho View Post
    i did now these and show me the same 87 number
    1. Did you check if the GetStdHandle returned the valid handle?
    2. Did you set the cbSize member of CONSOLE_SCREEN_BUFFER_INFOEX structure?
    Victor Nijegorodov

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,264

    Re: invalid parameter?

    You must read and understand the documentation for the functions you use!

    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo = {sizeof(CONSOLE_SCREEN_BUFFER_INFOEX)};
    Last edited by 2kaud; August 20th, 2013 at 03:21 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Apr 2009
    Posts
    605

    Re: invalid parameter?

    Quote Originally Posted by 2kaud View Post
    You must read and understand the documentation for the functions you use!

    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo = {sizeof(CONSOLE_SCREEN_BUFFER_INFOEX)};
    Code:
    CONSOLE_SCREEN_BUFFER_INFOEX bufferInfo;
    		bufferInfo.cbSize=sizeof( CONSOLE_SCREEN_BUFFER_INFOEX);
    		GetConsoleScreenBufferInfoEx(GetStdHandle(STD_OUTPUT_HANDLE), &bufferInfo);
    		cout << GetLastError();
    		bufferInfo.dwCursorPosition.X   =Position.x ;
    		bufferInfo.dwCursorPosition.Y   =Position.y ;
    		SetConsoleScreenBufferInfoEx( GetStdHandle(STD_OUTPUT_HANDLE),&bufferInfo);
    		 cout << GetLastError();
    and how i use:

    Code:
    Console a;
    	POINT b;
    	b.x =10;
    	b.y=20;
    	a.CaretPosition= b;
    	a.Write("hello"); 
        a.Read();	
        return 0;
    i see 2 problems:
    1 - the caret position isn't changed;
    2 - why the window buffer size is changed?

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,264

    Re: invalid parameter?

    To set the cursor position use something like

    Code:
    DWORD SetCursorPosition(HANDLE sout, COORD pos)
    {
    	return (!SetConsoleCursorPosition(sout, pos) ? GetLastError() : 0);
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Apr 2009
    Posts
    605

    Re: invalid parameter?

    Quote Originally Posted by 2kaud View Post
    To set the cursor position use something like

    Code:
    DWORD SetCursorPosition(HANDLE sout, COORD pos)
    {
        return (!SetConsoleCursorPosition(sout, pos) ? GetLastError() : 0);
    }
    thank you very much my friend.. thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center