int GetWindowTextUTF8(HWND hWnd, LPTSTR lpString, int nMaxCount) { if (!lpString) return 0; if (nMaxCount>0 AND_IS_NOT_WIN9X) { int alloc_size=nMaxCount; // prevent large values of nMaxCount from allocating memory unless the underlying text is big too if (alloc_size > 512) { int l=GetWindowTextLengthW(hWnd); if (l>=0 && l < 512) alloc_size=1000; } { WIDETOMB_ALLOC(wbuf, alloc_size); if (wbuf) { GetWindowTextW(hWnd,wbuf,(int) (wbuf_size/sizeof(WCHAR))); if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,lpString,nMaxCount,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) lpString[nMaxCount-1]=0; WIDETOMB_FREE(wbuf); return (int)strlen(lpString); } } } return GetWindowTextA(hWnd,lpString,nMaxCount); }
static LRESULT WINAPI cb_newProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { WNDPROC oldproc = (WNDPROC)GetProp(hwnd,WDL_UTF8_OLDPROCPROP); if (!oldproc) return 0; if (msg==WM_NCDESTROY) { SetWindowLongPtr(hwnd, GWLP_WNDPROC,(INT_PTR)oldproc); RemoveProp(hwnd,WDL_UTF8_OLDPROCPROP); RemoveProp(hwnd,WDL_UTF8_OLDPROCPROP "W"); } else if (msg == CB_ADDSTRING || msg == CB_INSERTSTRING || msg == LB_ADDSTRING || msg == LB_INSERTSTRING) { char *str=(char*)lParam; if (lParam && WDL_HasUTF8(str)) { MBTOWIDE(wbuf,str); if (wbuf_ok) { WNDPROC oldprocW = (WNDPROC)GetProp(hwnd,WDL_UTF8_OLDPROCPROP "W"); LRESULT rv=CallWindowProcW(oldprocW ? oldprocW : oldproc,hwnd,msg,wParam,(LPARAM)wbuf); MBTOWIDE_FREE(wbuf); return rv; } MBTOWIDE_FREE(wbuf); } } else if (msg == CB_GETLBTEXT && lParam) { WNDPROC oldprocW = (WNDPROC)GetProp(hwnd,WDL_UTF8_OLDPROCPROP "W"); LRESULT l = CallWindowProcW(oldprocW ? oldprocW : oldproc,hwnd,CB_GETLBTEXTLEN,wParam,0); if (l != CB_ERR) { WIDETOMB_ALLOC(tmp,l+1); if (tmp) { LRESULT rv=CallWindowProcW(oldprocW ? oldprocW : oldproc,hwnd,msg,wParam,(LPARAM)tmp)+1; if (rv>=0) { *(char *)lParam=0; rv=WideCharToMultiByte(CP_UTF8,0,tmp,-1,(char *)lParam,((int)l+1)*3 + 32,NULL,NULL); if (rv>0) rv--; } WIDETOMB_FREE(tmp); return rv; } } } else if (msg == CB_GETLBTEXTLEN) { WNDPROC oldprocW = (WNDPROC)GetProp(hwnd,WDL_UTF8_OLDPROCPROP "W"); return CallWindowProcW(oldprocW ? oldprocW : oldproc,hwnd,msg,wParam,lParam) * 3 + 32; // make sure caller allocates a lot extra } return CallWindowProc(oldproc,hwnd,msg,wParam,lParam); }
int GetKeyNameTextUTF8(LONG lParam, LPTSTR lpString, int nMaxCount) { if (!lpString) return 0; if (nMaxCount>0 AND_IS_NOT_WIN9X) { WIDETOMB_ALLOC(wbuf, nMaxCount); if (wbuf) { GetKeyNameTextW(lParam,wbuf,(int) (wbuf_size/sizeof(WCHAR))); if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,lpString,nMaxCount,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) lpString[nMaxCount-1]=0; WIDETOMB_FREE(wbuf); return (int)strlen(lpString); } } return GetKeyNameTextA(lParam,lpString,nMaxCount); }
BOOL SHGetPathFromIDListUTF8(const struct _ITEMIDLIST *pidl, LPSTR pszPath, int pszPathLen) { if (pszPath AND_IS_NOT_WIN9X) { const int alloc_sz = pszPathLen < 4096 ? 4096 : pszPathLen; WIDETOMB_ALLOC(wfn,alloc_sz); if (wfn) { BOOL b = FALSE; if (SHGetPathFromIDListW(pidl,wfn)) { b = WideCharToMultiByte(CP_UTF8,0,wfn,-1,pszPath,pszPathLen,NULL,NULL) > 0; } WIDETOMB_FREE(wfn); return b; } } return SHGetPathFromIDListA(pidl,pszPath); }
UINT DragQueryFileUTF8(HDROP hDrop, UINT idx, char *buf, UINT bufsz) { if (buf && bufsz && idx!=-1 AND_IS_NOT_WIN9X) { const UINT reqsz = DragQueryFileW(hDrop,idx,NULL,0); WIDETOMB_ALLOC(wbuf, reqsz+32); if (wbuf) { UINT rv=DragQueryFileW(hDrop,idx,wbuf,(int)(wbuf_size/sizeof(WCHAR))); if (rv) { if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,buf,bufsz,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) buf[bufsz-1]=0; } WIDETOMB_FREE(wbuf); return rv; } } return DragQueryFileA(hDrop,idx,buf,bufsz); }
int DragQueryFileUTF8(HDROP hDrop, int idx, char *buf, int bufsz) { if (buf && bufsz && idx!=-1 && GetVersion()< 0x80000000) { int reqsz = DragQueryFileW(hDrop,idx,NULL,0)+32; WIDETOMB_ALLOC(wbuf, reqsz); if (wbuf) { int rv=DragQueryFileW(hDrop,idx,wbuf,wbuf_size/sizeof(WCHAR)); if (rv) { if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,buf,bufsz,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) buf[bufsz-1]=0; } WIDETOMB_FREE(wbuf); return rv; } } return DragQueryFileA(hDrop,idx,buf,bufsz); }
BOOL GetMenuItemInfoUTF8( HMENU hMenu,UINT uItem, BOOL fByPosition, LPMENUITEMINFO lpmii) { if (!lpmii) return FALSE; if ((lpmii->fMask & MIIM_TYPE) && lpmii->dwTypeData && lpmii->cch AND_IS_NOT_WIN9X) { MENUITEMINFOW tmp = *(MENUITEMINFOW*)lpmii; WIDETOMB_ALLOC(wbuf,lpmii->cch); if (wbuf) { BOOL rv; char *otd=lpmii->dwTypeData; int osz=lpmii->cbSize; tmp.cbSize=sizeof(tmp); tmp.dwTypeData = wbuf; tmp.cch = (UINT)(wbuf_size/sizeof(WCHAR)); rv=GetMenuItemInfoW(hMenu,uItem,fByPosition,&tmp); if (rv && (tmp.fType&(MFT_SEPARATOR|MFT_STRING|MFT_BITMAP)) == MFT_STRING) { if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,lpmii->dwTypeData,lpmii->cch,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) { lpmii->dwTypeData[lpmii->cch-1]=0; } *lpmii = *(MENUITEMINFO*)&tmp; // copy results lpmii->cbSize=osz; // restore old stuff lpmii->dwTypeData = otd; } else rv=0; WIDETOMB_FREE(wbuf); if (rv)return rv; } } return GetMenuItemInfoA(hMenu,uItem,fByPosition,lpmii); }
static LRESULT WINAPI tv_newProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { WNDPROC oldproc = (WNDPROC)GetProp(hwnd,WDL_UTF8_OLDPROCPROP); if (!oldproc) return 0; if (msg==WM_NCDESTROY) { SetWindowLongPtr(hwnd, GWLP_WNDPROC,(INT_PTR)oldproc); RemoveProp(hwnd,WDL_UTF8_OLDPROCPROP); } else if (msg == TVM_INSERTITEMA || msg == TVM_SETITEMA) { LPTVITEM pItem = msg == TVM_INSERTITEMA ? &((LPTVINSERTSTRUCT)lParam)->item : (LPTVITEM) lParam; char *str; if (pItem && (str=pItem->pszText) && (pItem->mask&TVIF_TEXT) && WDL_HasUTF8(str)) { MBTOWIDE(wbuf,str); if (wbuf_ok) { LRESULT rv; pItem->pszText=(char*)wbuf; // set new buffer rv=CallWindowProc(oldproc,hwnd,msg == TVM_INSERTITEMA ? TVM_INSERTITEMW : TVM_SETITEMW,wParam,lParam); pItem->pszText = str; // restore old pointer MBTOWIDE_FREE(wbuf); return rv; } MBTOWIDE_FREE(wbuf); } } else if (msg==TVM_GETITEMA) { LPTVITEM pItem = (LPTVITEM) lParam; char *obuf; if (pItem && (pItem->mask & TVIF_TEXT) && (obuf=pItem->pszText) && pItem->cchTextMax > 3) { WIDETOMB_ALLOC(wbuf,pItem->cchTextMax); if (wbuf) { LRESULT rv; int oldsz=pItem->cchTextMax; *wbuf=0; *obuf=0; pItem->cchTextMax=(int) (wbuf_size/sizeof(WCHAR)); pItem->pszText = (char *)wbuf; rv=CallWindowProc(oldproc,hwnd,TVM_GETITEMW,wParam,lParam); if (!WideCharToMultiByte(CP_UTF8,0,wbuf,-1,obuf,oldsz,NULL,NULL) && GetLastError()==ERROR_INSUFFICIENT_BUFFER) obuf[oldsz-1]=0; pItem->cchTextMax=oldsz; pItem->pszText=obuf; WIDETOMB_FREE(wbuf); if (obuf[0]) return rv; } } } return CallWindowProc(oldproc,hwnd,msg,wParam,lParam); }