TransmitFile() API

  • Thread starter Thread starter Shariq Khan
  • Start date Start date
Shariq,
Rather then use P/Invoke have you considered using .NET classes?

Such as WebClient.UploadFile?

Hope this helps
Jay
 
Jay:
(I am not trying to upload a file.)

The application is a small web server that runs on client machines to serve
content to the browser (also running locally.) Currently, to send the file
through the socket, I open the file, read chunks at a time and write them to
the socket. Performance is ok at best -- so I am looking to improve it by
using the TransmitFile api.

All I need if someone has already used the API to give me the declarations
so it can save time for me to translate it and the two structures it uses.
Thanks in advance.

Shariq
 
Shariq,
(I am not trying to upload a file.)
Upload a file, transmit a file, other then protocol & nomenclature is there
really a difference???

Remember that .NET supports pluggable protocols, WebClient.UploadFile can
actually support any protocol you want...

The application is a small web server that runs on client machines to
serve content to the browser (also running locally.)
I can see where WebClient.UploadFile probably won't work in this scenario...


Have you tried asking in the microsoft.public.dotnet.framework.interop
newsgroup? As TransmitFile includes OVERLAPPED, which I always have trouble
with...

When I need to do P/Invoke I normally reference Adam Nathan's book ".NET and
COM - The Complete Interoperabiility Guide" from SAMS Press. Adam lists the
P/Invoke signatures for GDI32, KERNEL32, OLE32, SHELL32, and USER32,
unfortunately not mswsock.dll...

Hope this helps
Jay
 
Ok here it is, for those who might need it in the future:

[DllImport("mswsock.dll", SetLastError=true)]
internal static extern bool TransmitFile(
[In] IntPtr socket,
[In] IntPtr fileHandle,
[In] int numberOfBytesToWrite,
[In] int numberOfBytesPerSend,
[In] IntPtr overlapped,
[In] TransmitFileBuffers buffers,
[In] TransmitFileFlags flags);

[StructLayout(LayoutKind.Sequential)]
internal class TransmitFileBuffers
{
internal IntPtr preBuffer;
internal int preBufferLength;
internal IntPtr postBuffer;
internal int postBufferLength;
public TransmitFileBuffers()
{
}
}

[Flags]
public enum TransmitFileFlags
{
// Fields
Disconnect = 1,
ReuseSocket = 2,
UseDefaultWorkerThread = 0,
UseKernelApc = 0x20,
UseSystemThread = 0x10,
WriteBehind = 4
}

In my usage, I can simply send the IntPtr.Zero for the overlapped parameter
and it does the job. I am going to do some performance testing to verify
that it is actually better than reading the file and writing to the socket.

Thanks for everyone's help.
Shariq
 
Back
Top