본문 바로가기

Windows/MFC

USB Memory Stick Tester

Link : http://sourceforge.net/project/downloading.php?group_id=131835&use_mirror=nchc&filename=USB_Stick_Tester_0.1.source.zip&932190

// USB Stick TesterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "USB Stick Tester.h"
#include "USB Stick TesterDlg.h"
#include <process.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUSBStickTesterDlg dialog

CUSBStickTesterDlg::CUSBStickTesterDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CUSBStickTesterDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CUSBStickTesterDlg)
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUSBStickTesterDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CUSBStickTesterDlg)
    DDX_Control(pDX, IDC_SLIDER_PATTERNS, m_patterns_slider);
    DDX_Control(pDX, IDC_LIST_SIZES, m_listsizes);
    DDX_Control(pDX, IDC_LIST_ERRORS, m_error_list);
    DDX_Control(pDX, IDC_STATIC3, m_static3);
    DDX_Control(pDX, IDC_PROGRESS3, m_progress3);
    DDX_Control(pDX, IDC_LIST_DISKS, m_disks_list);
    DDX_Control(pDX, IDC_STATIC2, m_static2);
    DDX_Control(pDX, IDC_STATIC1, m_static1);
    DDX_Control(pDX, IDC_PROGRESS2, m_progress2);
    DDX_Control(pDX, IDC_PROGRESS1, m_progress1);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUSBStickTesterDlg, CDialog)
    //{{AFX_MSG_MAP(CUSBStickTesterDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_LBN_SELCHANGE(IDC_LIST_DISKS, OnSelchangeListDisks)
    ON_BN_CLICKED(IDC_BUTTON_ABORT, OnButtonAbort)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUSBStickTesterDlg message handlers

#define MAX_NUM_PATTERNS 4
unsigned char patterns[MAX_NUM_PATTERNS] = {0xAA, 0x55, 0x00, 0xFF};


