473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

UnmanagedType.LPStruct & INTERNET_BUFFERS

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_BUFFE RS));
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
Nov 16 '05 #1
4 6416
public byte[] lpvBuffer;
This should be an IntPtr.

[In, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS lpBuffersIn,
[Out, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS lpBuffersOut,


You don't need the MarshalAs attribute here, but you have to make
these ref parameters.

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 #2
I change my wininet declare, but one question

My VC code like this:
C++
INTERNET_BUFFERS BufferIn;
memset(&BufferIn, 0, sizeof(INTERNET_BUFFERS));
BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);

BufferIn.lpvBuffer = (void*)m_sFormPostData.c_str();
BufferIn.dwBufferTotal = BufferIn.dwBufferLength =
m_sFormPostData.length();

ResetContext();
if (!HttpSendRequestEx(hInternet, &BufferIn, NULL, <--null param
HSR_ASYNC, (DWORD)pContext)) {
if (GetLastError() != ERROR_IO_PENDING) {
ret = 1;
goto err;
}

How to change the 3rd param with NULL in C#?
UInt32 ret = WinInet.rawHttpSendRequestEx(m_hRequest,
ref bi,
null, <-- Compiler say error!
WinInet.HTTPSENDREQUEST.HSR_ASYNC,
3);

I can not use null as the parameter!

How to
"Mattias Sjögren" <ma********************@mvps.org> ????
news:er**************@TK2MSFTNGP10.phx.gbl...
public byte[] lpvBuffer;
This should be an IntPtr.

[In, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS lpBuffersIn,
[Out, MarshalAs(UnmanagedType.LPStruct)] INTERNET_BUFFERS

lpBuffersOut,
You don't need the MarshalAs attribute here, but you have to make
these ref parameters.

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 #3

If you want to pass NULL you can change the parameter to IntPtr (by
value) and use IntPtr.Zero as the argument.

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
I can declare INTERNET_BUFFERS as class
and pass the null to bufferin & bufferout

Thanks Mattias Sjögren

"Mattias Sjögren" <ma********************@mvps.org> ????
news:eg*************@tk2msftngp13.phx.gbl...

If you want to pass NULL you can change the parameter to IntPtr (by
value) and use IntPtr.Zero as the argument.

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 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
0
by: [Yosi] | last post by:
I have DLL (unmannaged code) , in this DLL I have some functions and structures. one of those functions is GetDataBaseStructure (GDS_Structure *str), the structure definition is as following : ...
0
by: Stanislav Simicek | last post by:
Hello, I've noticed that MC++ compiler (VS.NET 2003) marks "bool" type with MarshalAs(UnmanagedType.U1) attribute, even if System::Boolean is used. I would understand, why this happens for C++...
8
by: **Developer** | last post by:
Been reading some of the Marshal doc and am a little confused. Anyway, I've been using the following and it works OK. Public Declare Auto Function SHGetFolderPath Lib "shell32.dll" (ByVal hWnd...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
0
by: Charming12 | last post by:
Hi , I need to pass Dynamic Size in the statement Is there anyway to pass a dynamic value in sizeconst or is there any other statement likethis which can take dynamic Size. Help will be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.