Windows Title

So I am trying to make a function that allows me to easily change the Title, but there seems to be a bit of difficulty. Acting difficult and Acting Stupid, nor am I good with either so here is what I have.
#include <Windows.h>
#include <strsafe.h>

wchar_t Title(wchar_t a [1], wchar_t b [255])
{
	GetConsoleTitle (a, 0);
	StringCchPrintf (b, 255, TEXT (b), a);
	SetConsoleTitle (b);
	return b [255];
}

int main()
{
	Title ("C Command Prompt");
}

1>
Build started: Project: Title Function, Configuration: Debug Win32
1> Title Function.cpp
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\title function\title function\title function.cpp(7): error C2065: 'Lb' : undeclared identifier
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\title function\title function\title function.cpp(14): error C2660: 'Title' : function does not take 1 arguments
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Comments

  • Did you even read the error message?
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\title function\title function\title function.cpp(7): error C2065: 'Lb' : [B][U]undeclared identifier[/U][/B]
    
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\title function\title function\title function.cpp(14): error C2660: 'Title' : [U][B]function does not take 1 arguments[/B][/U]
    

    They clearly state the issues you are having. They also state the line number (7 and 14)
  • >return b [255];

    Wrong. There is no 255th character in that array.

    Your code for GetConsoleTitle() is all wrong. The first parameter should be a pointer to a block of memory that is allocated by your program, for example: GetConsoleTitle() does NOT allocate memory for this. IMO the MSDN docs for GetConsoleTitle() is a little confusing -- the function does not allocate any memory as implied by the Parameters paragraph here.
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
    	char title[255];
    	GetConsoleTitle(title,sizeof(title));
    
    	printf("%s\n", title);
    	SetConsoleTitle("Hello World");
    	GetConsoleTitle(title,sizeof(title));
    	printf("%s\n", title);
    }
    
    
  • Your code gave me this.

    1>
    Build started: Project: Practice Programming, Configuration: Debug Win32
    1> Practice Programming.cpp
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(7): error C2664: 'GetConsoleTitleW' : cannot convert parameter 1 from 'char [255]' to 'LPWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(10): error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(11): error C2664: 'GetConsoleTitleW' : cannot convert parameter 1 from 'char [255]' to 'LPWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • if you want to compile for unicode then you have to use wchar_t instead of char. Tell your compiler not to compile for unicode and it will compile without problems.
  • I didn't think to change char to wchar_t at the time, but I just did and it got rid of all error messages except this one. I have not tried to turn of the Unicode thing yet I have never done it before. I can google it even so I think I have a pretty good idea of how it's done.

    1>
    Build started: Project: Practice Programming, Configuration: Debug Win32
    1> Practice Programming.cpp
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(11): error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • You need to do this:

    [icode]44SetConsoleTitle(TEXT("Hello World")7);[/icode]

    Here is a summary of how to declare UNICODE strings.

    With Microsoft compilers turn off UNICODE in Project settings, as shown in the red circle in the attached thumbnail
  • Ok I did that check.
    With the 44 in there I a lot of errors.
    Without the 44 I get less errors, but it's no wonder why people just use
    system ("title");
    
    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {
    	wchar_t title[12];
    	GetConsoleTitle(title,sizeof(title));
    	printf("%s\n", title);
    	SetConsoleTitle(TEXT("Hello World")7);
    	GetConsoleTitle(title,sizeof(title));
    	printf("%s\n", title);
    }
    

    1>
    Build started: Project: Practice Programming, Configuration: Debug Win32
    1> Practice Programming.cpp
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(7): error C2664: 'GetConsoleTitleA' : cannot convert parameter 1 from 'wchar_t [12]' to 'LPSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(9): error C2059: syntax error : 'bad suffix on number'
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(9): error C2146: syntax error : missing ';' before identifier 'SetConsoleTitleA'
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(9): error C2143: syntax error : missing ')' before 'constant'
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(9): error C2059: syntax error : ')'
    1>c:\users\smjprogrammer\documents\visual studio 2010\projects\practice programming\practice programming\practice programming.cpp(10): error C2664: 'GetConsoleTitleA' : cannot convert parameter 1 from 'wchar_t [12]' to 'LPSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • Now you are still trying to mix apples and oranges. If you disable UNICODE then you have to change wchar_t back to char. The two can not be mixed. By using macros you can make your code compile with either UNICODE or not so you have same codde
    #include <stdio.h>
    #include <windows.h>
    #include <tchar.h> // defines UNICODE macros used below
    
    int main()
    {
            TCHAR title[12];
    	GetConsoleTitle(title,sizeof(title)/sizeof(title[0]));
    	_tprintf(TEXT("%s\n"), title);
    	SetConsoleTitle(TEXT("Hello World")7);
    	GetConsoleTitle(title,sizeof(title)/sizeof(title[0]));
    	_tprintf(TEXT("%s\n"), title);
    }
    
  • Well I got it to worked the way you had it. As far as making it into a function it just prints random characters. Also I would like to know why lines 7 and 9 when I take them out one or the other even both I get the same result.
  • Ok never mind I figured it out. Feel free to still explain though.
    This is what I came up with. :)
    #include <windows.h>
    
    char Title(char a [12])
    {
    	SetConsoleTitle (TEXT (a));
    	return a [12];
    }
    
    int main()
    {
    	Title("Hello World");
    } 
    
  • TEXT macro does NOT convert variables from char to wchar_t, it only converts string literals

    #include <windows.h>
    
    TCHAR* Title(TCHAR a [12])
    {
    	SetConsoleTitle (a);
    	return a;
    }
    
    int main()
    {
    	Title(TEXT("Hello World"));
    }
    
  • From what I can tell they both do the same thing. Are you saying your version is safer.
  • His code is returning the name of the array 'a', which has the address of the first element of the array (a[0]), as a constant address.

    Your code is returning a single element (a[12]), which is OUTSIDE the bounds of the 'a' array, and therefore, a bug.
  • Ok then.
    I have noticed if I remove TEXT out completely it still works, but does that make it a bug?
    I have noticed if I take
    TCHAR* Title(TCHAR a [12])
    

    and change it to
    TCHAR* Title(char a [12])
    

    it still works, so is that a bug if I do that?
  • Not a bug apparently, but it makes your code less flexible and less portable. This is quite a topic:
    http://stackoverflow.com/questions/234365/is-tchar-still-relevant

    Most answers appear to say NO to Tchar. Use wide chars whenever you can.
  • Is it a bug or not? Depends. Are you compiling for UNICODE? If you are, then your program has several bugs because it isn't using UNICODE strings. If your program is NOT compiled for UNICODE then it will compile ok only because TEXT is converted to be char instead of wchar_t.

    Here is a general rule-of-the-thumb when to use the macros in tchar.h and when not to use them
    • UNICODE -- always use the TEXT and other UNICODE macros
    • non-UNICODE -- Use of those macros is optional, but BE CONSISTENT -- if you use the macros some places then use the macros all over the program.
    • maybe UNICODE sometimes and maybe not other times -- always use the macros so that you don't have to keep changing your source code.
  • Can I change TCHAR to wchar_t?
  • you could -- but I wouldn't. TCHAR is a macro that expands to either char or wchar_t, depending on whether the program is being compiled for UNICODE or not.
  • It just dawned on me if all I am trying to do is make it easier to change the title through out the program then why don't I just use this instead of calling a bigger function that takes longer to write anyways.
    SetConsoleTitle (TEXT ("So Much Easier"));
    
  • If it only takes 1 line of code to do what they said takes 4 lines does this mean all, or some of the other functions in <windows.h> are easier then what the example show?
  • I don't know what examples you are referring to.
  • I tried to look them up but was unable to find them all, but they are all impossible to understand would take for ever to learn and if you know of a place that has good well working windows.h function examples. Post the link.
  • You will never find them all in one place because there are just too many of them. Microsoft and other publishers have published several large books on the subject. Actually, IMO it isn't worth the effort any more to try to learn them since Microsoft developed .NET platform, which is a replacement for win32 api. Microsoft isn't going to drop win32 api in the foreseeable future because there are thousands of windows programs that use it. Most, if not all, new desktop program development is being done with .NET languages. If I were you I'd start learning .NET languages, such as C++/CLR, C#, F# and VB.NET.
Sign In or Register to comment.