⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quicksysdlg.cpp

📁 The program is a wizard to create a NT kernel driver framework in C. It will create a Visual C++ wo
💻 CPP
字号:
// QuickSYSDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QuickSYS.h"
#include "QuickSYSDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQuickSYSDlg dialog

void SaveLastProjectLocation(const CString &str)
{
	HKEY hKey;
	if ( ERROR_SUCCESS == RegCreateKeyEx(HKEY_CURRENT_USER,
		"SOFTWARE\\QuickSYS", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL) )
	{
		RegSetValueEx(hKey, "LastProjectLocation", 0, REG_SZ, (CONST LPBYTE)(LPCSTR)str, str.GetLength()+1);
	}
}

CString LoadLastProjectLocation()
{
	CString dir;
	HKEY hKey;
	if ( ERROR_SUCCESS == RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\QuickSYS", &hKey) )
	{
		DWORD size = MAX_PATH;
		DWORD type = REG_SZ;
		RegQueryValueEx(hKey, "LastProjectLocation", NULL, &type, (LPBYTE)dir.GetBuffer(MAX_PATH), (LPDWORD)&size);
		dir.ReleaseBuffer();
	}
	return dir;
}

CQuickSYSDlg::CQuickSYSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQuickSYSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQuickSYSDlg)
	m_strProjectLocation = LoadLastProjectLocation();
	m_strProjectName = _T("");
	m_bSoftICE = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQuickSYSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQuickSYSDlg)
	DDX_Text(pDX, IDC_PROJECT_LOCATION, m_strProjectLocation);
	DDX_Text(pDX, IDC_PROJECT_NAME, m_strProjectName);
	DDX_Check(pDX, IDC_SOFTICE, m_bSoftICE);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQuickSYSDlg, CDialog)
	//{{AFX_MSG_MAP(CQuickSYSDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BROWSE_LOCATION, OnBrowseLocation)
	ON_BN_CLICKED(IDC_CREATE, OnCreate)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQuickSYSDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQuickSYSDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CQuickSYSDlg::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 CQuickSYSDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

#include <direct.h>
#include <io.h>
int CreateAllDirectories(char *pszDir)
{
    char*   pszLastSlash;
    char    cTmp;

    if ( _access( pszDir, 0 ) != -1 ) 
    {
        // it already exists
        return 0;
    }

    pszLastSlash = strrchr(pszDir, '\\');
    if ( pszLastSlash )
    {
        cTmp = *pszLastSlash;
        *pszLastSlash = '\0';

        // try again with one less dir
        CreateAllDirectories(pszDir); 

        *pszLastSlash = cTmp;
    }


    if ( _mkdir( pszDir ) == -1 )
    {
        return -1;
    }

    return 0;
}

BOOL BrowseFolder(HWND hWnd, char *dir)
{
	BROWSEINFO bi;
	LPITEMIDLIST il;
	
	bi.hwndOwner = hWnd;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = dir;
	bi.lpszTitle = "Choose directory:";
	bi.ulFlags = BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = 0;
	bi.iImage = 0;
	
	il = SHBrowseForFolder(&bi);
	
	if (il) //按下OK键选定了一个目录
	{
		SHGetPathFromIDList(il, dir);
		
		LPMALLOC lpMalloc;
		SHGetMalloc(&lpMalloc);
		lpMalloc->Free(il); //释放内存
		lpMalloc->Release();
		return TRUE;
	}
	return FALSE;
}

void CQuickSYSDlg::OnBrowseLocation() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	BOOL bNewFolder = BrowseFolder(m_hWnd, m_strProjectLocation.GetBuffer(MAX_PATH));
	m_strProjectLocation.ReleaseBuffer();
	if ( bNewFolder )
	{
		SaveLastProjectLocation(m_strProjectLocation);
		UpdateData(FALSE);
	}
}

