Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simple IO problem

Posted on 2003-03-12
5
Medium Priority
?
2,229 Views
Last Modified: 2013-11-20
I'm trying to write/read information from a file but I can't seem to get it working.  The error I get is 1784 which is ERROR_INVALID_USER_BUFFER.  Below is the code that I used to connect and write information.  The code should be easy to fix, I've seen many examples of working code, but I could really use some explanation of whats going on here, especially with buffers.  
Thanks.


void CTalkerDlg::OnConnectButton()
{
          m_hComPort = CreateFile(
          TEXT("COM1:"),              
          GENERIC_READ | GENERIC_WRITE,
          0,                              
          NULL,                                  
          CREATE_ALWAYS,
          OPEN_EXISTING,
          FILE_ATTRIBUTE_NORMAL,
          NULL                                  
          )
}


void CTalkerDlg::OnSendButton()
{
               char* lpBuf = "foo";
               DWORD dwWritten;
               WriteFile(m_hComPort, lpBuf, dwWritten, &dwWritten, NULL);
}
0
Comment
Question by:abelblaine111
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 240 total points
ID: 8121743
>>The error I get is 1784 which is ERROR_INVALID_USER_BUFFER.

The error code is correct - you are specifying an incorrect value as the 3rd parameter, which is supposed to be the length of the buffer to write. Use

void CTalkerDlg::OnSendButton()
{
              char* lpBuf = "foo";
              DWORD dwWritten;
              DWORD dwBufSize = lstrlen ( lpBuf);
              WriteFile(m_hComPort, lpBuf, dwBufSize, &dwWritten, NULL);
}


0
 

Author Comment

by:abelblaine111
ID: 8121807
Thanks jkr.  That was the problem.  But what exactly is a buffer?  Is it just any variable? does it have to be a char?  Like I said before my problem is more with conception.  What is a buffer?
Thanks
Abe
0
 

Author Comment

by:abelblaine111
ID: 8121878
Thanks jkr.  That was the problem.  But what exactly is a buffer?  Is it just any variable? does it have to be a char?  Like I said before my problem is more with conception.  What is a buffer?
Thanks
Abe
0
 
LVL 86

Expert Comment

by:jkr
ID: 8122065
>>But what exactly is a buffer?  Is it just any variable? does it have to be a char?

It sort of can be a pointer to any variable type, depending on what you want to write. If you e.g. wanted to send an 'int', you would

             int nBuf = 1234;
             DWORD dwWritten;
             DWORD dwBufSize = sizeof ( int);
             WriteFile(m_hComPort, ( LPVOID) &nBuf, dwBufSize, &dwWritten, NULL);
0
 

Author Comment

by:abelblaine111
ID: 8122713
Thats what I'm looking for.  Thanks jkr.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

580 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