UnmanagedType.LPStruct & INTERNET_BUFFERS

Giganews Newsgroups
Subject: UnmanagedType.LPStruct & INTERNET_BUFFERS
Posted by:  maxixi (greenxi…@hotmail.com)
Date: Fri, 4 Mar 2005

Hi,

It is my first time to use C#, and i struggle with
my code with wininet api.

Declare API:
public abstract class WinInet
{
  StructLayout(LayoutKind.Sequential)]
  public struct INTERNET_BUFFERS
  {
  public UInt32 dwStructSize;
  public IntPtr Next;
  public String lpcszHeader;
  public UInt32 dwHeadersLength;
  public UInt32 dwHeadersTotal;
  public byte[] lpvBuffer;
  public UInt32 dwBufferLength;
  public UInt32 dwBufferTotal;
  public UInt32 dwOffsetLow;
  public UInt32 dwOffsetHigh;
  };

[DllImport("WinInet.dll",
    EntryPoint="HttpSendRequestEx",
    CallingConvention=CallingConvention.StdCall,
    CharSet=CharSet.Auto,
    SetLastError=true)]
public static extern UInt32 rawHttpSendRequestEx(
  IntPtr hRequest,
  [In, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS lpBuffersIn,
  [Out, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS lpBuffersOut,
  UInt32 dwFlags,
  UInt32 dwContext);
....

Calling:

internal void DownloadThread()
{
  WinInet.INTERNET_BUFFERS bi = new WinInet.INTERNET_BUFFERS();
  WinInet.INTERNET_BUFFERS bo = new WinInet.INTERNET_BUFFERS();

  uint sz = (uint)Marshal.SizeOf(typeof(WinInet.INTERNET_BUFFERS));
  bi.dwStructSize = sz;
  bo.dwStructSize = sz;

  UInt32 ret = WinInet.rawHttpSendRequestEx(m_hRequest,
    bi,
    bo,
    WinInet.HTTPSENDREQUEST.HSR_ASYNC,
    3);

  int err = Marshal.GetLastWin32Error();
....

The call of rawHttpSendRequestEx raise MarshalDirectiveException.
When i changed the buffer as
  [In, MarshalAs(UnmanagedType.Struct)] INTERNET_BUFFERS lpBuffersIn,
  [Out, MarshalAs(UnmanagedType.Struct)] INTERNET_BUFFERS lpBuffersOut,
the calling of rawHttpSendRequestEx failed (no exception)
and raise 87 error (GetLastError()).
The normal result of this code is (as in my VC6 code)
calling HttpSendRequestEx failed with 997 error (ERROR_IO_PENDING).

Whats wrong with my C# code?

Maxixi

Replies