?
Solved

ATL Lib and File/ComPort IO

Posted on 1998-12-18
3
Medium Priority
?
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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. …
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 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…
Suggested Courses

765 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