[英]Get Text from an Edit Control (Pure Win32 API)
我一直試圖讓這個工作年齡相似,但沒有用(悲傷的臉)。
int iChars = GetWindowTextLength (GetDlgItem(handle,ID))+1; // Room for '\0'
char* pstrText;
pstrText = (char*) malloc (sizeof(char)*iChars);
if (pstrText != NULL) {
//GetWindowText (GetDlgItem(handle,ID), pstrText, iChars);
GetDlgItemText(handle,ID,pstrText,iChars);
}
return pstrText; // Memory gets freed after it returns
工作范例:
char* MWC::System::TextBox::GetText(){
int len = SendMessage(handle, WM_GETTEXTLENGTH, 0, 0);
char* buffer = new char[len];
SendMessage(handle, WM_GETTEXT, (WPARAM)len+1, (LPARAM)buffer);
return buffer;
}
wParam
參數在這里是錯誤的:
SendMessage(handle, WM_GETTEXT, (WPARAM)len, (LPARAM)buffer);
由於零終止符,您應該傳遞len+1
。
你回來之前就釋放了記憶!
if ((pstrText != NULL) {
GetDlgItemText(handle,ID,pstrText,sizeof(pstrText));
free (pstrText); // Freeing memory Here!
}
當客戶不再需要時,您必須為客戶提供免費的方式...
希望這可以幫助!
在返回之前,您已經釋放了pstrText
指向的內存。 您應該返回一個實際上可以包含文本的字符串對象,並在發布時自動釋放它。 或者你必須要求調用者為字符串分配內存,但是你只需要包裝API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.