Solved

ATL Lib and File/ComPort IO

Posted on 1998-12-18
3
376 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
Comment Utility
This could be due to several reasons ... could you post some code to clarify??
0
 

Author Comment

by:a3804
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

9 Experts available now in Live!

Get 1:1 Help Now