 |
|
 |
I have it in a test app on VS2005 but when I tried to integrate it in my game it does not work any more with the same code (uses just the same you posted as example)
Unhandled exception ... access violation reading location 0x00000000
at line pXMLDoc->get_documentElement(&base);
in loadProfile function
in this moment pXMLDoc is 0x00000000 and base 0xcccccccc
Help!
Please reply here not on email
|
|
|
|
 |
|
 |
Thanks, it's very useful.
|
|
|
|
 |
|
|
 |
|
 |
<?xml version="1.0" encoding="UTF-8"?>
add the Profile.
-- modified at 8:12 Tuesday 23rd May, 2006
|
|
|
|
 |
|
 |
// XMLProfile.h
// By Emilio Guijarro Cameros
#pragma once
#include
#include
#include
#include
class CXMLProfile
{
class CXMLProfileException
{
public:
TCHAR sMessage[255];
CXMLProfileException(LPTSTR sMessage)
{
lstrcpy(CXMLProfileException::sMessage, sMessage);
}
};
IXMLDOMDocumentPtr pXMLDoc;
IXMLDOMNodePtr blazeEntry(LPCTSTR lpszSection, LPCTSTR lpszEntry);
IXMLDOMNodePtr blazeSection(LPCTSTR lpszEntry);
_bstr_t sFileName;
public:
CXMLProfile(LPCTSTR lpszProfileName);
bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
bool writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszData);
int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);
bool saveProfile();
bool loadProfile();
~CXMLProfile();
};
// XMLProfile.cpp
// By Emilio Guijarro Cameros
#include "stdafx.h"
#include "XMLProfile.h"
// CXMLProfile
CXMLProfile::CXMLProfile(LPCTSTR lpszProfileName) : pXMLDoc(NULL)
{
sFileName = _bstr_t(lpszProfileName);
}
CXMLProfile::~CXMLProfile()
{
}
// CXMLProfile member functions
bool CXMLProfile::writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue)
{
IXMLDOMNodePtr nEntry;
WCHAR szTemp[255];
_itow(nValue, szTemp, 10);
nEntry = blazeEntry(lpszSection, lpszEntry);
nEntry->put_text(_bstr_t(szTemp));
return true;
}
bool CXMLProfile::writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
LPCTSTR lpszData)
{
IXMLDOMNodePtr nEntry;
nEntry = blazeEntry(lpszSection, lpszEntry);
nEntry->put_text(_bstr_t(lpszData));
return true;
}
int CXMLProfile::getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault)
{
IXMLDOMNodePtr nEntry;
BSTR szContent = NULL;
nEntry = blazeEntry(lpszSection, lpszEntry);
nEntry->get_text(&szContent);
if(szContent == NULL || _bstr_t(szContent) == _bstr_t(""))
{
IXMLDOMNodePtr nParent;
nEntry->get_parentNode(&nParent);
nParent->removeChild(nEntry);
return nDefault;
}
return _wtoi(szContent);
}
LPSTR CXMLProfile::getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize)
{
IXMLDOMNodePtr nEntry;
BSTR szContent = NULL;
nEntry = blazeEntry(lpszSection, lpszEntry);
nEntry->get_text(&szContent);
if(szContent == NULL || _bstr_t(szContent) == _bstr_t("")) {
IXMLDOMNodePtr nParent;
nEntry->get_parentNode(&nParent);
nParent->removeChild(nEntry);
return (LPSTR)lpszDefault;
}
if(_bstr_t(szContent).length() < nBufferSize)
{
CHAR* szAsciiContent = _com_util::ConvertBSTRToString(szContent);
strcpy(lpBuffer, szAsciiContent);
}
else
throw CXMLProfileException("Buffer overflow");
return lpBuffer;
}
IXMLDOMNodePtr CXMLProfile::blazeSection(LPCTSTR lpszSection)
{
IXMLDOMElementPtr base, element;
IXMLDOMNodePtr nSec, nResult;
_bstr_t szName;
BSTR bstrDomainDNSName;
bool bSecFound = false, bEntryFound = false;
pXMLDoc->get_documentElement(&base);
for(base->get_firstChild(&nSec); nSec != NULL; nSec->get_nextSibling(&nSec)) {
nSec->get_baseName(&bstrDomainDNSName);
szName =bstrDomainDNSName;
if(szName == _bstr_t(lpszSection)) {
nResult = nSec;
break;
}
}
if(nSec == NULL) {
element =pXMLDoc->createElement(_bstr_t(lpszSection));
nResult =base->appendChild(element);
}
return nResult;
}
IXMLDOMNodePtr CXMLProfile::blazeEntry(LPCTSTR lpszSection, LPCTSTR lpszEntry)
{
IXMLDOMElementPtr base, element;
IXMLDOMNodePtr n1Sec, n1Entry;
pXMLDoc->get_documentElement(&base);
n1Sec =base->selectSingleNode(_bstr_t(lpszSection));
if(n1Sec==NULL) {
element =pXMLDoc->createElement(_bstr_t(lpszSection));
n1Sec =base->appendChild(element);
}
n1Entry =n1Sec->selectSingleNode(_bstr_t(lpszEntry));
if(n1Entry==NULL) {
element =pXMLDoc->createElement(_bstr_t(lpszEntry));
n1Entry =n1Sec->appendChild(element);
}
return n1Entry;
}
bool CXMLProfile::saveProfile()
{
if(pXMLDoc != NULL && SUCCEEDED(pXMLDoc->save(_variant_t(sFileName))))
return true;
else
return false;
}
bool CXMLProfile::loadProfile()
{
if(pXMLDoc != NULL)
return false;
if(SUCCEEDED(pXMLDoc.CreateInstance(__uuidof(DOMDocument))))
{
if(FAILED(pXMLDoc->load(_variant_t(sFileName))))
return false;
}
IXMLDOMElementPtr base;
pXMLDoc->get_documentElement(&base);
if(base == NULL) {
base =pXMLDoc->createElement(_bstr_t("profile"));
pXMLDoc->appendChild(base);
}
return true;
}
|
|
|
|
 |
