CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Nov 2011
    Posts
    72

    WriteFile(...) exception

    Windows XP Pro, Visual Studio 2008, MFC application.
    When I call
    Code:
    WriteFile( m_log_file_handle, &m_log_message, m_write_length, mp_bytes_written,
                    NULL );
    I wind up in the debugger about 13 levels deep and do not understand what it is trying to tell me.

    My solution is:
    Code:
    try{
      WriteFile( ... );
      }
    catch
      {
       // need help here
    }
    I don't know what can be thrown and my Visual Studio help for WriteFile says nothing about exceptions. What can I put in the catch section to give me a clue as to what it does not like about my WriteFile call.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: WriteFile(...) exception

    Have you checked the values of the parameters when you get the crash?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: WriteFile(...) exception

    WriteFile is a C function from Windows API.
    C language has not a clue about C++ exceptions.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Nov 2011
    Posts
    72

    Re: WriteFile(...) exception

    Well, I thought I had the parameters right. So I changed my declarations to:
    Code:
    DWORD m_bytes_written;
    LPDWORD mp_bytes_written;
    ...
    <constructor>
    mp_bytes_written = &m_bytes_written;
    ...
    WriteFile( ..., mp_bytes_written, ... )
    and now it works.

    However, that still leaves me with the open question, how do I catch a general exception and determine what went wrong?
    Ok, given WriteFile does not know anything about expceptions, but Visual Studio does. How do I get it to tell me why it is throwing the exception.

    (ps, I tried a search of this site. I am on an Air Force installation and this computer thows up a forbidden page alert. My google searches did not discover a general answer.)

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    Well, I thought I had the parameters right. So I changed my declarations to:
    Code:
    DWORD m_bytes_written;
    LPDWORD mp_bytes_written;
    ...
    <constructor>
    mp_bytes_written = &m_bytes_written;
    ...
    WriteFile( ..., mp_bytes_written, ... )
    and now it works.
    Why did you need to do all of this just to pass an address of a DWORD?
    Code:
    DWORD m_bytes_written;
    WriteFile( ..., &m_bytes_written, ... )
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    I don't know what can be thrown and my Visual Studio help for WriteFile says nothing about exceptions.
    It isn't WriteFile that threw the exception. The issue is that you probably have an access violation or some other system exception.

    Look up Structured Exception Handling (SEH) if you need further information as to how to catch system exceptions.
    How do I get it to tell me why it is throwing the exception
    If you're debugging your code, look at the Output Window. It will state the reason for the exception. Also, you should see numbers like 0xC00000005, telling you what the exception is.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Nov 2011
    Posts
    72

    Re: WriteFile(...) exception

    Quote Originally Posted by Paul McKenzie View Post
    Why did you need to do all of this just to pass an address of a DWORD?
    Code:
    DWORD m_bytes_written;
    WriteFile( ..., &m_bytes_written, ... )
    Regards,

    Paul McKenzie
    No valid reason. I just saw the LPDWORD in the help file and declared that without thinking, then had to declare the pointer, ....
    Thanks for the comment.

    Regarding the output window, it just said something like no more endpoints and nothing of value to me. I still wish to know how to catch the exception and put up some type of decent error messages. The Catch( ...) should work, but where do I find the reason for the thrown exception?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    Regarding the output window, it just said something like no more endpoints and nothing of value to me.
    Maybe it is of great value to the ones helping you here. Please post what the output window is stating concerning the exception.
    The Catch( ...) should work, but where do I find the reason for the thrown exception?
    Again, look up Structured Exception Handling.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Nov 2011
    Posts
    72

    Re: WriteFile(...) exception

    re: Again, look up Structured Exception Handling.

    Ok, in progress.

    Maybe a re-direct is warrented. I need to write a log file in a real time application. (It processes telemetry data and really is real time.) The environemnt is Visual Studio 2008, MFC, and C++. A search got me going with:

    CreateFileA(...)
    and
    WriteFile(...)

    Is this a good approach for text based log file in an MFC application? Would something like this be better:

    StreamWriter^ sw = gcnew StreamWriter(fileName);

    I found that in an MSDN web page.

    Thanks for your time.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    ...
    Is this a good approach for text based log file in an MFC application? Would something like this be better:
    Code:
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    I found that in an MSDN web page.
    This code has nothing to do with an MFC application. Nor is it a native C++ code.
    Microsoft developed a lot of new languages and/or language extensions, and so called "managed C++/CLI" is one of them.
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2011
    Posts
    72

    Re: WriteFile(...) exception

    Quote Originally Posted by VictorN View Post
    This code has nothing to do with an MFC application. Nor is it a native C++ code.
    Microsoft developed a lot of new languages and/or language extensions, and so called "managed C++/CLI" is one of them.
    Interesting. A google search for the phrase:
    write text file mfc c++
    Noting I specifically included "mfc" and "C++" in my search, turned up this page:
    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
    and there under the title File Handling with Visual C++ is a section on writing text files, that links to here: http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx and says to use that. So I got there with what appears to be a fully legimitate search.

    But as I read your post, you say Nay.
    OK, please suggest a specific method that I should use to write my text file. Do you have a link to your favorite page that gives some example code?

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    Maybe a re-direct is warrented. I need to write a log file in a real time application. (It processes telemetry data and really is real time.)
    What Windows OS are you building for? Windows desktop OS'es are not real-time operating systems.
    Would something like this be better:
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    Hate to break it to you, but that code is not C++.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Nov 2011
    Posts
    72

    Re: WriteFile(...) exception

    Quote Originally Posted by Paul McKenzie View Post
    What Windows OS are you building for? Windows desktop OS'es are not real-time operating systems.
    Hate to break it to you, but that code is not C++.

    Regards,

    Paul McKenzie
    Paul,
    Yes, I am fully aware that windows is not real time. However, companies like Wyle TDS (Telemetry and Data Systems, well known in the telemetry business) build their telemetry system under Windows XP Pro and companies like Symvionics build their display systems called IADS (Interactive Analysis and Display System, again well known in the telemetry business) under Windows XP Pro. Both serve a real time purpose and are used real time, and I must deal with them. And now both are migrating to Windows 7. Still not a real time system, but that is what the vendors use.

    A template application provided by Wyle TDS to use as a starter application uses MFC to fetch data from their decommutation system so that is the foundation of my code to grab data, process it, and forward it to IADS.

    Evidently my search yielded some results that are not fully appropriate for the MFC environmet.

    So what should I use to open and write a text based log file?

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: WriteFile(...) exception

    Quote Originally Posted by bkelly View Post
    So I got there with what appears to be a fully legimitate search.
    But as I read your post, you say Nay.
    This forum deals with C++ as described by the ANSI/ISO standard, with emphasis on the Visual C++ compiler and libraries (MFC, ATL).

    That code you have is not ANSI C++. Try to take that code and compile it in an MFC or Win32 project. You will get errors galore.
    Code:
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    There is no such thing as "gcnew", or the "^" character next to the type like that in ANSI C++. In C++, the only use for "^" is for the XOR operation (unless you overload it to do something else).

    Regards,

    Paul McKenzie

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: WriteFile(...) exception

    Well, CreateFile and WriteFileWindows API should (I'd say must) work good if you used them right.
    What exactly is your problem? Why don't you want to show your actual code producing the problems?
    Did you try something with SEH as Paul suggested?
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured