468,505 Members | 1,623 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

ReadFile API Crashing Program

Hello:

I am converting a working VB6 program to C#. During testing of the C#
version, I noticed the ReadFile API is crashing. The parameters going into
the this function are identical to the working VB6 version. I have tried
changing the returned buffer datatype from string to object. The program
does not crash (no work) if I remove the ref argument on the 2nd argument
within the ReadFile API ('psBuffer'). A quick API sniffer revealed:
"EXCEPTION_ACCESS_VIOLATION". Here's the code:

DEFINITION
==========
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int ReadFile
(int hFile, ref string psBuffer, int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead, ref OVERLAPPED olOverlapped);

CALL
====
// Read
try
{
this.c_sStringBuffer = new string(' ', (int)this.c_lRecLen);
liRC = Win32Support.ReadFile(this.c_intFileHndl, ref
this.c_sStringBuffer, (int)this.c_lRecLen, ref liBytesRead, ref
this.c_olMudt);
this.c_lBytesRead = (this.c_lBytesRead + liBytesRead);
}
catch(Exception ErrorException)
{
throw new ApplicationException("Could not read the specified file. " +
ErrorException.Message);
}

Any ideas?

-Shawn
Nov 16 '05 #1
3 2989
Shawn August wrote:
Hello:

I am converting a working VB6 program to C#. During testing of the C#
version, I noticed the ReadFile API is crashing. The parameters going into
the this function are identical to the working VB6 version. I have tried


If you are indeed reading a file you should rewrite the code to use the
FileStream class or similar.

If you don't know how to do interop, blindly converting code to .net
will get you into trouble, as you've already found out.

You should not pass a String to a method that expects to change the
string data, you should use StringBuffer, so the definition for ReadFile
is wrong.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2
Thanks for the reply.

The original VB6 program works just fine and I am using a very similiar
signature. If I was writing the code, I may do things differently but why
rewrite working code? Well, working in the VB6 program anyhow. :)

What is a 'StringBuffer'? I have tried other datatypes for the return buffer
such as string and object but nothing seems to work. I guess I am not
following you. What is the 'right' definition?

Thanks again.

-Shawn

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:uY**************@TK2MSFTNGP09.phx.gbl...
Shawn August wrote:
Hello:

I am converting a working VB6 program to C#. During testing of the C#
version, I noticed the ReadFile API is crashing. The parameters going
into the this function are identical to the working VB6 version. I have
tried


If you are indeed reading a file you should rewrite the code to use the
FileStream class or similar.

If you don't know how to do interop, blindly converting code to .net will
get you into trouble, as you've already found out.

You should not pass a String to a method that expects to change the string
data, you should use StringBuffer, so the definition for ReadFile is
wrong.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B

Nov 16 '05 #3
Shawn,
If I was writing the code, I may do things differently but why
rewrite working code?
If you're translating it to C# you _are_ already rewriting the code,
no? Then you might as well pick a more appropriate and easy to use
API.

What is a 'StringBuffer'?
I believe Lasse means StringBuilder, located in the System.Text
namespace.

What is the 'right' definition?


Check www.pinvoke.net

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Daragoth | last post: by
1 post views Thread by Jørn Dahl-Stamnes | last post: by
8 posts views Thread by code break | last post: by
4 posts views Thread by Eric Renken | last post: by
1 post views Thread by mkarja | last post: by
15 posts views Thread by Ketchup | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.