使用了MapViewOfFile将文件映射到内存
使用完 必须 使用UnmapViewOfFile 取消映射,否则报内存泄露。
CConfigDlg::CConfigDlg(CWnd* pParent /*=NULL*/) : CDialog(CConfigDlg::IDD, pParent), m_pUserInfo(0) { m_dllPath = GetModulePath(); m_hFile = CreateFile(m_dllPath+"\\echoconfig.dat",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_HIDDEN,0); if (m_hFile == INVALID_HANDLE_VALUE) { m_hFile = CreateFile(m_dllPath+"\\echoconfig.dat",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_WRITE|FILE_SHARE_READ,0,CREATE_NEW,FILE_ATTRIBUTE_HIDDEN,0); m_hFileMapping = CreateFileMapping(m_hFile,0,PAGE_READWRITE,0,4096,"mmiku_ae_echo_configuration"); ASSERT(m_hFileMapping != 0); m_pUserInfo = (tagUserInfo*) MapViewOfFile(m_hFileMapping,FILE_MAP_ALL_ACCESS,0,0,0); ASSERT(m_pUserInfo != 0); m_pUserInfo->i_nTime = 5; m_pUserInfo->i_nFeedback = 65; } else { m_hFileMapping = CreateFileMapping(m_hFile,0,PAGE_READWRITE,0,4096,"mmiku_ae_echo_configuration"); ASSERT(m_hFileMapping != 0); m_pUserInfo = (tagUserInfo*) MapViewOfFile(m_hFileMapping,FILE_MAP_ALL_ACCESS,0,0,0); ASSERT(m_pUserInfo != 0); } m_nTime = m_pUserInfo->i_nTime * 4410; m_fFeedback = m_pUserInfo->i_nFeedback / 100.0f; } CConfigDlg::~CConfigDlg() { if(m_hFile != INVALID_HANDLE_VALUE) CloseHandle(m_hFile); if(m_hFileMapping != NULL) { UnmapViewOfFile( m_pUserInfo); //这一句 不写 的话 报 内存xielou CloseHandle(m_hFileMapping); } }