To see your application icon on the status area of task bar

June 5, 2008

Do you want to see your application icon on the status area of task bar (we can call this area – “Tray”)?

 

 

I am offering a little help. In this post I will give you some code for

Adding your application icon on the tray.

Show balloon for your icon

Show menu on your icon

Remove the icon

 

To achieve it, we have the API Shell_NotifyIcon

 

Now add a member variable for the structure NOTIFYICONDATA and for CMenu

 

NOTIFYICONDATA m_TrayData;

CMenu ObjMenu;

 

Also define a message

 

#define WM_TRAY_MESSAGE (WM_USER + 1)

 

 

Add icon to tray

 

BOOL CTrayDlg::OnInitDialog()

{

    …………………

    // IDR_MENU1 – Required menu which is added in resource

    ObjMenu.LoadMenu( IDR_MENU1 );

    MoveToTray();

}

 

void CTrayDlg::MoveToTray()

{

   m_TrayData.cbSize = sizeof(NOTIFYICONDATA);

   CString cstemp = L“How is it?” ;

    swprintf( m_TrayData.szTip, L“%s”, cstemp );

    m_TrayData.hWnd = m_hWnd;

    m_TrayData.uID = 1;

    m_TrayData.uCallbackMessage  = WM_TRAY_MESSAGE;

    m_TrayData.hIcon = this->m_hIcon;

    m_TrayData.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP ;

    Shell_NotifyIcon( NIM_ADD, &m_TrayData );

}

 

Show balloon on the icon

void CTrayDlg::OnButtonShowBalloon()

{

    ShowBalloon( L“I am running in this system”, L“Ajeesh”, 1 );

}

 

void CTrayDlg::ShowBalloon( LPCTSTR szText, LPCTSTR szTitle, UINT uTimeout )

{

            m_TrayData.uFlags = NIF_INFO;

            _tcsncpy( m_TrayData.szInfo, szText, 256 );

            _tcsncpy( m_TrayData.szInfoTitle, szTitle, 64 );

            m_TrayData.dwInfoFlags = NIIF_INFO;

            m_TrayData.uTimeout = uTimeout;

            Shell_NotifyIcon( NIM_MODIFY, &m_TrayData );

}

 

Show menu on your icon

For this we need a message mapped function

 

ON_MESSAGE(WM_TRAY_MESSAGE, OnTrayNotify)

 

void CTrayDlg::OnTrayNotify(WPARAM wParam, LPARAM lParam)

{

    UINT uID;

    UINT uMsg;

    uID = (UINT) wParam;

    uMsg = (UINT) lParam;

    if (uID != 1)

    {

        return;

    }

    switch (uMsg )

    {

    // Show your application on LBtnDBClick on icon

    case WM_LBUTTONDBLCLK:

        ::SetWindowPos( m_hWnd, HWND_TOPMOST,

                                0, 0, 0, 0,

                                SWP_NOMOVE|SWP_NOSIZE );

        ::SetWindowPos( m_hWnd, HWND_NOTOPMOST,

                                0, 0, 0, 0,

                                SWP_NOMOVE|SWP_NOSIZE );

        ShowWindow( SW_SHOW );

        ShowWindow( SW_RESTORE );

        break;

    // Show Menu on RBtnClick on icon

    case WM_RBUTTONDOWN:

    case WM_CONTEXTMENU:;

        CPoint pt;

        GetCursorPos(&pt);

        ObjMenu.GetSubMenu(0)->TrackPopupMenu( TPM_BOTTOMALIGN|

                                               TPM_LEFTBUTTON|

                                               TPM_RIGHTBUTTON,

                                               pt.x,pt.y,this );

    }

    return;

}

 

Remove icon from the tray

void CTrayDlg::OnCancel()

{

    Shell_NotifyIcon( NIM_DELETE, &m_TrayData );

    CDialog::OnCancel();

}

Get the servers information added in a domain

June 3, 2008

Here is an API – NetServerEnum. This API will help you to get the servers information which is added in a domain. Of course, you will get more information and sample code from MSDN about this API.

 

I am just trying to help you that how you can use this API to get the system names in a domain. One sample code is given below. This code will list all the system names which are added in the domain.

 

//////////////////////////////////////////////////////////////////////

LPSERVER_INFO_101 pBuf = NULL;

LPSERVER_INFO_101 pTmpBuf;

DWORD dwEntriesRead = 0;

DWORD dwTotalEntries = 0;

DWORD dwResumeHandle = 0;

NET_API_STATUS nStatus;

nStatus = NetServerEnum(NULL,

                        101,

                        (LPBYTE *) &pBuf,

                        -1,

                        &dwEntriesRead,

                        &dwTotalEntries,

                        SV_TYPE_SERVER,

                        NULL,

                        &dwResumeHandle);

if((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))

{

   if((pTmpBuf = pBuf) != NULL)

   {

      for( DWORD i = 0; i < dwEntriesRead; i++ )

      {

            wchar_t buff[50];

            swprintf(buff,L“%s”, pTmpBuf->sv101_name);

            // Server names are added to combobox

            m_SystemNameCombo.AddString(buff);

            pTmpBuf++;  

      }

   }

} 

NetApiBufferFree(pBuf);

//////////////////////////////////////////////////////////////////////

To Build: include Lm.h and Netapi32.lib

Get information about the services in your machine

May 28, 2008

If you want to get the information about the services that are running in your system, I hope the below APIs will be helpful for you.

 

OpenSCManager – Opens service control manager database.

EnumServicesStatus – Provides name and status of each service

OpenService Opens the specified service.

QueryServiceStatus / QueryServiceStatusEx – Service status and other information of specified service

 

Given below is an example for checking the service status of the service whose display name is “Indexing Service” in services.msc.

 

/////////////////////////////////////////////////////////////////////////////

SC_HANDLE schSCManager;

SC_HANDLE schService;

// Get the handle of SC manager

schSCManager = OpenSCManager( 0, 0, SC_MANAGER_ALL_ACCESS );

if( 0 == schSCManager )

{

    return;

}

// Get the handle to “Indexing Service” service

// Service name “CiSvc” – got from registry

// HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services

CString csServiceName = _T( “CiSvc” );

schService = OpenService( schSCManager, csServiceName, SERVICE_ALL_ACCESS );

if( 0 == schService )

{

    CloseServiceHandle( schSCManager );

    return;

}

SERVICE_STATUS lpServiceStatus;

// Get the service status

if( !QueryServiceStatus( schService, &lpServiceStatus ))

{

    CloseServiceHandle( schSCManager );

    CloseServiceHandle( schService);

    return;

}

// Check if service is running or not

if( SERVICE_RUNNING == lpServiceStatus.dwCurrentState )

{

    AfxMessageBox( “Indexing Service is running” );

}

else

{

    AfxMessageBox( “Indexing Service is not running” );

}

CloseServiceHandle( schSCManager );

CloseServiceHandle( schService );

/////////////////////////////////////////////////////////////////////////////

 

Don’t play too much with these APIs J. Use only if necessary.