Solved

ATL Lib and File/ComPort IO

Posted on 1998-12-18
3
382 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now