|
 |
Hi,
I try to compile but the compiler complains about GetAddress() is not a member of '_bstr_t.
I'm using VC++ 6.0 and WTL 7.0. Any suggestion ?
Regards,
Kim
|
|
|
|
 |
|
 |
GetAddress() is not avaitable in VC++ 6.0 COM support header files (only in 7.0+). You can directly use BSTR type instead of _bstr_t in those error situations.
|
|
|
|
 |
|
 |
Thank you for presenting an XML-based solution with a minimum of COM/DOM/SAX voodoo code; I'm glad that the KISS principle (Keep It Simple Stupid!) hasn't been completely stomped into oblivion by Micro$oft.
Unfortunately, the class as presented will leak memory: the string returned by _com_util::ConvertBSTRToString() should be deleted afterwards.
Leaky:
CHAR* szAsciiContent = _com_util::ConvertBSTRToString(szContent);
strcpy(lpBuffer, szAsciiContent);
Fixed:
CHAR* szAsciiContent = _com_util::ConvertBSTRToString(szContent);
strcpy(lpBuffer, szAsciiContent);
delete [] szAsciiContent;
Cheers!
Humble Programmer
,,,^..^,,,
|
|
|
|
 |
|
 |
In XMLProfile.h you use
class CXMLProfileException
{
public:
TCHAR sMessage[255];
CXMLProfileException(LPTSTR sMessage)
{
lstrcpy(CXMLProfileException::sMessage, sMessage);
}
};
What's if the Message longer than 254 Chars?
Corrupted stack, potential attack with buffer overflow.
class CXMLProfileException
{
public:
TCHAR sMessage[255];
CXMLProfileException(LPTSTR sMessage)
{
lstrcpyn(CXMLProfileException::sMessage, sMessage,255);
CXMLProfileException::sMessage[254] = _T('\0'); // Make sure that string ends with zero
}
};
Regards
Der Albert
|
|
|
|
 |
|
 |
