📄 util.cpp
字号:
RECT rcCaret;
} GUITHREADINFO, *PGUITHREADINFO;
#endif //WINVER < 0x0500
/*WINUSERAPI BOOL WINAPI
GetGUIThreadInfo(
DWORD idThread,
PGUITHREADINFO pgui);
*/
typedef WINUSERAPI BOOL WINAPI GETGUITHREADINFO
(DWORD idThread, PGUITHREADINFO pgui);
typedef GETGUITHREADINFO * LPGETGUITHREADINFO;
HWND GetWindow(DWORD dwWhichWindow)
{
DWORD dwRet=0;
BOOL bRet=TRUE;
char szClassName[255]={0};
GUITHREADINFO gtiThreadInfo={0};
gtiThreadInfo.cbSize=sizeof(GUITHREADINFO);
HWND hWndRet=NULL;
HMODULE hModUser32 = LoadLibrary("user32");
LPGETGUITHREADINFO pGetGuiThreadInfo=(LPGETGUITHREADINFO)
GetProcAddress(hModUser32, "GetGUIThreadInfo");
pgpAssert(NULL != pGetGuiThreadInfo);
if(NULL == pGetGuiThreadInfo)
goto method_2;//95 does not support this method
bRet=(*pGetGuiThreadInfo)(GetCurrentThreadId(), >iThreadInfo);
pgpAssert(bRet != FALSE);
if(FALSE == bRet)
{
dwRet=GetLastError();
PgpGwTrace("GetGuiThreadInfo failed - because of error %d\n", dwRet);
goto method_2;
}
else
{
//we succeeded in getting the thread info
//PgpGwTrace("gtiThreadInfo.hwndActive=%#x\n", gtiThreadInfo.hwndActive);
//PgpGwTrace(" hwndFocus=%#x\n", gtiThreadInfo.hwndFocus);
//PgpGwTrace(" hwndCapture=%#x\n", gtiThreadInfo.hwndCapture);
//PgpGwTrace(" hwndMenuOwner=%#x\n", gtiThreadInfo.hwndMenuOwner);
//PgpGwTrace(" hwndMoveSize=%#x\n", gtiThreadInfo.hwndMoveSize);
//PgpGwTrace(" hwndCaret=%#x\n", gtiThreadInfo.hwndCaret);
}
hWndRet=gtiThreadInfo.hwndActive;
if(NULL == hWndRet)//activewindow is null
hWndRet=gtiThreadInfo.hwndFocus;
if(NULL == hWndRet)//active and focus windows are null
hWndRet=gtiThreadInfo.hwndCaret;
method_2:
if(NULL == hWndRet)//active, focus and caret windows are null
{
hWndRet=GetForegroundWindow();//get the foregroundwindow
PgpGwTrace("GetForegroundWindow() returned %#x\n", hWndRet);
}
if((GWO_CURRENTMAIL_WINDOW == dwWhichWindow) ||
(GWO_CURRENTREBAR_WINDOW == dwWhichWindow))
{
pgpAssert(NULL != hWndRet);
if(NULL != hWndRet)//if we are returning a valid window handle
{
//make sure it is a mail window
int iRet=0;
do
{
//clean up the buf
ZeroMemory(szClassName, sizeof(szClassName));
//get the window class name
pgpAssert(NULL != hWndRet);
iRet=GetClassName(hWndRet, szClassName,
sizeof(szClassName)-sizeof(char));
pgpAssert(iRet > 0);
}
while((0 != strcmp(szClassName, MAILVIEWWINDOWCLASSNAMEA)) &&//until we get a mail window
(NULL != (hWndRet=GetParent(hWndRet))));//we switch to the parent of the window
}
if(GWO_CURRENTMAIL_WINDOW == dwWhichWindow)
{
if(NULL == hWndRet)
PgpGwTrace("WARNING: current mail window not found !\n");
return hWndRet;
}
//if we have not got the current mail window
if(NULL == hWndRet)
{
//it may be that the mail window is not active yet- as in the
//case of us being called during addition of toolbar items
//we try to find all the mail windows under desktop window
hWndRet=FindWindowEx(GetDesktopWindow(), NULL,
MAILVIEWWINDOWCLASSNAMEA, NULL);
//if there is an invisible mail window - that is the mail window
//we are looking for !
while(NULL != hWndRet)
{
if(!IsWindowVisible(hWndRet))//if it is invisible
break;//we have found the soon to be current mail window
//get the next mail window
hWndRet=FindWindowEx(GetDesktopWindow(), hWndRet,
MAILVIEWWINDOWCLASSNAMEA, NULL);
}
}
//get the rebar window child of that window
if(NULL != hWndRet)
hWndRet=FindWindowEx(hWndRet, NULL, REBARCLASSNAME, NULL);
pgpAssert(NULL != hWndRet);
return hWndRet;
}
else if(GWO_CURRENTATTVIEWER_WINDOW == dwWhichWindow)
{
pgpAssert(NULL != hWndRet);
if(NULL != hWndRet)//if we are returning a valid window handle
{
//make sure it is a attachment viewer window
int iRet=0;
do
{
//clean up the buf
ZeroMemory(szClassName, sizeof(szClassName));
//get the window class name
pgpAssert(NULL != hWndRet);
iRet=GetClassName(hWndRet, szClassName,
sizeof(szClassName)-sizeof(char));
pgpAssert(iRet > 0);
}
while((0 != strcmp(szClassName, ATTACHMENTVIEWERWINDOWCLASSNAMEA)) &&//until we get the attachment viewer window
(NULL != (hWndRet=GetParent(hWndRet))));//we switch to the parent of the window
}
return hWndRet;
}
else if(GWO_CURRENTTOPLEVEL_WINDOW == dwWhichWindow)
{
pgpAssert(NULL != hWndRet);
if(NULL != hWndRet)//if we are returning a valid window handle
{
//make sure it is the top level window
HWND hWndParent=NULL;
while(NULL != (hWndParent=GetParent(hWndRet)))
hWndRet=hWndParent;
}
pgpAssert(GetCurrentThreadId() == GetWindowThreadProcessId(hWndRet, NULL));//a process id check is more accurate
return hWndRet;
}
return hWndRet;
}
BOOL SafeLoadString(UINT uiResourceId, char **ppszString)
{
pgpAssert(NULL != uiResourceId);
pgpAssert((NULL != ppszString) && (NULL == *ppszString));
if((NULL == uiResourceId) || (NULL == ppszString))
return FALSE;
DWORD dwAllocSize=255;//initial allocation size
DWORD dwAllocIncrement=255;//allocation increment
BOOL bRet=FALSE;
UINT uiRet=0;
void *pTmpBuf=NULL;
*ppszString=NULL;//if the caller had a string in there, well bad luck !!
*ppszString=(char *)calloc(dwAllocSize, sizeof(char));
pgpAssert(NULL != *ppszString);
if(NULL == *ppszString)
return FALSE;//initial allocation failed !!
while(TRUE)
{
uiRet=LoadString(g_hInstance, uiResourceId, *ppszString, dwAllocSize-sizeof(char));
if(0 == uiRet)//string not found ?
{
pgpAssert(FALSE);
PgpGwTrace("String %d not found in resources. LoadString failed returning error: %d\n",
uiResourceId, GetLastError());
break;
}
pgpAssert(uiRet <= (dwAllocSize-sizeof(char)-1));
if(uiRet < (dwAllocSize-sizeof(char)-1))
{
bRet=TRUE;//we have got the whole string (success)
break;
}
//string must have been as long as the max we passed
pgpAssert(uiRet == (dwAllocSize-sizeof(char)-1));
//increment the allocation size and allocate again
dwAllocSize += dwAllocIncrement;
pTmpBuf=realloc(*ppszString, dwAllocSize);
if(NULL == pTmpBuf)//failed to allocate more
break;
*ppszString=(char *)pTmpBuf;//switch to the new buffer
}
//if we did not get the string in its entirety
if((FALSE == bRet) && (NULL != *ppszString))
{
//free the buffer
free(*ppszString);
*ppszString=NULL;
}
return bRet;
}
BOOL PgpGwGetBoolPref(PGPPrefIndex ppiPrefToQuery, PGPBoolean& refpbPrefValue)
{
PGPBoolean pgpbFlag=FALSE;
PGPPrefRef pgpprPref=NULL;
PGPError pgpErrRet=kPGPError_NoErr;
//get a ref to the prefs from the client lib
pgpErrRet=PGPclPeekClientLibPrefRefs(&pgpprPref, NULL);
pgpAssert(!IsPGPError(pgpErrRet) && (NULL != pgpprPref));
if (IsPGPError(pgpErrRet) || (NULL == pgpprPref))
return FALSE; //failed
//get the pref proper
pgpErrRet=PGPGetPrefBoolean(pgpprPref, ppiPrefToQuery, &refpbPrefValue);
pgpAssert(!IsPGPError(pgpErrRet));
if(!IsPGPError(pgpErrRet))
return TRUE;//succeeded
return FALSE;//failed
}
//function to do extended license checking for this plugin in addition to
//license checking in shared code during encrypt/decrypt operations
BOOL PgpGwExLicenseChk()
{
BOOL bRet = FALSE;
PGPError pgpErrRet=kPGPError_NoErr;
//check if the user has license to run us. we must have a valid license
//with desktop flag turned on in the license number for pgp operations
//to be carried out.
if(FALSE == (bRet = PGPlnDesktop()))
{
//let her know of license requirements and lead to buy.
pgpErrRet=PGPclNoLicense(NULL, kPGPclGroupwisePlugin);
pgpAssert(IsntPGPError(pgpErrRet));
}
return bRet;
}
void PgpGwTrace(LPCTSTR lpszFormat, ...)
{
#ifdef _DEBUG
if (!g_dwModuleOptions&GWMO_TRACE)
return;
va_list args;
va_start(args, lpszFormat);
int nBuf;
char szBuffer[512]={0};
nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
// was there an error? was the expanded string too long?
//pgpAssert(nBuf >= 0);
if(nBuf >= 0)
OutputDebugString(szBuffer);
va_end(args);
#endif
}
void TraceRefCount(LPUNKNOWN lpUnkIntfPtr)
{
#ifdef _DEBUG
if(NULL != lpUnkIntfPtr)
{
ULONG ulCount=lpUnkIntfPtr->AddRef();
ulCount=lpUnkIntfPtr->Release();
PgpGwTrace("Interface pointer %#x has ref count %d\n", lpUnkIntfPtr, ulCount);
}
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -