Solved

ATL Lib and File/ComPort IO

Posted on 1998-12-18
3
388 Views
Last Modified: 2012-08-13
I have a DLL using ATL which is trying to write to HD and
to COM1. I used the methods CreateFile/WriteFile. My System is WinNT.
Every time I run the DLL from my VB Program, I get a "Access
violation" error. This occurs after the Filehandle has been
closed res. I wrote a byte to COM1. What could be the reason?
0
Comment
Question by:a3804
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 1180449
This could be due to several reasons ... could you post some code to clarify??
0
 

Author Comment

by:a3804
ID: 1180450
Hi jkr,
Here is an example of the code. As I said, everzthing works fine, but after leaving the function I get a "Access Violation".


STDMETHODIMP CCRSData::Save()
{
HANDLE hFile;
char string[] = "WinCRS Version 1.0";
int i;
LPDWORD BytesWritten;
DWORD err;

USES_CONVERSION;
hFile = CreateFile ((LPCTSTR) W2CT(m_CRSData.bstrDateiname),
            GENERIC_WRITE,
            (DWORD) 0,
            NULL,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL,
            NULL);

if (hFile !=INVALID_HANDLE_VALUE) {
      WriteFile ( hFile, string, 18, BytesWritten, NULL);
      WriteFile ( hFile, &m_CRSData.sPunkte_X, sizeof(short), BytesWritten, NULL);
      WriteFile ( hFile, &m_CRSData.sPunkte_Y, sizeof(short), BytesWritten, NULL);
      WriteFile ( hFile, &m_CRSData.sPunkte_Spektrum, sizeof(short), BytesWritten, NULL);
      ...

      if ( CloseHandle (hFile)==0 ) {
      err = GetLastError();            
      }
return S_OK;
}

0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1180451
This is easy...
You'll have to call 'WriteFile()' with the _address_ of a DWORD to receive the number of bytes written, _not_ just an uninitialized LPDWORD... a simple change in your code, and it'll work:

HANDLE hFile;
char string[] = "WinCRS Version 1.0";
int i;
DWORD dwBuffer;
LPDWORD BytesWritten;
DWORD err;

BytesWritten = &dwBuffer; // <--- !!!!!

This is of course just a quick workaround, you should change all calls into e.g.

WriteFile ( hFile, string, 18, &dwBuffer, NULL);


PS: Gruß aus Schwaben ;-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question