Using an API

A

amdrit

Hello Everyone,

I am having an issue using an API and was wondering if you could point out what I am doing wrong. The error that I am getting is 'The paramater is not correct.' I am unable to figure out what paramater the error is referring to.

The API Call is WTSVirtualChannelRead and below is the signature from MSDN.

Thanks in advance


//////////////////////////////////
// //
// BOOL WTSVirtualChannelRead( //
// HANDLE hChannelHandle, //
// ULONG TimeOut, //
// PCHAR Buffer, //
// ULONG BufferSize, //
// PULONG pBytesRead //
// ); //
// //
/////////////////////////////////

[DllImport("Wtsapi32.dll", SetLastError = true)]
private static extern bool WTSVirtualChannelRead(IntPtr handle,
int timeout,
ref byte[] buffer,
int readlength,
ref int bytesRead);

void buttonClick(..)
{
int intHandle =0;
byte[] bytes =new byte[0];
int bytesRead =0;


if(success = Win32.WtsApi32.WTSVirtualChannelQuery(
mHandle,
(int)1, //file handle
ref intHandle,
ref bytesRead
))
{
bytesRead=0;
IntPtr intHandlePtr=(IntPtr)intHandle;

//Wait for 5 seconds before timing out
//Channel Chunk size is 1600 bytes

if(!WTSVirtualChannelRead(intHandlePtr, (int)5000, ref bytes, (int)1600, ref bytesRead))
{
try
{ throw new Win32Exception(Marshal.GetLastWin32Error()); }
catch (Win32Exception e)
{ Console.WriteLine("Read Error:" + e.ToString()); }
}
else
{
Console.WriteLine("Success!");
}
}

}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Take a look at pinvoke.net . Most probably you have your native method incorrectly declared.
If the method in question is not found just look for a method (or methods) that have the same kind of parameters and see how the managed version is declared.

Hello Everyone,

I am having an issue using an API and was wondering if you could point out what I am doing wrong. The error that I am getting is 'The paramater is not correct.' I am unable to figure out what paramater the error is referring to.

The API Call is WTSVirtualChannelRead and below is the signature from MSDN.

Thanks in advance


//////////////////////////////////
// //
// BOOL WTSVirtualChannelRead( //
// HANDLE hChannelHandle, //
// ULONG TimeOut, //
// PCHAR Buffer, //
// ULONG BufferSize, //
// PULONG pBytesRead //
// ); //
// //
/////////////////////////////////

[DllImport("Wtsapi32.dll", SetLastError = true)]
private static extern bool WTSVirtualChannelRead(IntPtr handle,
int timeout,
ref byte[] buffer,
int readlength,
ref int bytesRead);

void buttonClick(..)
{
int intHandle =0;
byte[] bytes =new byte[0];
int bytesRead =0;


if(success = Win32.WtsApi32.WTSVirtualChannelQuery(
mHandle,
(int)1, //file handle
ref intHandle,
ref bytesRead
))
{
bytesRead=0;
IntPtr intHandlePtr=(IntPtr)intHandle;

//Wait for 5 seconds before timing out
//Channel Chunk size is 1600 bytes

if(!WTSVirtualChannelRead(intHandlePtr, (int)5000, ref bytes, (int)1600, ref bytesRead))
{
try
{ throw new Win32Exception(Marshal.GetLastWin32Error()); }
catch (Win32Exception e)
{ Console.WriteLine("Read Error:" + e.ToString()); }
}
else
{
Console.WriteLine("Success!");
}
}

}
 
M

Mattias Sjögren

[DllImport("Wtsapi32.dll", SetLastError = true)]
private static extern bool WTSVirtualChannelRead(IntPtr handle,
int timeout,
ref byte[] buffer,
int readlength,
ref int bytesRead);

'buffer' should not be a ref parameter. You should pass in a buffer
you allocate you self by value.


Mattias
 

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