void CQuickSYSDlg::OnCreate() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if ( m_strProjectName.IsEmpty() )
	{
		MessageBox("Please input project name.", "QuickSYS", MB_ICONSTOP);
		return;
	}
	if ( m_strProjectLocation.IsEmpty() )
	{
		MessageBox("Please input project location.", "QuickSYS", MB_ICONSTOP);
		return;
	}
	int ret = CreateProject();
	switch (ret)
	{
	case -1:
		MessageBox("Failed to create directory.", "QuickSYS", MB_ICONSTOP);
		break;
	case -2:
		MessageBox("Failed to create file.", "QuickSYS", MB_ICONSTOP);
		break;
	case -3:
		MessageBox("Create project canceled.", "QuickSYS", MB_ICONSTOP);
		break;
	default:
		MessageBox("Create project successfully.", "QuickSYS", MB_ICONINFORMATION);
		break;
	}
}

CString LoadText(LPCTSTR lpName)
{
	CString strText;
	HRSRC hRsrc;
	
	hRsrc = FindResource(AfxGetInstanceHandle(), lpName, RT_HTML);
	HGLOBAL hMem = LoadResource(AfxGetInstanceHandle(), hRsrc);
	DWORD dwSize = SizeofResource(AfxGetInstanceHandle(), hRsrc);
	char *src = (char*)LockResource(hMem);
	char *dst = strText.GetBuffer(dwSize+1);
	memcpy(dst, src, dwSize);
	dst[dwSize] = 0;
	strText.ReleaseBuffer();

	return strText;
}

void ReplaceInString(CString &str, const CString &src, const CString &dst)
{
	CString tmp = str;
	while (1)
	{
		int pos = tmp.Find(src);
		if ( pos == -1 ) break;
		str = tmp.Left(pos);
		str += dst;
		str += tmp.Right(tmp.GetLength() - pos - src.GetLength());
		tmp = str;
	}
}

