IInternetProtocol's Read method (pulling my hair out)

B

biogates

I'm writing a small app in C# and I managed to let the browser call my
IInternetProtocol's Start and Read methods.
Problem is: I can't pass the data back to IE.

I have tried various signatures for Read, including:

public int Read(
IntPtr pv,
int cb,
out IntPtr pcbRead
);

public int Read(
byte[] pv,
int cb,
out uint pcbRead
);

and all the combinations in between.
IE keeps saying "Action cancelled" or nothing at all (blank page).

Read returns 0 if further data is available, 1 if data is over, and
it's prepared to get further notifications after data is finished.

I can't really figure out how I should stuff data in pv and pcbRead, so
they result back in IE.
If someone can give out any clues, any help would be greatly
appreciated.

Mirco
 
N

Nicholas Paldino [.NET/C# MVP]

Mirco,

Part of the problem is the interface definition. There are two things
you need to do. The first is to use the PreserveSig attribute, so that it
will take the return value as an HRESULT. Your method (on the interface)
should look like this:

[PreserveSig]
[return:MarshalAs(UnmanagedType.Error)]
int Read(
IntPtr pv,
[MarshalAs(UnmanagedType.U4)] cb,
[MarshalAs(UnmanagedType.U4)] ref pcbRead);

Notice the ref on the last parameter as well. This should work once you
define it this way.

Hope this helps.
 
B

biogates

Nicholas,
Thank you for replying to my post.
Indeed I think that was part of the problem, I followed your advice,
but still no luck with passing data back to IE.
I suspect there's a problem in the way I handle IntPtr and byte[].
Can you find something wrong with something even as simple as this?

public int Read(
IntPtr pv,
uint cb,
ref uint pcbRead
)
{
....

string str = "This is a silly attempt";

for (int k = 0; k < str.Length; k++)
{
((byte *)pv.ToPointer())[k] = (byte)(str[k]);
}
}

Thanks,
Mirco

Mirco,

Part of the problem is the interface definition. There are two things
you need to do. The first is to use the PreserveSig attribute, so that it
will take the return value as an HRESULT. Your method (on the interface)
should look like this:

[PreserveSig]
[return:MarshalAs(UnmanagedType.Error)]
int Read(
IntPtr pv,
[MarshalAs(UnmanagedType.U4)] cb,
[MarshalAs(UnmanagedType.U4)] ref pcbRead);

Notice the ref on the last parameter as well. This should work once you
define it this way.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm writing a small app in C# and I managed to let the browser call my
IInternetProtocol's Start and Read methods.
Problem is: I can't pass the data back to IE.

I have tried various signatures for Read, including:

public int Read(
IntPtr pv,
int cb,
out IntPtr pcbRead
);

public int Read(
byte[] pv,
int cb,
out uint pcbRead
);

and all the combinations in between.
IE keeps saying "Action cancelled" or nothing at all (blank page).

Read returns 0 if further data is available, 1 if data is over, and
it's prepared to get further notifications after data is finished.

I can't really figure out how I should stuff data in pv and pcbRead, so
they result back in IE.
If someone can give out any clues, any help would be greatly
appreciated.

Mirco
 

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