哪位高手帮忙看看这段管理系统托盘的代码为什么总是运行不正确???
HideSysTrayIcon( CString& ATrayHint)
{
HWND hTrayWnd;
DWORD dwProcessID;
HANDLE hSourceHandle;
HANDLE hTargetHandle;
LPVOID pStart;
LPVOID pBufStart;
int Result;
DWORD dwAllocSize;
const int TextBufSize = 128;
///////////////////
hTrayWnd = ::FindWindow("Shell_TrayWnd",NULL);
hTrayWnd = ::FindWindowEx(hTrayWnd,0,"TrayNotifyWnd",NULL);
hTrayWnd = ::FindWindowEx(hTrayWnd,0,"SysPager",NULL);
hTrayWnd = ::FindWindowEx(hTrayWnd,0,"ToolbarWindow32",NULL);
GetWindowThreadProcessId(hTrayWnd,&dwProcessID);
hSourceHandle = OpenProcess(PROCESS_ALL_ACCESS,false,dwProcessID);
if(hSourceHandle == NULL)
{
::MessageBox(NULL,"hSourceHandle == NULL","Alert",MB_OK);
return;
}
Result = DuplicateHandle(GetCurrentProcess(),hSourceHandle,GetCurrentProcess(),
&hTargetHandle,PROCESS_ALL_ACCESS,false,0);
if(Result == 0)
{
::MessageBox(NULL,"Result == 0","Alert",MB_OK);
return;
}
dwAllocSize = sizeof(TBBUTTONINFO);
pStart = VirtualAllocEx(hTargetHandle,NULL,dwAllocSize,MEM_COMMIT,PAGE_READWRITE);
pBufStart = VirtualAllocEx(hTargetHandle,NULL,TextBufSize,MEM_COMMIT,PAGE_READWRITE);
if(pStart == NULL || pBufStart == NULL)
{
::MessageBox(NULL,"pStart == NULL || pBufStart == NULL","Alert",MB_OK);
return;
}
int ButtonCount;
// TBBUTTONINFO *Button;
// byte *TempData;
TBBUTTONINFO tbi;
byte *TempBuf;
DWORD dwWrite,dwRead;
ButtonCount = ::SendMessage(hTrayWnd,TB_BUTTONCOUNT,0,0) + 30;//随便多加几个数
for(int i=0; i < ButtonCount; i++)
{
tbi.cbSize = dwAllocSize;
tbi.dwMask = TBIF_TEXT;
tbi.pszText = (LPTSTR)pBufStart;
WriteProcessMemory(hTargetHandle,pStart,(LPVOID)&tbi,dwAllocSize,&dwWrite);
TempBuf = new byte[TextBufSize];
ZeroMemory(TempBuf,TextBufSize);
WriteProcessMemory(hTargetHandle,pBufStart,(LPVOID)TempBuf,TextBufSize,&dwWrite);
/////////////
::SendMessage(hTrayWnd,TB_GETBUTTONINFO,i,(LPARAM)((TBBUTTONINFO*)pStart));// Button);
/////////////
// TempData = new byte[dwAllocSize];
// ReadProcessMemory(hTargetHandle,(LPCVOID)pStart,TempData,dwAllocSize,&dwRead);
// Button = (TBBUTTONINFO*)TempData;
// ....
// delete[] TempData;
ReadProcessMemory(hTargetHandle,(LPCVOID)pBufStart,(LPVOID)TempBuf,TextBufSize,&dwRead);
ATrayHint.MakeLower();
CString temp=TempBuf;
temp.MakeLower();
// if( > 0)
{
// Memo1->Lines->Add("Delete now");
//删除(图标)按钮
Result = ::SendMessage(hTrayWnd,TB_DELETEBUTTON,i,0);
if(Result)
//Memo1->Lines->Add("Succeed in deleting Icon");
::MessageBox(NULL,"Succeed in deleting Icon","Alert",MB_OK);
else
//Memo1->Lines->Add("Fail to deleting Icon");
::MessageBox(NULL,"Fail to deleting Icon","Alert",MB_OK);
delete[] TempBuf;
break;
}
delete[] TempBuf;
}
VirtualFreeEx(hTargetHandle,pStart,dwAllocSize,MEM_DECOMMIT);
VirtualFreeEx(hTargetHandle,pBufStart,TextBufSize,MEM_DECOMMIT);
CloseHandle(hTargetHandle);
CloseHandle(hSourceHandle);
}