用vc实现隐藏系统托盘图标

      这几天在做一个msn隐藏的工具时,想实现隐藏msn系统托盘的图标,在网上搜索了一下,发现有人用vb实现的代码,依样画葫芦弄到了vc里面,但是发现我的到的托盘图标的描述基本上是乱码。今天在看ATL的东西时突然想起会不会是字串格式的问题呢?查msdn后发现有一个String Conversion Macro的主题,在使用了OLE2T后(还需要一个USES_CONVERSION;不然会有编译错误),终于得到了在vb中一样的字符串了。^O^ 

islq 2006-3-19

完整代码如下:

void CMyDlg::MoveTray(BOOL flag)
{
 // TODO: Add your control notification handler code here
 HWND hWnd,hWndTmp;
 unsigned long lngPID;
 long ret,lngButtons;
 HANDLE hProcess;
 LPVOID lngAddress;
 long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;
 char strBuff[1024]={0};
 char* str = NULL;

    hWnd = ::FindWindow("Shell_TrayWnd", NULL);
    hWnd = ::FindWindowEx(hWnd, 0, "TrayNotifyWnd", NULL);
    hWndTmp = ::FindWindowEx(hWnd, 0, "SysPager", NULL);
 if(!hWndTmp)
  hWnd = ::FindWindowEx(hWnd, 0, "ToolbarWindow32", NULL);
 else
  hWnd = ::FindWindowEx(hWndTmp, 0, "ToolbarWindow32", NULL);
    TRACE("/nhWnd=%x/n",hWnd);
    ret = ::GetWindowThreadProcessId(hWnd, &lngPID);
    hProcess = ::OpenProcess(PROCESS_ALL_ACCESS
       |PROCESS_VM_OPERATION
       |PROCESS_VM_READ
       |PROCESS_VM_WRITE,
       0,
       lngPID);
    lngAddress = VirtualAllocEx(hProcess,  0,  0x4096, MEM_COMMIT, PAGE_READWRITE);//0x02e60000
    lngButtons = ::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);

    for(int i = 0 ;i< lngButtons - 1;i++){
        ret = ::SendMessage(hWnd,TB_GETBUTTON,i,long(lngAddress));
        ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16), &lngTextAdr, 4, 0);
        if(lngTextAdr != -1){
            ret = ::ReadProcessMemory(hProcess, LPVOID(lngTextAdr),  strBuff,  1024,  0);
            ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),  &lngHwndAdr,  4,  0);
            ret = ::ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),  &lngHwnd,  4,  0);
            ret = ::ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4),  &lngButtonID,  4,  0);

   USES_CONVERSION;
   str = OLE2T((LPOLESTR)(strBuff));
//   MessageBox((const char*)(strTmp),NULL,MB_OK);//Just for test
   if(strstr(str,"Messenger")>0){
    if(flag){
     ::SendMessage(hWnd, TB_HIDEBUTTON , lngButtonID, 1);
    }
    else{
     ::SendMessage(hWnd, TB_HIDEBUTTON , lngButtonID, 0);
    }
   }
      }
    }
    ::VirtualFreeEx( hProcess,  lngAddress,  0X4096, MEM_RELEASE);
    ::CloseHandle(hProcess);
}

一下是从网上找到的参考的vb代码

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1
      Caption         =   "Command1"
      Height          =   375
      Left            =   2760
      TabIndex        =   1
      Top             =   2280
      Width           =   1455
   End
   Begin VB.ListBox List1
      Height          =   1860
      Left            =   360
      TabIndex        =   0
      Top             =   240
      Width           =   3855
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Declare Function OpenProcess Lib "KERNEL32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Private Const READ_CONTROL As Long = &H20000
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const STANDARD_RIGHTS_READ As Long = (READ_CONTROL)
Private Const STANDARD_RIGHTS_EXECUTE As Long = (READ_CONTROL)
Private Const STANDARD_RIGHTS_ALL As Long = &H1F0000
Private Const STANDARD_RIGHTS_WRITE As Long = (READ_CONTROL)
Private Const SYNCHRONIZE As Long = &H100000
Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
Private Const PROCESS_TERMINATE As Long = (&H1)
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Const WM_USER As Long = &H400
Private Declare Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Const TB_BUTTONCOUNT As Long = (WM_USER + 24)
Private Const TB_HIDEBUTTON As Long = (WM_USER + 4)
Private Const TB_GETBUTTON As Long = (WM_USER + 23)
Private Const TB_GETBITMAP As Long = (WM_USER + 44)
Private Const TB_DELETEBUTTON As Long = (WM_USER + 22)
Private Const TB_ADDBUTTONS As Long = (WM_USER + 20)
Private Const TB_INSERTBUTTON As Long = (WM_USER + 21)
Private Const TB_ISBUTTONHIDDEN As Long = (WM_USER + 12)
Private Const ILD_NORMAL As Long = &H0
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Const PROCESS_VM_OPERATION As Long = (&H8)
Private Const PROCESS_VM_READ As Long = (&H10)
Private Const PROCESS_VM_WRITE As Long = (&H20)
Private Const MEM_RESERVE As Long = &H2000
Private Const MEM_COMMIT As Long = &H1000
Private Const MEM_RELEASE As Long = &H8000
Private Const PAGE_READWRITE As Long = &H4

