all groups > dotnet interop > july 2006 >
You're in the

dotnet interop

group:

Problem calling DeviceIoControl



Problem calling DeviceIoControl eduwushu
7/27/2006 11:58:02 AM
dotnet interop: Hi Im trying tu use deviceiocontrol to retrieve serial port informationin a
C#project.
The problem is that i cant make it work. it gives me always the same error.
The function returns a win32 error telling me that the parameter is not valid
The problem is that i dont know which parameter is the function referring to.
I have tried everything to make it work but i cant
I create the handle to the serial port correctly in the overlapped mode. I
call some functions to change different parameters of the serial port and
everything goes fine, none of the imported functions gives me any error.
But when i finally call DeviceiOcONTROL:

IntPtr control = Marshal.AllocHGlobal(3);
IntPtr stats = Marshal.AllocHGlobal(24);
IntPtr BaudRate = Marshal.AllocHGlobal(4);
if (hFile != IntPtr.Zero)
{
if (false != DeviceIoControl(hFile,
IOCTL_SERIAL_GET_LINE_CONTROL, IntPtr.Zero, 0, control, 3, out returned,
IntPtr.Zero))
{
SERIAL_LINE_CONTROL c =
(SERIAL_LINE_CONTROL)Marshal.PtrToStructure(control,
typeof(SERIAL_LINE_CONTROL));
arg.DataBits = c.WordLenght;
arg.bitsStop = c.StopBits;
arg.paridad = c.Parity;
}
else
{
MessageBox.Show(new
System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error()).Message);
arg.DataBits = -1;
arg.bitsStop = -1;
arg.paridad = -1;
}

i always enter the else clause and it gives me the "inbcorrect parameter"
error.
¿Somebody knows which parameter could be referring to?
Re: Problem calling DeviceIoControl bullshark NO[at]SPAM gmail.com
7/27/2006 1:15:36 PM

[quoted text, click to view]

I don't have reference handy. Are you sure about the size of the
SERIAL_LINE_CONTROL structure? It seems kind of odd to use just three
bytes.

Could you post your declarations for SERIAL_LINE_CONTROL and
DeviceIoControl?

bullshark
Re: Problem calling DeviceIoControl eduwushu
7/28/2006 4:01:02 AM
Hi I solved the problem. The problem was the declaration i made of
IOCTL_SERIAL_GET_LINE_CONTROL. the C# function i wrote to emulate the
CTL_CODE macro to construct the IOCTL's codes was wrong. So i was passing to
DeviceIoControl a 'guess-what' control code so it was probably expecting as
input or output buffer something different from i was actually passing.
And yes the size of the structures are all right. I put that in that way
because i was desperate and i was triyng all kind of thing but at first i had
something like this:

IntPtr
control=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SERIAL_LINE_CONTROL)); or
something like that

Many Thanks anyway for the reply , see you!

[quoted text, click to view]
Re: Problem calling DeviceIoControl bullshark NO[at]SPAM gmail.com
7/28/2006 5:37:35 AM

[quoted text, click to view]

Glad to hear it. It's always something simple isn't it?

I would still like to see your declarations for SERIAL_LINE_CONTROL
structure. It might help me with a problem I have.

thanks
AddThis Social Bookmark Button