BOOL CUSBStickTesterDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog. The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    DWORD disk_mask = GetLogicalDrives();
    char buffer[1000];
    DWORD disks_size = GetLogicalDriveStrings(999, buffer);
    char * tok = buffer;

    while (tok[0])
    {
        if (GetDriveType(tok) == DRIVE_REMOVABLE)
        {
            if ( strcmpi(tok,"A:\\") && strcmpi(tok,"B:\\") )
                m_disks_list.AddString(tok);
        }
        tok += strlen(tok) + 1;
    }

    m_selected_drive = m_disks_list.GetCount() - 1;
    m_disks_list.SetCurSel(m_selected_drive);

    m_listsizes.ResetContent();
    m_listsizes.AddString("0.1%");
    m_listsizes.AddString("1%");
    m_listsizes.AddString("5%");
    m_listsizes.AddString("10%");
    m_listsizes.AddString("25%");
    m_listsizes.AddString("50%");
    m_listsizes.AddString("75%");
    m_listsizes.AddString("100%");
    m_listsizes.SetCurSel(m_listsizes.GetCount() - 1);

    m_patterns_slider.SetRange(1,MAX_NUM_PATTERNS);
    m_patterns_slider.SetPos(MAX_NUM_PATTERNS);

    return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CUSBStickTesterDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CUSBStickTesterDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void RunTests(void * data)
{
    CUSBStickTesterDlg * dlg = (CUSBStickTesterDlg *)data;

    dlg->m_num_patterns = dlg->m_patterns_slider.GetPos();

    dlg->m_error_list.ResetContent();

    CString disk_path;
    dlg->m_disks_list.GetText(dlg->m_selected_drive, disk_path);
    CString test_file = disk_path + "test.bin";

    DeleteFile((LPCSTR)test_file);

    DWORD SectorsPerCluster;
    DWORD BytesPerSector;
    DWORD NumberOfFreeClusters;
    DWORD TotalNumberOfClusters;

    GetDiskFreeSpace((LPCSTR)disk_path,&SectorsPerCluster,&BytesPerSector,&NumberOfFreeClusters, &TotalNumberOfClusters);

    DWORD FreeNumberOfSectors = NumberOfFreeClusters * SectorsPerCluster;
    DWORD ClusterSize = BytesPerSector * SectorsPerCluster;

    //NumberOfFreeClusters = 10;
    CString cs_percent_to_use;
    float percent_to_use;
    dlg->m_listsizes.GetText(dlg->m_listsizes.GetCurSel(), cs_percent_to_use);
    percent_to_use = atof((LPCSTR)cs_percent_to_use);
    NumberOfFreeClusters = NumberOfFreeClusters * percent_to_use / 100;


    unsigned char * write_buffer;
    unsigned char * read_buffer;

    write_buffer = (unsigned char *) VirtualAlloc(NULL, ClusterSize, MEM_COMMIT, PAGE_READWRITE);
    read_buffer = (unsigned char *) VirtualAlloc(NULL, ClusterSize, MEM_COMMIT, PAGE_READWRITE);

    dlg->m_progress1.SetRange(0,dlg->m_num_patterns);
    dlg->m_progress1.SetStep(1);
    dlg->m_progress1.SetPos(0);

    dlg->m_progress2.SetPos(0);
    dlg->m_progress3.SetPos(0);

    int i,j,k;
    for (i=0;i<dlg->m_num_patterns;i++)
    {
        if (dlg->mb_isabort) break;

        CString overall_message;
        overall_message.Format("Test %d of %d",i+1,dlg->m_num_patterns);
        dlg->m_static1.SetWindowText(overall_message);
        dlg->UpdateWindow();

        for (k=0;k<ClusterSize;k++) write_buffer[k] = patterns[i];
        dlg->m_progress1.SetPos(i+1);

        dlg->m_progress2.SetRange32(0,NumberOfFreeClusters);
        dlg->m_progress3.SetRange32(0,NumberOfFreeClusters);
        dlg->m_progress2.SetStep(1);
        dlg->m_progress3.SetStep(1);
        dlg->m_progress2.SetPos(0);
        dlg->m_progress3.SetPos(0);

        // write test pattern to the file
        DeleteFile((LPCSTR)test_file);
        HANDLE hfout = CreateFile((LPCSTR)test_file, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
        for (j=0;j<NumberOfFreeClusters;j++)
        {
            if (dlg->mb_isabort) break;
            DWORD BytesWritten;
            WriteFile(hfout, write_buffer, ClusterSize, &BytesWritten, NULL);
            dlg->m_progress2.SetPos(j);
            dlg->UpdateWindow();
        }
        FlushFileBuffers(hfout);
        CloseHandle(hfout);

        // read test pattern from the file
        if (dlg->mb_isabort) break;
        HANDLE hfin = CreateFile((LPCSTR)test_file, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING , NULL);
        for (j=0;j<NumberOfFreeClusters;j++)
        {
            if (dlg->mb_isabort) break;
            DWORD BytesRead;
            ReadFile(hfin, read_buffer, ClusterSize, &BytesRead, NULL);
            dlg->m_progress3.SetPos(j);
            dlg->UpdateWindow();
            if ( memcmp(read_buffer, write_buffer, ClusterSize) )
            {
                int num_errors = 0;
                int k;
                for (k=0;k<ClusterSize;k++)
                {
                    if (read_buffer[k] != patterns[i])
                        num_errors ++;
                }

                CString s;
                s.Format("Pass %d Mask=0x%.2x Addr=%.4d Total=%.4d",i+1,patterns[i], j,num_errors);
                dlg->m_error_list.AddString(s);
            }
        }
        CloseHandle(hfin);
        if (dlg->mb_isabort) break;
    }

    dlg->m_progress1.SetPos(0);
    dlg->m_progress2.SetPos(0);
    dlg->m_progress3.SetPos(0);
    dlg->m_static1.SetWindowText("Overall progress");

    VirtualFree(write_buffer,ClusterSize,MEM_DECOMMIT);
    VirtualFree(read_buffer,ClusterSize,MEM_DECOMMIT);

    DeleteFile((LPCSTR)test_file);

    if (!dlg->mb_isabort)
        AfxMessageBox("Scan completed !");
}


void CUSBStickTesterDlg::OnOK()
{
    mb_isabort = 0;
    // TODO: Add extra validation here
    if (m_selected_drive < 0)
    {
        AfxMessageBox("No disk selected !");
        return;
    }

    _beginthread(RunTests,0,this);


}

void CUSBStickTesterDlg::OnCancel()
{
    // TODO: Add extra cleanup here
    CString s;
    s.Format("%.4d",2);
    //AfxMessageBox(s);
    
    CDialog::OnCancel();
}

void CUSBStickTesterDlg::OnSelchangeListDisks()
{
    m_selected_drive = m_disks_list.GetCurSel();
    // TODO: Add your control notification handler code here
    
}

void CUSBStickTesterDlg::OnButtonAbort()
{
    // TODO: Add your control notification handler code here
    mb_isabort = 1;
}

'Windows > MFC' 카테고리의 다른 글

microtime  (0) 2013.10.02
Fade window  (0) 2013.10.02
USB header <UniFullUsr>  (0) 2013.10.02
마우스 오른쪽 버튼  (0) 2013.10.02
zip compression  (0) 2013.10.02