Thank you so MUCH!!! I feel my self so stupide , i worked with smart pointers a little time, and concentrate my attention on them, that's why i missed these...
In programm it works OK, no memory leaks, but i made a COM dll, and i got same problem - memory leaks!!!, and i delete all buffers!!!
Here is the code:
#include "stdafx.h"
#include "MTServerImpl.h"
#include ".\mtserverimpl.h"
char PathToNomenOnPocket[] = "\\Program Files\\Mobile Terminal\\Data\\Nomen.dat";
char PathToAgentsOnPocket[] = "\\Program Files\\Mobile Terminal\\Data\\Agents.dat";
char PathToNaklOnPocket[] = "\\My Documents\\*.xml";
char PathToAgentCodeOnPocket[] = "\\Program Files\\Mobile Terminal\\Data\\MTExpeditor.mtc";
char PathToMyDocyments[] = "\\My Documents\\";
char PathToHistory[] = "\\Program Files\\Mobile Terminal\\Data\\History\\";
char PathToHistoryNakl[] = "\\Program Files\\Mobile Terminal\\Data\\History\\*.xml";
STDMETHODIMP CMTServer::ClearHistory(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDialogProgress dlg;
dlg.Create(CDialogProgress::IDD);
dlg.CenterWindow();
dlg.ShowWindow(SW_SHOW);
RAPIINIT ri = {sizeof(RAPIINIT)};
HRESULT hRes = CeRapiInitEx(&ri);
DWORD dwRet = WaitForSingleObject(ri.heRapiInit,5000);
if((dwRet!=WAIT_OBJECT_0)||!SUCCEEDED(ri.hrRapiInit))
{
MessageBeep(MB_OK);
::MessageBox(NULL,"Невозможно инициализировать Мобильный Терминал. Убедитесь В Том Что Он Соединен Через Active Sync","Ошибка",MB_ICONERROR);
}
else
{
WCHAR wsdata[512];
WCHAR tempwsdata[512];
MultiByteToWideChar(CP_ACP,0,PathToHistoryNakl,strlen(PathToHistoryNakl)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
LPCE_FIND_DATA FindData = NULL;
DWORD Count;
CeFindAllFiles(wsdata,FAF_ATTRIB_NO_HIDDEN|FAF_NAME,&Count,&FindData);
MultiByteToWideChar(CP_ACP,0,PathToHistory,strlen(PathToHistory)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
StrCpyW(tempwsdata,wsdata);
dlg.m_Progress.SetRange32(0,Count);
dlg.m_Progress.SetStep(1);
for(int i = 0;i<Count;i++)
{
StrCatW(wsdata,FindData[i].cFileName);
if(!CeDeleteFile(wsdata))
MessageBox(NULL,"Невозможно удалить файл","Ошибка Удаления Файла",MB_ICONERROR);
StrCpyW(wsdata,tempwsdata);
dlg.m_Progress.StepIt();
}
if(CeRapiFreeBuffer(FindData)==E_FAIL)
{
MessageBox(NULL,"Невозможно очистить память","Ошибка Очистки Памяти",MB_ICONINFORMATION);
}
}
CeRapiUninit();
dlg.ShowWindow(SW_HIDE);
dlg.DestroyWindow();
return S_OK;
}
STDMETHODIMP CMTServer::ExportAgents(BSTR PathToAgents)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDialogProgress dlg;
dlg.Create(CDialogProgress::IDD);
dlg.CenterWindow();
dlg.ShowWindow(SW_SHOW);
dlg.m_Progress.SetRange32(0,30);
dlg.m_Progress.SetStep(10);
RAPIINIT ri = {sizeof(RAPIINIT)};
HRESULT hRes = CeRapiInitEx(&ri);
DWORD dwRet = WaitForSingleObject(ri.heRapiInit,5000);
if((dwRet!=WAIT_OBJECT_0)||!SUCCEEDED(ri.hrRapiInit))
{
MessageBeep(MB_OK);
::MessageBox(NULL,"Не возможно инициализировать Мобильный Терминал. Убедитесь В Том Что Он Соединен Через Active Sync","Ошибка",MB_ICONERROR);
}
else
{
variant_t vResult;
vResult = m_plDomDocument->load(PathToAgents);
if(((bool)vResult)==TRUE)
{
char Razdelitel = '?';
m_File = CreateFile(m_MainPath+"Agents.dat",GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
m_plDocRoot = m_plDomDocument->documentElement;
MSXML::IXMLDOMNodePtr pDocRoot = m_plDomDocument->documentElement;
CreateHeadForAgents(pDocRoot);
int Size = m_vHeadForAgents.size();
DWORD red;
WriteFile(m_File,&Size,sizeof(int),&red,NULL);
Size--;
for(int i = 0;i<m_vHeadForAgents.size();i++)
Size+=m_vHeadForAgents[i].GetLength();
WriteFile(m_File,&Size,sizeof(int),&red,NULL);
for(i = 0;i<m_vHeadForAgents.size();i++)
{
WriteFile(m_File,m_vHeadForAgents[i],m_vHeadForAgents[i].GetLength(),&red,NULL);
if(i<m_vHeadForAgents.size()-1)
WriteFile(m_File,&Razdelitel,1,&red,NULL);
}
dlg.m_Progress.StepIt();
m_vHead.clear();
Razdelitel = '.';
WriteFile(m_File,&Razdelitel,1,&red,NULL);
GetChildForAgents(m_plDocRoot);
dlg.m_Progress.StepIt();
CloseHandle(m_File);
m_File = CreateFile(m_MainPath+"Agents.dat",GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD HIGH;
int FileSize;
FileSize = GetFileSize(m_File,&HIGH);
char* buffer = new char[FileSize];
ReadFile(m_File,buffer,FileSize,&red,NULL);
WCHAR wsdata[256];
MultiByteToWideChar(CP_ACP,0,PathToAgentsOnPocket,strlen(PathToAgentsOnPocket)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
HANDLE hFile = CeCreateFile(wsdata,GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Невозможно создать файл","Ошибка Мобильного терминала",MB_ICONERROR);
return FALSE;
}
DWORD Bytes;
CeWriteFile(hFile,buffer,FileSize,&Bytes,NULL);
CeCloseHandle(hFile);
CloseHandle(m_File);
DeleteFile(m_MainPath+"Agents.dat");
delete [] buffer;
dlg.m_Progress.StepIt();
}
}
CeRapiUninit();
dlg.ShowWindow(SW_HIDE);
dlg.DestroyWindow();
return S_OK;
}
void CMTServer::GetChildForAgents(MSXML::IXMLDOMNodePtr pParent)
{
MSXML::IXMLDOMNamedNodeMapPtr pNamedNodeMapPtr =NULL;
MSXML::IXMLDOMNodePtr pXMLAttrNode = NULL;
long AttributesNumber = 0;
CString AttrName;
HRESULT hResult = pParent->get_attributes(&pNamedNodeMapPtr);
if(FAILED(hResult))
{
::MessageBox(NULL,"Ошибка получения атрибута","Ошибка XML",MB_ICONERROR);
return;
}
pNamedNodeMapPtr->get_length(&AttributesNumber);
char Razdelitel = '#';
for(int i = 0;i<AttributesNumber;i++)
{
pNamedNodeMapPtr->get_item(i,&pXMLAttrNode);
VARIANT var;
pXMLAttrNode->get_nodeValue(&var);
CString str =(CString) var.bstrVal;
if(str=="")
str=" ";
str.Replace('#','-');
DWORD red;
WriteFile(m_File,str,str.GetLength(),&red,NULL);
WriteFile(m_File,&Razdelitel,1,&red,NULL);
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
GetChildForAgents(pChild);
}
}
void CMTServer::GetChild(MSXML::IXMLDOMNodePtr pParent,CString FullPath = "")
{
MSXML::IXMLDOMNamedNodeMapPtr pNamedNodeMapPtr =NULL;
MSXML::IXMLDOMNodePtr pXMLAttrNode = NULL;
long AttributesNumber = 0;
CString AttrName;
HRESULT hResult = pParent->get_attributes(&pNamedNodeMapPtr);
if(FAILED(hResult))
{
::MessageBox(NULL,"Ошибка получения атрибута","Ошибка XML",MB_ICONERROR);
return;
}
pNamedNodeMapPtr->get_length(&AttributesNumber);
char Razdelitel = '#';
for(int i = 0;i<AttributesNumber;i++)
{
pNamedNodeMapPtr->get_item(i,&pXMLAttrNode);
CString str((LPCTSTR)pXMLAttrNode->nodeName);
if(str=="Группа")
{
VARIANT var;
pXMLAttrNode->get_nodeValue(&var);
FullPath += (CString)var.bstrVal+"\\";
}
else
{
DWORD red;
if(!i)
{
WriteFile(m_File,FullPath,FullPath.GetLength(),&red,NULL);
WriteFile(m_File,&Razdelitel,1,&red,NULL);
}
VARIANT var;
pXMLAttrNode->get_nodeValue(&var);
CString str = (CString)var.bstrVal;
if(str=="")
str = " ";
str.Replace('#','-');
WriteFile(m_File,str,str.GetLength(),&red,NULL);
WriteFile(m_File,&Razdelitel,1,&red,NULL);
}
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
GetChild(pChild,FullPath);
}
}
int CMTServer::CreateHeadForAgents(MSXML::IXMLDOMNodePtr pParent)
{
MSXML::IXMLDOMNamedNodeMapPtr pNamedNodeMapPtr =NULL;
MSXML::IXMLDOMNodePtr pXMLAttrNode = NULL;
long AttributesNumber = 0;
HRESULT hResult = pParent->get_attributes(&pNamedNodeMapPtr);
if(FAILED(hResult))
{
::MessageBox(NULL,"Ошибка получения атрибута","Ошибка XML",MB_ICONERROR);
return 1;
}
pNamedNodeMapPtr->get_length(&AttributesNumber);
for(int i = 0;i<AttributesNumber;i++)
{
pNamedNodeMapPtr->get_item(i,&pXMLAttrNode);
for(int j = 0;j<AttributesNumber;j++)
{
pNamedNodeMapPtr->get_item(j,&pXMLAttrNode);
CString str((LPCTSTR)pXMLAttrNode->nodeName);
m_vHeadForAgents.push_back(str);
}
return 1;
break;
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
if(CreateHeadForAgents(pChild)==1)
return 1;
}
return 0;
}
int CMTServer::CreateHead(MSXML::IXMLDOMNodePtr pParent)
{
MSXML::IXMLDOMNamedNodeMapPtr pNamedNodeMapPtr =NULL;
MSXML::IXMLDOMNodePtr pXMLAttrNode = NULL;
long AttributesNumber = 0;
HRESULT hResult = pParent->get_attributes(&pNamedNodeMapPtr);
if(FAILED(hResult))
{
::MessageBox(NULL,"Ошибка получения атрибута","Ошибка XML",MB_ICONERROR);
return 1;
}
pNamedNodeMapPtr->get_length(&AttributesNumber);
for(int i = 0;i<AttributesNumber;i++)
{
pNamedNodeMapPtr->get_item(i,&pXMLAttrNode);
CString str((LPCTSTR)pXMLAttrNode->nodeName);
if(str!="Группа")
{
for(int j = 0;j<AttributesNumber;j++)
{
pNamedNodeMapPtr->get_item(j,&pXMLAttrNode);
CString str((LPCTSTR)pXMLAttrNode->nodeName);
m_vHead.push_back(str);
}
return 1;
}
break;
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
if(CreateHead(pChild)==1)
return 1;
}
return 0;
}
STDMETHODIMP CMTServer::ExportNomen(BSTR PathToNomen)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CDialogProgress dlg;
dlg.Create(CDialogProgress::IDD);
dlg.CenterWindow();
dlg.ShowWindow(SW_SHOW);
dlg.m_Progress.SetRange32(0,30);
dlg.m_Progress.SetStep(10);
RAPIINIT ri = {sizeof(RAPIINIT)};
HRESULT hRes = CeRapiInitEx(&ri);
DWORD dwRet = WaitForSingleObject(ri.heRapiInit,5000);
if((dwRet!=WAIT_OBJECT_0)||!SUCCEEDED(ri.hrRapiInit))
{
MessageBeep(MB_OK);
::MessageBox(NULL,"Не возможно инициализировать Мобильный Терминал. Убедитесь В Том Что Он Соединен Через Active Sync","Ошибка",MB_ICONERROR);
}
else
{
variant_t vResult;
vResult = m_plDomDocument->load(PathToNomen);
if(((bool)vResult)==TRUE)
{
char Razdelitel = '?';
m_File = CreateFile(m_MainPath+"Nomen.dat",GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
m_plDocRoot = m_plDomDocument->documentElement;
MSXML::IXMLDOMNodePtr pDocRoot = m_plDomDocument->documentElement;
m_vHead.push_back(CString("Путь"));
CreateHead(pDocRoot);
int Size = m_vHead.size();
DWORD red;
WriteFile(m_File,&Size,sizeof(int),&red,NULL);
Size--;
for(int i = 0;i<m_vHead.size();i++)
Size+=m_vHead[i].GetLength();
WriteFile(m_File,&Size,sizeof(int),&red,NULL);
for(i = 0;i<m_vHead.size();i++)
{
WriteFile(m_File,m_vHead[i],m_vHead[i].GetLength(),&red,NULL);
if(i<m_vHead.size()-1)
WriteFile(m_File,&Razdelitel,1,&red,NULL);
}
dlg.m_Progress.StepIt();
m_vHead.clear();
Razdelitel = '.';
WriteFile(m_File,&Razdelitel,1,&red,NULL);
GetChild(m_plDocRoot);
dlg.m_Progress.StepIt();
CloseHandle(m_File);
m_File = CreateFile(m_MainPath+"Nomen.dat",GENERIC_READ,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD HIGH;
int FileSize;
FileSize = GetFileSize(m_File,&HIGH);
char* buffer = new char[FileSize];
ReadFile(m_File,buffer,FileSize,&red,NULL);
WCHAR wsdata[256];
MultiByteToWideChar(CP_ACP,0,PathToNomenOnPocket,strlen(PathToNomenOnPocket)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
HANDLE hFile = CeCreateFile(wsdata,GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Невозможно создать файл","Ошибка Мобильного терминала",MB_ICONERROR);
}
DWORD Bytes;
CeWriteFile(hFile,buffer,FileSize,&Bytes,NULL);
CeCloseHandle(hFile);
CloseHandle(m_File);
DeleteFile(m_MainPath+"Nomen.dat");
delete [] buffer;
dlg.m_Progress.StepIt();
}
else
{
::MessageBox(NULL,"Невозможно загрузить файл XML","Ошибка XML",MB_ICONERROR);
}
}
CeRapiUninit();
dlg.ShowWindow(SW_HIDE);
dlg.DestroyWindow();
return S_OK;
}
STDMETHODIMP CMTServer::ImportNaklad(BSTR PathToNaklad)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
RAPIINIT ri = {sizeof(RAPIINIT)};
HRESULT hRes = CeRapiInitEx(&ri);
DWORD dwRet = WaitForSingleObject(ri.heRapiInit,5000);
if((dwRet!=WAIT_OBJECT_0)||!SUCCEEDED(ri.hrRapiInit))
{
MessageBeep(MB_OK);
::MessageBox(NULL,"Не возможно инициализировать Мобильный Терминал. Убедитесь В Том Что Он Соединен Через Active Sync","Ошибка",MB_ICONERROR);
}
else
{
HANDLE file;
char* XMLHEAD = "<?xml version=\"1.0\" encoding=\"windows-1251\"?><НАКЛАДНАЯ>";
char* XMLEND = "</НАКЛАДНАЯ>";
WCHAR wsdata[256];
WCHAR tempwsdata[256];
MultiByteToWideChar(CP_ACP,0,PathToNaklOnPocket,strlen(PathToNaklOnPocket)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
LPCE_FIND_DATA FindData = NULL;
DWORD Count;
CeFindAllFiles(wsdata,FAF_ATTRIB_NO_HIDDEN|FAF_NAME,&Count,&FindData);
MultiByteToWideChar(CP_ACP,0,PathToMyDocyments,strlen(PathToMyDocyments)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
StrCpyW(tempwsdata,wsdata);
if(Count==0)
{
MessageBox(NULL,"Мобильный терминал пуст","Информация",MB_ICONINFORMATION);
return S_OK;
}
CDialogProgress dlg;
dlg.Create(CDialogProgress::IDD);
dlg.CenterWindow();
dlg.ShowWindow(SW_SHOW);
dlg.m_Progress.SetRange(0,Count*10);
dlg.m_Progress.SetStep(10);
file = CreateFile(m_PathToNaklad,GENERIC_READ|GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD red;
WriteFile(file,XMLHEAD,strlen(XMLHEAD),&red,NULL);
for(int i = 0;i<Count;i++)
{
StrCatW(wsdata,FindData[i].cFileName);
HANDLE hFileTemp = CeCreateFile(wsdata,GENERIC_READ,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_READONLY,NULL);
if(hFileTemp==INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"Невозможно открыть файл","Ошибка Мобильного Терминала",MB_ICONINFORMATION);
}
DWORD HIGH;
int Size = CeGetFileSize(hFileTemp,&HIGH);
if(Size>45)
{
char* ldata = new char[Size-45];
LARGE_INTEGER li;
li.QuadPart = 45;
li.LowPart = CeSetFilePointer(hFileTemp,li.LowPart,&li.HighPart,FILE_BEGIN);
CeReadFile(hFileTemp,ldata,Size-45,&HIGH,NULL);
WriteFile(file,ldata,Size-45,&red,NULL);
CeCloseHandle(hFileTemp);
delete [] ldata;
}
WCHAR HistoryPath[512];
MultiByteToWideChar(CP_ACP,0,PathToHistory,strlen(PathToHistory)+1,HistoryPath,sizeof(HistoryPath)/sizeof(HistoryPath[0]));
CeCopyFile(wsdata,StrCatW(HistoryPath,FindData[i].cFileName),FALSE);
if(!CeDeleteFile(wsdata))
MessageBox(NULL,"Невозможно удалить файл с мобильного терминала","Ошибка мобильного терминала",MB_ICONERROR);
StrCpyW(wsdata,tempwsdata);
dlg.m_Progress.StepIt();
}
WriteFile(file,XMLEND,strlen(XMLEND),&red,NULL);
CloseHandle(file);
if(CeRapiFreeBuffer(FindData)==E_FAIL)
{
MessageBox(NULL,"Невозможно очистить память","Ошибка очистки памяти",MB_ICONINFORMATION);
};
dlg.ShowWindow(SW_HIDE);
dlg.DestroyWindow();
}
CeRapiUninit();
return S_OK;
}
STDMETHODIMP CMTServer::SetTempPath(BSTR TempPath)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
m_MainPath = (CString) TempPath;
return S_OK;
}
STDMETHODIMP CMTServer::GetAgentCode(BSTR* AgentCode)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
RAPIINIT ri = {sizeof(RAPIINIT)};
HRESULT hRes = CeRapiInitEx(&ri);
DWORD dwRet = WaitForSingleObject(ri.heRapiInit,5000);
if((dwRet!=WAIT_OBJECT_0)||!SUCCEEDED(ri.hrRapiInit))
{
MessageBeep(MB_OK);
::MessageBox(NULL,"Не возможно инициализировать Мобильный Терминал. Убедитесь В Том Что Он Соединен Через Active Sync","Ошибка",MB_ICONERROR);
}
else
{
WCHAR wsdata[256];
DWORD HIGH;
MultiByteToWideChar(CP_ACP,0,PathToAgentCodeOnPocket,strlen(PathToAgentCodeOnPocket)+1,wsdata,sizeof(wsdata)/sizeof(wsdata[0]));
HANDLE hFileTemp = CeCreateFile(wsdata,GENERIC_READ,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_READONLY,NULL);
int Size = CeGetFileSize(hFileTemp,&HIGH);
char* data = new char[Size+1];
data[Size] = 0;
CeReadFile(hFileTemp,data,Size,&HIGH,NULL);
CString str = data;
delete []data;
CeCloseHandle(hFileTemp);
*AgentCode = str.AllocSysString();
}
CeRapiUninit();
return S_OK;
}
|