Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm getting Spotify's window title for my Now Playing plugin with function:

GetWindowText(spotify_window_handle, title, title_length)

but output contain Replacement character uFFFD.

Ex. Spotify - Killswitch Engage ? One Last Sunset

How to replace ? with - in C?

Below full code:

char* spotify_title(int window_handle)
{
    int title_length = GetWindowTextLength(window_handle);
        if(title_length != 0)
        {
            char* title;
            title = (char*)malloc((++title_length) * sizeof *title );
            if(title != NULL)
            {
                GetWindowText(window_handle, title, title_length);
                if(strcmp(title, "Spotify") != 0)
            {
                return title;
            }
            else
            {
                return "Spotify is not playing anything right now. Type !botnext command to restart playback.";
            }
        }
        else
        {
            printf("PLUGIN: Unable to allocate memory for title
");
        }
        free(title);
    }
    else
    {
        printf("PLUGIN: Unable to get Spotify window title
");
    }
}
// End of Spotify get title function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

The replacement character is used during Unicode->Ansi conversions. Without seeing how title is actually declared (is it using char or wchar_t?), my guess is that you are calling the Ansi version of GetWindowText() (aka GetWindowTextA()) and the window title contains a Unicode character that cannot be represented in your OS's default Ansi locale, so GetWindowTextA() replaces that character when converting the window text to Ansi for output. Keep in mind that Windows is actually a Unicode-based OS, so you should use the Unicode version of GetWindowText() (aka GetWindowTextW()) instead, eg:

WCHAR title[256];
int title_length = 256;
GetWindowTextW(spotify_window_handle, title, title_length);

Or:

int title_length = GetWindowTextLengthW(spotify_window_handle);
LPWSTR title = (LPWSTR) malloc((title_length+1) * sizeof(WCHAR));
GetWindowTextW(spotify_window_handle, title, title_length+1);
...
free(title);

Or at least make sure your project is configured to compile for Unicode so that UNICODE and _UNICODE are defined during compiling. That will make GetWindowText() map to GetWindowTextW() instead of GetWindowTextA(). You will then have to use TCHAR for your title buffer, eg:

TCHAR title[256];
int title_length = 256;
GetWindowText(spotify_window_handle, title, title_length);

Or:

int title_length = GetWindowTextLength(spotify_window_handle);
LPTSTR title = (LPTSTR) malloc((title_length+1) * sizeof(TCHAR));
GetWindowText(spotify_window_handle, title, title_length+1);
...
free(title);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...