Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

big problem appear when i change between consol to win32

2 views
Skip to first unread message

Sa3Q

unread,
Nov 16, 2009, 11:53:31 PM11/16/09
to sa...@live.com
/*
this code for generate xml file for the hardisk partion drivers
Tree
this code when i execute it in the console application it run
perfect but if i use win32 application it appear to me this problem
(Debug Error! DAMAGE: after Normal block (#259) at 0x00421D00)

i use visul c++ version "6"
please can any body help me for that

thank you
*/

#include <windows.h>
#include "wchar.h"
#include <STDIO.h>
#include <string.h>


int _scprintf(const char* format, ...){
va_list paramList;
va_start(paramList, format);

int size = vprintf(format, paramList);

va_end(paramList);

return size;
}

char *create_tab_buffer(int n)
{
char *result = new char[n + 1];
for(int i = 0; i < n; i++)
result[i] = '\t';
result[n] = 0;
return result;
}

class XMLFile
{
public:
XMLFile(char *pszFilename);
~XMLFile();

void openNewDirectoryLevel(char *pszName);
void closeCurrentDirectoryLevel();
void addNewFile(char *pszName, int size);

private:
HANDLE m_hFile;
int m_nTabs;
};

XMLFile::XMLFile(char *pszFilename)
{
char szBuffer[] = "<?xml version=\"1.0\"?>\n";
unsigned long dwWritten;

m_hFile = CreateFile(pszFilename, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
WriteFile(m_hFile, szBuffer, strlen(szBuffer), &dwWritten, NULL);

m_nTabs = 0;
}

XMLFile::~XMLFile()
{
CloseHandle(m_hFile);
}

void XMLFile::openNewDirectoryLevel(char *pszName)
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;

pTabBuffer = create_tab_buffer(m_nTabs);
iLength =_scprintf("%s<directory name=\"%s\">\n", pTabBuffer,
pszName) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s<directory name=\"%s\">\n", pTabBuffer,
pszName);

WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);

delete[] pszBuffer;
delete[] pTabBuffer;

m_nTabs++;
}

void XMLFile::closeCurrentDirectoryLevel()
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;

pTabBuffer = create_tab_buffer(--m_nTabs);
iLength = _scprintf("%s</directory>\n", pTabBuffer) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s</directory>\n", pTabBuffer);

WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);

delete[] pszBuffer;
delete[] pTabBuffer;
}

void XMLFile::addNewFile(char *pszName, int size)
{
char *pszBuffer, *pTabBuffer;
unsigned long dwWritten;
int iLength;

pTabBuffer = create_tab_buffer(m_nTabs);
iLength = _scprintf("%s<file name=\"%s\" size=\"%d\"/>\n",
pTabBuffer, pszName, size) + 1;
pszBuffer = new char[iLength];
sprintf(pszBuffer, "%s<file name=\"%s\" size=\"%d\"/>\n", pTabBuffer,
pszName, size);

WriteFile(m_hFile, pszBuffer, iLength - 1, &dwWritten, NULL);

delete[] pszBuffer;
delete[] pTabBuffer;
}

char *getCurrentDir()
{
char *pszCurrentDir;
int length;

length = GetCurrentDirectory(0, NULL);
pszCurrentDir = new char[length + 1];
GetCurrentDirectory(length + 1, pszCurrentDir);

return pszCurrentDir;
}

void traverse(XMLFile *pOut, char *directory)
{
char *pszCurrentDir = new char[strlen(directory) + 3];
WIN32_FIND_DATA wfd;
HANDLE hFind;

pOut->openNewDirectoryLevel(directory);
sprintf(pszCurrentDir, "%s\\*", directory);
hFind = FindFirstFile(pszCurrentDir, &wfd);
if(hFind != INVALID_HANDLE_VALUE) {
do {
if(strcmp(wfd.cFileName, ".") && strcmp(wfd.cFileName, "..")) {
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
char *pszCurrentDir = new char[strlen(directory) + strlen
(wfd.cFileName) + 2];
sprintf(pszCurrentDir, "%s\\%s", directory, wfd.cFileName);
traverse(pOut, pszCurrentDir);
delete[] pszCurrentDir;

} else {
pOut->addNewFile(wfd.cFileName, wfd.nFileSizeLow);
}
}
} while(FindNextFile(hFind, &wfd) != 0);
}
pOut->closeCurrentDirectoryLevel();
FindClose(hFind);