Private Sub Command1_Click()
    Dim lngTemp As Long
    Dim lngTray
    Dim lngPID As Long
    Dim lngPID2 As Long
    Dim hProcess As Long
    Dim lngProcess As Long
    Dim lngAddress As Long
    Dim lngCount As Long
    Dim lngButtons As Long
    Dim ret As Long
    Dim lngTextAdr As Long
    Dim lngHwndAdr As Long
    Dim lngButtonID As Long
    Dim hIcon As Long
    Dim lngHwnd As Long
    Dim lpFileName As String * 1024
    Dim i As Long
    Dim strBuff(1024) As Byte
    Dim strText As String
    Dim lngTrayDC As Long
    lngTemp = FindWindow("Shell_TrayWnd", vbNullString)
    lngTemp = FindWindowEx(lngTemp, 0, "TrayNotifyWnd", vbNullString)
    lngTemp = FindWindowEx(lngTemp, 0, "SysPager", vbNullString)
    lngTray = FindWindowEx(lngTemp, 0, "ToolbarWindow32", vbNullString)
    Debug.Print "lngTray="; lngTray
    ret = GetWindowThreadProcessId(lngTray, lngPID)
    hProcess = OpenProcess(PROCESS_ALL_ACCESS Or PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, lngPID)
    lngAddress = VirtualAllocEx(hProcess, ByVal 0&, ByVal 4096&, MEM_COMMIT, PAGE_READWRITE)
    lngButtons = SendMessage(lngTray, TB_BUTTONCOUNT, 0, 0)

    For i = 0 To lngButtons - 1
        ret = SendMessage(lngTray, TB_GETBUTTON, ByVal i, ByVal lngAddress)
        ret = ReadProcessMemory(hProcess, ByVal lngAddress + 16, ByVal VarPtr(lngTextAdr), ByVal 4, ByVal 0&)
        If lngTextAdr <> -1 Then
            ret = ReadProcessMemory(hProcess, ByVal lngTextAdr, ByVal VarPtr(strBuff(0)), ByVal 1024, ByVal 0&)
            ret = ReadProcessMemory(hProcess, ByVal lngAddress + 12, ByVal VarPtr(lngHwndAdr), ByVal 4, ByVal 0&)
            ret = ReadProcessMemory(hProcess, ByVal lngHwndAdr, ByVal VarPtr(lngHwnd), ByVal 4, ByVal 0&)
            ret = ReadProcessMemory(hProcess, ByVal lngAddress + 4, ByVal VarPtr(lngButtonID), ByVal 4, ByVal 0&)

            strText = ConverNull(strBuff)
            'lngHwnd 所属窗口handle
            'strText 托盘图标文字
            List1.AddItem lngHwnd & " - " & strText
        End If
    Next i
    VirtualFreeEx hProcess, ByVal lngAddress, ByVal 4096&, MEM_RELEASE
    CloseHandle hProcess
End Sub

Private Sub Form_Load()
Command1.Caption = "&Enumerate"
End Sub

Function ConverNull(ByVal s As String) As String
    Dim nullpos As Long
    nullpos = InStr(s, Chr$(0))
    If nullpos > 0 Then
        ConverNull = Left(s, nullpos - 1)
    Else
        ConverNull = s
    End If
End Function

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值