Just a few comments.
- You don't need to use MFC. To break the dependency, all you need to do is:
1. Don't inherit from CObject
2. Use std::string instead of CString
3. Use _bstr_t instead of CComBSTR
4. Use _variant_t instead of COleVariant
- As has been pointed out already, you could use the stack instead of the heap for temporary variables.
- It would be better to allow the caller to do a save and load when they want rather than just at construction and destruction.
- Needs more error checking, eg. checking pointers aren't NULL before you use them!
Apart from that it is a nice little class. Well done!
Regards
Jon
|
|
|
|
 |
|
 |
How is can use with Unicode?
Thanking of much
|
|
|
|
 |
|
 |
Helo,
I is get unhandled exception, access violaton. I using the instance, and then I does:
profile.WriteProfileString(_T("test section"), _T("test entry"), element);
Is violating access on the WriteProfileString. What I is the wrong doing?
|
|
|
|
 |
|
 |
Is anybody having sample projeckt. I is not successfully, and I am wanting to be using this.
Thank youu
|
|
|
|
 |
|
 |
The following should work
profile.WriteProfileString(_T("test section"), _T("test_entry"), element);
There are, among other things, no whitespaces allowed for the first two parameters. The function does not check if the creation of the entry was successful and attempts to access a null pointer then.
|
|
|
|
 |
|
 |
I tried to compile this but my msxml.h file does not define the types used. What file should I be using? And if not the msxml.h that comes with VC98, what MSDN disk or URL should I use to find it?
|
|
|
|
 |
|
 |
In addition, the code does not specify that atlbase.h is required.
A working example showing how to use this code would help a lot.
|
|
|
|
 |
|
 |
I´ve designed this code under VC7, and it has not been tested under VC6 ... I don´t know if these header are avaitable for earlier VC versions.
The use of this code is just the same as WriteProfile... functions included with MFC, which has some samples in MSDN.
"When I look into your eyes, there´s nothing there to see, nothing but my own mistakes staring back at me"
|
|
|
|
 |
|
 |
Just add #include <msxml2.h> Best, Alex Y. http://www.alerma.com
|
|
|
|
 |
|
 |
The files do not appear to exist under VS6.
|
|
|
|
 |
|
 |
That is correct. Install MS Platform SDK.
Alex Y.
http://www.alerma.com
|
|
|
|
 |
|
 |
Thanks a lot. I modified the class to write to a file specified at the constructor, otherwise, fits me perfectly. I hope you don't mind.
Thanks again.
Djof
"Je me souviens"
|
|
|
|
 |
|
 |
Enjoy it. I aleady modified the constructor when using this class, but I didn´t upload it.
"When I look into your eyes, there´s nothing there to see, nothing but my own mistakes staring back at me"
|
|
|
|
 |
|
 |
hi,
I have had problems compiling the code but got there in the end, I now have an exception error
on: pXMLDoc->load( COleVariant( szFileName));
The file name is valid and so is pXMLDoc. The exception seems to happen
on the _raw_load(xmlSource, &_result); within the IXMLDOMDocument::load();
I also had problems with the CLSID_XMLDOMDocument and IID_IXMLDOMDocument
defines which I had to change to CLSID_XMLDocument and IID_IXMLDocument respectively.
I am sure I am missing something obvious here.
I am using VC6 under win 2k pro and have IE6.
Can anybody help ??
thanks
jeff
|
|
|
|
 |
|
 |
Hi,
I´ve developed this class under VC7, with MSXML4 engine installed, and IE6... maybe your versions are´nt newer enough to support the interfaces that I use, and this crashes your program.
"When I look into your eyes, there´s nothing there to see, nothing but my own mistakes staring back at me"
|
|
|
|
 |
|
 |
I just ran into this error and wanted to leave an FYI for anyone else that might be having trouble with 2k. You can
fix this problem by adding one update for 2K:
MS IE 6, SP1.
IF you haven't updated in a while, it will be the first update suggested for you anyway. The MSXML4.0 file you need comes in this update, but it's not the only thing you need. I tried just
adding that update by itself and still had the problem.
Enjoy!
|
|
|
|
 |