delete[] pszCurrentDir;
}

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)

{
XMLFile outc("c:\\d.xml");
char *pszCurrentDirc = "D:\\";
traverse(&outc, pszCurrentDirc);
while(1){Sleep(200);}
return 0;
}

Todd Aspeotis

unread,
Nov 17, 2009, 5:28:29 AM11/17/09
to
Did you change between Console Release and Win32 Debug? This error can occur
with heap corruption from buffer overflow, eg

int num_of_ints = 2;
int *pi = (int *)malloc(num_of_ints); // should be num_of_ints * sizeof(
int )

for( int x = 0; x < num_of_ints; x++ )
pi[x] = 1234;

free(pi); // Error will occur here, you have overflowed


You may want to set breakpoints at your delete[] statements and step over
them. Alternatively run in debug mode and when the application breaks you
can step up through the stack to find which line in your code triggers the
break.

You may find it useful to artificially inflate each buffer one at a time (eg
new char[iLength + 10000]) to see if it is an overflow error.

"Sa3Q" <s3q...@gmail.com> wrote in message
news:bca74b9a-d828-4836...@r24g2000yqd.googlegroups.com...

Alf P. Steinbach

unread,
Nov 17, 2009, 6:26:45 AM11/17/09
to
* Sa3Q:

> i use visul c++ version "6"

Don't know if this will compile with MSVC 6.0 (hey, it's time to upgrade!), but
I translated your mostly-C code to C++.

It's not how I'd done it, but it's a fairly direct translation.

Note how it's shorter, simpler and works:


<code>
#include <windows.h>
#include <string>
#include <fstream>

std::string tabs( int n ) { return std::string( 4*n, ' ' ); }

class XMLFile
{
public:
XMLFile( std::string const& name );

void openNewDirectoryLevel( std::string const& name );
void closeCurrentDirectoryLevel();
void addNewFile( std::string const& name, int size);

private:
std::ofstream m_file;
int m_nTabs;

XMLFile( XMLFile const& ); // No such.
XMLFile& operator=( XMLFile const& ); // No such.
};

XMLFile::XMLFile( std::string const& name )
: m_file( name.c_str() )
, m_nTabs( 0 )
{
m_file << "<?xml version=\"1.0\"?>\n";
}

void XMLFile::openNewDirectoryLevel( std::string const& name )
{
m_file << tabs( m_nTabs ) << "<directory name=\"" << name << "\">\n";
++m_nTabs;
}

void XMLFile::closeCurrentDirectoryLevel()
{
--m_nTabs;
m_file << tabs( m_nTabs ) << "</directory>\n";
}

void XMLFile::addNewFile( std::string const& name, int size )
{
m_file << tabs( m_nTabs ) << "<file name=\"" << name << "\"/>\n";
}

std::string getCurrentDir()
{
int const length = GetCurrentDirectory(0, NULL);
std::string result = std::string( length + 1, '\0' );

GetCurrentDirectory( length + 1, &result[0] );
result.resize( length );

return result;
}

void traverse( XMLFile& out, std::string const& directory )
{
WIN32_FIND_DATA wfd;
HANDLE hFind;

out.openNewDirectoryLevel( directory );
hFind = FindFirstFile( (directory + "\\*").c_str(), &wfd );
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
if( strcmp(wfd.cFileName, ".") && strcmp( wfd.cFileName, ".." ) )
{
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
traverse( out, directory + "\\" + wfd.cFileName );
}
else
{
out.addNewFile( wfd.cFileName, wfd.nFileSizeLow );
}
}
} while( FindNextFile(hFind, &wfd ) != 0 );
}
out.closeCurrentDirectoryLevel();
FindClose(hFind);
}

int main()
{
XMLFile outc( "d.xml" );
traverse( outc, "c:\\test" );
}
</code>


Cheers & hth.,

- Alf

Sa3Q

unread,
Nov 17, 2009, 6:56:15 AM11/17/09
to


this code not run :(

Alf P. Steinbach

unread,
Nov 17, 2009, 10:14:14 AM11/17/09
to
* Sa3Q:

>
> this code not run :(

If you want help with that then you'll have to supply the relevant information.

Sa3Q

unread,
Nov 18, 2009, 3:07:57 AM11/18/09
to

thank you the code run perfect on msvc 6

i try to compile it on
msvc 9
but it not run
thank you thank you

0 new messages