ReadFile API Crashing Program

S

Shawn August

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
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Shawn said:
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.
 
S

Shawn August

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
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top