CString GetSoftICEInstallDir()
{
	CString dir;
	HKEY hKey;
	if ( ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\NuMega\\SoftICE", &hKey) )
	{
		DWORD size = MAX_PATH;
		DWORD type = REG_SZ;
		RegQueryValueEx(hKey, "InstallDir", NULL, &type, (LPBYTE)dir.GetBuffer(MAX_PATH), (LPDWORD)&size);
		dir.ReleaseBuffer();
	}
	return dir;
}

int CQuickSYSDlg::CreateProject()
{
	CString strPrjBaseDir = m_strProjectLocation;
	if ( strPrjBaseDir.Right(1) != "\\" ) strPrjBaseDir += "\\";
	strPrjBaseDir += m_strProjectName;
	
	if ( CreateAllDirectories(strPrjBaseDir.GetBuffer(MAX_PATH)) != 0 ) return -1;
	strPrjBaseDir.ReleaseBuffer();
	strPrjBaseDir += "\\";

	// upper case project name
	CString strProjectNameU = m_strProjectName;
	strProjectNameU.MakeUpper();

	FILE *fp;
	// frist create .dsw
	CString strDSWFileName = strPrjBaseDir + m_strProjectName;
	strDSWFileName += ".dsw";
	
	if ( _access( strDSWFileName, 0 ) != -1 )
	{ // project already exist
		DWORD ret = MessageBox("Project already exist, overwrite?",
			"QuickSYS", MB_YESNO|MB_ICONQUESTION);
		if ( ret == IDNO ) return -3;
	}

	fp = fopen(strDSWFileName, "w");
	if ( !fp ) return -2;
	CString strDSWFileContent;
	strDSWFileContent.LoadString(IDS_DSW);
	fprintf(fp, strDSWFileContent, m_strProjectName, m_strProjectName);
	fclose(fp);

	// get softice installed directory
	CString strNMsym = GetSoftICEInstallDir();
	if ( !strNMsym.IsEmpty() )
	{
		if ( strNMsym.Right(1) != "\\" ) strNMsym += "\\";
		strNMsym += "nmsym.exe";
	}

	// 2nd, create .dsp
	CString strDSPFileName = strPrjBaseDir + m_strProjectName;
	strDSPFileName += ".dsp";
	
	fp = fopen(strDSPFileName, "wb");
	if ( !fp ) return -2;
	CString strDSPFileContent;
	strDSPFileContent = LoadText(MAKEINTRESOURCE(IDR_DSP));
	ReplaceInString(strDSPFileContent, "$(PROJECT_NAME)", m_strProjectName);
	if ( m_bSoftICE && !strNMsym.IsEmpty() )
	{
		CString strSoftICE;

		strSoftICE.Format("# Begin Special Build Tool\r\n"
			"SOURCE=\"$(InputPath)\"\r\n"
			"PostBuild_Desc=Generating SoftICE Symbol file %s.nms\r\n"
			"PostBuild_Cmds=\"%s\" /translate:source,package,always Release\\%s.sys\r\n"
			"# End Special Build Tool\r\n",
			m_strProjectName, strNMsym, m_strProjectName);
		ReplaceInString(strDSPFileContent, "$(SOFTICE_CMD_RELEASE)", strSoftICE);

		strSoftICE.Format("# Begin Special Build Tool\r\n"
			"SOURCE=\"$(InputPath)\"\r\n"
			"PostBuild_Desc=Generating SoftICE Symbol file %s.nms\r\n"
			"PostBuild_Cmds=\"%s\" /translate:source,package,always Debug\\%s.sys\r\n"
			"# End Special Build Tool\r\n",
			m_strProjectName, strNMsym, m_strProjectName);
		ReplaceInString(strDSPFileContent, "$(SOFTICE_CMD_DEBUG)", strSoftICE);
	}
	else
	{
		ReplaceInString(strDSPFileContent, "$(SOFTICE_CMD_RELEASE)", "");
		ReplaceInString(strDSPFileContent, "$(SOFTICE_CMD_DEBUG)", "");
	}
	fprintf(fp, "%s", strDSPFileContent);
	fclose(fp);

	// 3rd, create .c file
	CString strCFileName = strPrjBaseDir + m_strProjectName;
	strCFileName += ".c";
	
	fp = fopen(strCFileName, "wb");
	if ( !fp ) return -2;
	CString strCFileContent;
	strCFileContent = LoadText(MAKEINTRESOURCE(IDR_CFILE));
	ReplaceInString(strCFileContent, "$(PROJECT_NAME)", m_strProjectName);
	ReplaceInString(strCFileContent, "$(PROJECT_NAME_U)", strProjectNameU);
	fprintf(fp, "%s", strCFileContent);
	fclose(fp);

	// 4th, create .h file
	CString strHFileName = strPrjBaseDir + m_strProjectName;
	strHFileName += ".h";
	
	fp = fopen(strHFileName, "wb");
	if ( !fp ) return -2;
	CString strHFileContent;
	strHFileContent = LoadText(MAKEINTRESOURCE(IDR_HFILE));
	ReplaceInString(strHFileContent, "$(PROJECT_NAME)", m_strProjectName);
	ReplaceInString(strHFileContent, "$(PROJECT_NAME_U)", strProjectNameU);
	fprintf(fp, "%s", strHFileContent);
	fclose(fp);

	// 5th, create readme.txt
	CString strReadmeFileName = strPrjBaseDir + "readme.txt";
	
	fp = fopen(strReadmeFileName, "wb");
	if ( !fp ) return -2;
	CString strReadmeFileContent;
	strReadmeFileContent = LoadText(MAKEINTRESOURCE(IDR_README));
	ReplaceInString(strReadmeFileContent, "$(PROJECT_NAME)", m_strProjectName);
	ReplaceInString(strReadmeFileContent, "$(PROJECT_NAME_U)", strProjectNameU);
	fprintf(fp, "%s", strReadmeFileContent);
	fclose(fp);
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -