Avatar of riceman0
riceman0 asked on

NativeOverlapped* lpOverlapped = null?

Hello experts:

How would one set the Argument 5 of the function below to be NULL?  The MSDN reference says that if we are not doing an asynchronous write then it needs to be set to NULL.

I have tried 0, null and IntPtr.Zero unsuccessfully.   My attempts to create a fake NativeOverlapped* struct led me down a path where I had to declare unsafe and isolate the WriteFile from any access to necessary buffers and parameters.

The write file call:
success = FileIO.WriteFile
             IntPtr.Zero);   <- error CS1503: Argument '5': cannot convert from 'System.IntPtr' to 'System.Threading.NativeOverlapped*'

Pinvoke method

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
internal static extern unsafe int WriteFile(IntPtr handle, IntPtr buffer, int numBytesToWrite, IntPtr numBytesWritten, NativeOverlapped* lpOverlapped);

I made sure the Write Handle is not flagging the OVERLAPPED argument.

IntPtr pWriteHandle = FileIO.CreateFile

Thanks in advance for any insight.
.NET ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Thanks DaTribe, was playing around with that.  Here is the solution that we got working...

1.  create struct for Overlapped
             System.Threading.NativeOverlapped lpOverLapped = new System.Threading.NativeOverlapped();
             lpOverLapped.InternalHigh = IntPtr.Zero;
             lpOverLapped.EventHandle = IntPtr.Zero;
             lpOverLapped.OffsetHigh = 0;

2.  Use an overloaded WriteFile that does NOT reference the pointer
internal static extern bool WriteFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped);

3.  success = FileIO.WriteFile
                  out bytesWritten,
                  ref lpOverLapped);

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck