🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13234

Unexpected Access Violation when calling WriteFile()

#13234
From: Fernando
Date: Tue, 21 Oct 2003 14:43
67 lines
2612 bytes
Hi,

I'm trying to extract a resource from my exe and then save it to a file.  When
I try to save it, I get an access violation.  :-?

I suspect the culprit is my usage of the API function WriteFile(), but I'm not
sure.  WriteFile(9 does write the file and doesn't return any error...

Here's the code I'musing to extract a resource with ID 100 and type POG:

Any help would be appreciated. O:-)

Thanks!

HANDLE openFile( const std::string & aFileName )
  {


    return ::CreateFile( aFileName.c_str(),
                         GENERIC_WRITE,
                         0,
                         NULL,
                         CREATE_NEW,
                         FILE_ATTRIBUTE_NORMAL,
                         NULL );


  }

  bool writeToFile( HANDLE aFileHandle,
                                  void * data,
                                  DWORD numOfBytes )
  {
    LPDWORD bytesWritten;
    return ::WriteFile( aFileHandle,
                        data,
                        numOfBytes,
                        bytesWritten,
                        NULL );
  }

void dumpToFile(void)
{
 HRSRC theRes; // resource handle

 theRes = ::FindResource( NULL, "#100", "POG" );

 if( theRes != NULL )
 {
   void * theResData;
    HANDLE fileHandle;
    bool result;
    HRSRC theResLoad;       // handle to loaded resource
    DWORD theSize;

    theSize = ::SizeofResource( NULL, theRes );
    theResLoad = ::LoadResource( NULL, theRes );
    theResData = ::LockResource( theResLoad );

    fileHandle = openFile( "test.dat" );
    if ( fileHandle != INVALID_HANDLE_VALUE )
      {
        result = writeToFile( fileHandle, theResData, theSize );
        ::CloseHandle( fileHandle );
      }
 }
}
Technical Details
Message-ID: <7bhapvkde568fjnf10ruf9t114kirltdr5@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,6fe7d1767c79674a,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-10-21 07:47:06 PST
From: Fernando  <frr@easyjob.net>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Unexpected Access Violation when calling WriteFile()
Date: Tue, 21 Oct 2003 16:43:55 +0200
Message-ID: <7bhapvkde568fjnf10ruf9t114kirltdr5@4ax.com>
X-Newsreader: Forte Agent 1.9/32.560
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 217.127.66.80
X-Trace: newsgroups.borland.com 1066747454 217.127.66.80 (21 Oct 2003 07:44:14 -0700)
Lines: 68
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail