2013-06-12 18 views
-1

我得到Spotify的窗口標題我現在播放插件與功能:替換字符 uFFFD而GetWindowText函數(C代碼)

GetWindowText(spotify_window_handle, title, title_length) 

但輸出包含替換字符\ uFFFD。

Ex. Spotify - Killswitch Engage � One Last Sunset 

如何用C代替 ?

下面全碼:

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\n"); 
     } 
     free(title); 
    } 
    else 
    { 
     printf("PLUGIN: Unable to get Spotify window title\n"); 
    } 
} 
// End of Spotify get title function 
+0

如何'title'聲明? – alk

+0

我已經粘貼了全部函數 –

回答

0

這取決於如果您正在使用Unicode或沒有。既然你說\ uFFFD你最有可能使用Unicode所以

WCHAR *wp; 

    while ((wp= wcschr(title, '\uFFFD'))!=NULL) { 
     *wp= L'-'; 
    } 
+0

我使用了char * title; –

+0

用char *標題,怎麼會有16位\ xFFFD在裏面? – Grezgory

0

假設字符串的wchar_t數組:

wchar_t * p = wcschr(title, `\uFFFD`); 
if (p) 
    *p = `\u002D`; 
+0

我使用char * title; –

1

替換字符在Unicode的>安思轉換使用。沒有看到title實際上是如何聲明的(它是使用char還是wchar_t?),我的猜測是你正在調用Ansi版本GetWindowText()(又名GetWindowTextA()),並且窗口標題包含一個Unicode字符,無法在操作系統的默認Ansi中表示所以當將窗口文本轉換爲Ansi輸出時,GetWindowTextA()會替換該字符。請記住,Windows實際上是一個基於Unicode的操作系統,所以你應該使用的GetWindowText()(又名GetWindowTextW())Unicode版本代替,例如:

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

或者:

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); 

或者至少使確保您的項目被配置爲編譯爲Unicode,以便在編譯期間定義UNICODE_UNICODE。這將使GetWindowText()映射到GetWindowTextW()而不是GetWindowTextA()。然後,您將不得不使用TCHARtitle緩衝區,如:

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

或者:

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);