Solved

ATL Lib and File/ComPort IO

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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