|
I got a programm, that works with xml files. And it uses MSXML.dll. But i found i greate memory leak, and i can't understand - what am i doing wrong???
Here is the code:
#pragma once
#include <vector>
#import <msxml.dll> named_guids
using namespace MSXML;
using namespace std;
class CNakladImporter
{
vector <CString> m_vHead;
vector <CString> m_vHeadForAgents;
CFile m_File;
CString m_PathToNomen;
CString m_PathToAgents;
CString m_PathToNaklad;
MSXML::IXMLDOMDocumentPtr m_plDomDocument;
MSXML::IXMLDOMElementPtr m_plDocRoot;
void GetChild(MSXML::IXMLDOMNodePtr pParent,CString FullPath);
void GetChildForAgents(MSXML::IXMLDOMNodePtr pParent);
int CreateHead(MSXML::IXMLDOMNodePtr pParent);
int CreateHeadForAgents(MSXML::IXMLDOMNodePtr pParent);
public:
CNakladImporter(void);
void SetPathToNomen(CString Path);
void SetPathToAgents(CString Path);
void SetPathToNaklad(CString Path);
BOOL ExportAgents(CProgressCtrl* ProgressCtrl);
BOOL ExportNomen(CProgressCtrl* ProgressCtrl);
BOOL ImportNaklad(CProgressCtrl* ProgressCtrl);
void ClearHistory();
~CNakladImporter(void);
bool InitXML(void);
};
and implementation
#include "StdAfx.h"
#include "nakladimporter.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 PathToMyDocyments[] = "\\My Documents\\";
char PathToHistory[] = "\\Program Files\\Mobile Terminal\\Data\\History\\";
char PathToHistoryNakl[] = "\\Program Files\\Mobile Terminal\\Data\\History\\*.xml";
CNakladImporter::CNakladImporter(void)
{
}
CNakladImporter::~CNakladImporter(void)
{
}
void CNakladImporter::SetPathToNomen(CString Path)
{
m_PathToNomen = Path;
}
void CNakladImporter::SetPathToAgents(CString Path)
{
m_PathToAgents = Path;
}
void CNakladImporter::SetPathToNaklad(CString Path)
{
m_PathToNaklad = Path;
}
BOOL CNakladImporter::ExportAgents(CProgressCtrl* ProgressCtrl)
{
_bstr_t bstrFileName;
bstrFileName = m_PathToAgents.AllocSysString();
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName);
if(((bool)vResult)==TRUE)
{
ProgressCtrl->SetRange(0,30);
ProgressCtrl->SetStep(10);
char Razdelitel = '?';
m_File.Open("Agents.dat",CFile::modeCreate|CFile::modeWrite);
m_plDocRoot = m_plDomDocument->documentElement;
MSXML::IXMLDOMNodePtr pDocRoot = m_plDomDocument->documentElement;
CreateHeadForAgents(pDocRoot);
int Size = m_vHeadForAgents.size();
m_File.Write(&Size,sizeof(int));
Size--;
for(int i = 0;i<m_vHeadForAgents.size();i++)
Size+=m_vHeadForAgents[i].GetLength();
m_File.Write(&Size,sizeof(int));
for(i = 0;i<m_vHeadForAgents.size();i++)
{
m_File.Write(m_vHeadForAgents[i],m_vHeadForAgents[i].GetLength());
if(i<m_vHeadForAgents.size()-1)
m_File.Write(&Razdelitel,1);
}
ProgressCtrl->StepIt();
m_vHead.clear();
Razdelitel = '.';
m_File.Write(&Razdelitel,1);
GetChildForAgents(m_plDocRoot);
ProgressCtrl->StepIt();
m_File.Close();
m_File.Open("Agents.dat",CFile::modeRead);
char* buffer = new char[m_File.GetLength()];
m_File.Read(buffer,m_File.GetLength());
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,"Can't Create File On Mobile Terminal","MT Error",MB_ICONERROR);
return FALSE;
}
DWORD Bytes;
CeWriteFile(hFile,buffer,m_File.GetLength(),&Bytes,NULL);
CeCloseHandle(hFile);
m_File.Close();
DeleteFile("Agents.dat");
ProgressCtrl->StepIt();
return TRUE;
}
return FALSE;
}
void CNakladImporter::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,"Attribute Getting Error","XML Error",MB_ICONERROR);
return;
}
pNamedNodeMapPtr->get_length(&AttributesNumber);
char Razdelitel = '#';
for(int i = 0;i<AttributesNumber;i++)
{
pNamedNodeMapPtr->get_item(i,&pXMLAttrNode);
COleVariant OleVariant;
pXMLAttrNode->get_nodeValue(OleVariant);
CString str = OleVariant;
if(str=="")
str=" ";
str.Replace('#','-');
m_File.Write(str,str.GetLength());
m_File.Write(&Razdelitel,1);
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
GetChildForAgents(pChild);
}
}
void CNakladImporter::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,"Attribute Getting Error","XML Error",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=="Группа")
{
COleVariant OleVariant;
pXMLAttrNode->get_nodeValue(OleVariant);
CString OleString = OleVariant;
FullPath += OleString+"\\";
}
else
{
if(!i)
{
m_File.Write(FullPath,FullPath.GetLength());
m_File.Write(&Razdelitel,1);
}
COleVariant OleVariant;
pXMLAttrNode->get_nodeValue(OleVariant);
CString str = OleVariant;
if(str=="")
str = " ";
str.Replace('#','-');
m_File.Write(str,str.GetLength());
m_File.Write(&Razdelitel,1);
}
}
for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;NULL!=pChild;pChild=pChild->nextSibling)
{
GetChild(pChild,FullPath);
}
}
int CNakladImporter::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,"Error","Error",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 CNakladImporter::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,"Error","Error",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;
}
BOOL CNakladImporter::ExportNomen(CProgressCtrl* ProgressCtrl)
{
_bstr_t bstrFileName;
bstrFileName = m_PathToNomen.AllocSysString();
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName);
if(((bool)vResult)==TRUE)
{
ProgressCtrl->SetRange(0,30);
ProgressCtrl->SetStep(10);
char Razdelitel = '?';
m_File.Open("Nomen.dat",CFile::modeCreate|CFile::modeWrite);
m_plDocRoot = m_plDomDocument->documentElement;
MSXML::IXMLDOMNodePtr pDocRoot = m_plDomDocument->documentElement;
m_vHead.push_back(CString("Путь"));
CreateHead(pDocRoot);
int Size = m_vHead.size();
m_File.Write(&Size,sizeof(int));
Size--;
for(int i = 0;i<m_vHead.size();i++)
Size+=m_vHead[i].GetLength();
m_File.Write(&Size,sizeof(int));
for(i = 0;i<m_vHead.size();i++)
{
m_File.Write(m_vHead[i],m_vHead[i].GetLength());
if(i<m_vHead.size()-1)
m_File.Write(&Razdelitel,1);
}
ProgressCtrl->StepIt();
m_vHead.clear();
Razdelitel = '.';
m_File.Write(&Razdelitel,1);
GetChild(m_plDocRoot);
ProgressCtrl->StepIt();
m_File.Close();
m_File.Open("Nomen.dat",CFile::modeRead);
char* buffer = new char[m_File.GetLength()];
m_File.Read(buffer,m_File.GetLength());
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,"Can't Create File On Mobile Terminal","MT Error",MB_ICONERROR);
return FALSE;
}
DWORD Bytes;
CeWriteFile(hFile,buffer,m_File.GetLength(),&Bytes,NULL);
CeCloseHandle(hFile);
m_File.Close();
DeleteFile("Nomen.dat");
ProgressCtrl->StepIt();
return TRUE;
}
else
{
::MessageBox(NULL,"Can't Load XML File","XML Error",MB_ICONERROR);
return FALSE;
}
}
void CNakladImporter::ClearHistory()
{
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);
for(int i = 0;i<Count;i++)
{
StrCatW(wsdata,FindData[i].cFileName);
if(!CeDeleteFile(wsdata))
MessageBox(NULL,"Can't Delete File From Mobile Terminal","File Error",MB_ICONERROR);
StrCpyW(wsdata,tempwsdata);
}
if(CeRapiFreeBuffer(FindData)==E_FAIL)
{
MessageBox(NULL,"Memory Free Error","Memory Error",MB_ICONINFORMATION);
};
}
BOOL CNakladImporter::ImportNaklad(CProgressCtrl* ProgressCtrl)
{
CFile 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);
ProgressCtrl->SetRange(0,Count*10);
ProgressCtrl->SetStep(10);
if(Count==0)
{
MessageBox(NULL,"Mobile Terminal Is Empty","Information",MB_ICONINFORMATION);
return TRUE;
}
CFileException fe;
file.Open(m_PathToNaklad,CFile::modeCreate|CFile::modeWrite,&fe);
file.Write(XMLHEAD,strlen(XMLHEAD));
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,"Can't Open File","Error",MB_ICONINFORMATION);
}
DWORD HIGH;
int Size = CeGetFileSize(hFileTemp,&HIGH);
if(Size>45)
{
char* data = new char[Size-45];
LARGE_INTEGER li;
li.QuadPart = 45;
li.LowPart = CeSetFilePointer(hFileTemp,li.LowPart,&li.HighPart,FILE_BEGIN);
CeReadFile(hFileTemp,data,Size-45,&HIGH,NULL);
file.Write(data,Size-45);
CeCloseHandle(hFileTemp);
delete [] data;
}
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,"Can't Delete File From Mobile Terminal","File Error",MB_ICONERROR);
StrCpyW(wsdata,tempwsdata);
ProgressCtrl->StepIt();
}
file.Write(XMLEND,strlen(XMLEND));
file.Close();
if(CeRapiFreeBuffer(FindData)==E_FAIL)
{
MessageBox(NULL,"Memory Free Error","Memory Error",MB_ICONINFORMATION);
};
return TRUE;
}
bool CNakladImporter::InitXML(void)
{
::CoInitialize(NULL);
HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument);
if(FAILED(hr))
{
_com_error er(hr);
AfxMessageBox(er.ErrorMessage());
return false;
}
return true;
}
Please help, anybody...
Maybe problem in smart pointers???
If i don't call CreateInstance of smart pointer object, do i have to call Release??? Or can i send it in function parameter Somenting(SMartPtr pPtr)???
|
|
|
|
|
did you forget to delete char* buffer's allocated at two places.
suhredayan There is no spoon.
|
|
|
|
|
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;
}
|
|
|
|
|
btw, Did you miss to call VariantClear() ?
suhredayan There is no spoon.
|
|
|
|
|
Thanks Again, I spent some time by comparing two sources, and i found in one source COleVariant, and in other VARIANT, so i found all the leaks ( exactly - you helped me so much - i must say - you found all my leaks ), but i used SysFreeString (var.bstrVal) - is it right?
|
|
|
|
|
And Here Is The Way I Call It:
IMTServerPtr pServer(CLSID_MTServer);
CString str;
BSTR bstring;
for(int i = 0 ;i<10;i++)
{
bstring = pServer->GetAgentCode();
str = (CString)bstring;
SysFreeString(bstring);
}
CFileDialog dlg(TRUE,0,"*.xml",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"XML Files");
if(dlg.DoModal()==IDOK)
{
for(int i = 0;i<20;i++)
{
BSTR bstr = dlg.GetPathName().AllocSysString();
pServer->ExportAgents(bstr);
SysFreeString(bstr);
}
}
if(dlg.DoModal()==IDOK)
{
for(int i = 0;i<20;i++)
{
BSTR bstr = dlg.GetPathName().AllocSysString();
pServer->ExportNomen(bstr);
SysFreeString(bstr);
}
}
str = "C:\\";
bstring = str.AllocSysString();
for(int i = 0;i<20;i++)
{
pServer->ImportNaklad(bstring);
pServer->ClearHistory();
}
SysFreeString(bstring);
bstring = pServer->GetAgentCode();
str = (CString)bstring;
AfxMessageBox(str);
|
|
|
|
|
Hi,
well my problem is a little weird. I am working on a text editor and will working with MSWord i came to realize that when we change the local of the system e.g from english to spanish and then press the right Alt key + E or any other key, word displays special character while Left Alt Key + E has the normal effect ( i.e open the Edit menu ) .
Now i want to implement the functionality in my editor... can anyone please guide me to any relevent material. any help in this regard will be extremely appreciated.
lid.
|
|
|
|
|
Are you wanting to know how to differentiate between the left and right Alt keys?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
can any body help me?i have some value or some generated key values.so i want to store it in database as encrypted and decryption mode.can any body give solution to me?pls..
shan
|
|
|
|
|
hi
Use, Crypt32.lib.
Microsofts Cryptography application programming interface (API)
regards
The Best Relligion is Science.
Once you understand it, you will know God.
|
|
|
|
|
well there are lotz of articles in CP itself....search it with keyword "encryption" or something like that....u ought to find articles.....
happy programmin...
aaah....here is one..
http://www.codeproject.com/file/fileenc.asp[^]
"faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
|
|
|
|
|
I'v deefined my own font for CTreeView, but have one problem. Not all words displays in tree branches. I mean, when I enter text in tree branche "Some text", in CTreeView it displays as "Some t".
So, I want to determine size in chars for tree branche to display all words completely.
Thanks in advance, bilas.
|
|
|
|
|
make sure you've defined a TVINSERTSTRUCT struct like here..
TVINSERTSTRUCT tvis = { 0 };<br />
<br />
tvis.item.mask = TVIF_TEXT | TVIF_CHILDREN;<br />
tvis.item.cChildren = 1;<br />
tvis.item.pszText = "name";<br />
tvis.item.cchTextMax = lstrlen(tvis.item.pszText);<br />
|
|
|
|
|
Yeah, you are rigt. But I need to set cchTextMax every time, when branche is renamed. I'll try again. Anyway, thanks a lot, bilas.
|
|
|
|
|
plz. tell me how can i integrate power point slide in my VC++ MFC dilogue application.
syed abdul qavi
|
|
|
|
|
I've not tried it, but I would suspect Automation would be what you want. You might also look to see if PowerPoint has an ActiveX control.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Hi All,
I have launched an application programmatically. I want to browse and hit all menu items of that application programmatically. How do I do that?
I have pointer to the mainframe window of that launched application. So from that I can get CMenu pointer to its menu using CWnd::GetMenu() fuction.
So now I have CMenu* which points to the menu of launched application. I am using CMenu::GetDefaultItem() to get the identifier and then hitting the menu using SendMessage but nothing is hapening. Any idea? Here is the code..
m_pMenu = m_pMainFrame->GetMenu(); // m_pMenu is of CMenu* type
UINT uItem = m_pMenu->GetDefaultItem(GMDI_GOINTOPOPUPS);
WPARAM wParam = MAKEWPARAM(0,uItem);
int iRet = m_pMainWnd->SendMessage(WM_COMMAND,wParam,NULL);
Do you see anything wrong in above code?
The other approach I adopted was as per below which just iterates though all menu items just to check that if I am able to access all menu items or not..
m_pMenu = m_pMainFrame->GetMenu();
UINT uItem = m_pMenu->GetMenuItemCount();
CString str;
for(int iPos=0; iPos < uItem; iPos++)
{
m_pMenu->GetMenuString(iPos,str,MF_BYCOMMAND);
}
Unfortunately this approach is not at all accurate. For instance If I launch Notepad.exe for then It gives me uItem = 4. But when I browse to get the menu string of each of them. It gives me blank as a string for Ipos= 0, gives Save when ipos=1, Save As for iPos= 2 and &Find for iPos = 3; This is an unexpected result because Save and Save As is part of File menu but Find is in the Edit menu.
Any clue?
--------------
Vaibhav...
|
|
|
|
|
Hi,
I need to find if my machine is On LAN, is there any APIfunction call in C++ which can give me this information.
I'll be very much thankful to you all for the information.
Thanks n Regards.
RuchirDhar Dwivedi
Software Engineer
Windowmaker Software Pvt.Ltd.
Baroda, India.
|
|
|
|
|
|
Hi Thanks but the function IsNetworkAlive() works only for TCP/IP connection, but my machine can be on IPX/SPX isn't there any generic function call which could tell me if my machine is on any type of LAN.
Thanks n Regards.
RuchirDhar Dwivedi
Software Engineer
Windowmaker Software Pvt.Ltd.
Baroda, India.
|
|
|
|
|
hello,
I have a dialog (CAxDialogImpl) and it included an ATLControl::CListViewCtrl. Now I will shown some different tooltips (ATLControls::CToolTipCtrl) depend on the column which the cursor stay. My problem is, I don´t know how. In different forums I found solutions which use function PreTranslateMessage. But this will be only work in a MFC-Dialog.Which event function should I call(OnMouseMove???) and which code should included the eventfunction?????
Can anybody help me???
Thanks for your helping conceptions
|
|
|
|
|
I am unable to display full item text in a yellow rectangle in CListCtrl when mouse is dragged over the item.I dont want to select the item.Can anyone help me please?
|
|
|
|
|
aruc wrote:
I am unable to display full item text in a yellow rectangle...
Are you talking about a tooltip?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Have a look at CWnd::EnableTooltips[^]
There is an example there. A little bit of work is necessary in order to show the appropriate list item data.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|
Thanks for the reply,
I will try that.
|
|
|
|
|