/*
C# Gurus,
I am trying to use interop marshalling to call SetCommTimeouts win32 API.
But I keep getting the "Object reference not set to an instance of an
object" error.
Can anybody help me figure out why ?
TIA.
-- shanko
Code and transcript follows:
*/
//--------------------------------------------------------------------------
-----------
using System;
using System.Runtime.InteropServices;
//
http://msdn.microsoft.com/library/de...uresunions.asp
[StructLayout(LayoutKind.Sequential)]
public struct CommTimeOuts {
public long ReadIntervalTimeout ; // Maximum time between read
chars.
public long ReadTotalTimeoutMultiplier ; // Multiplier of characters.
public long ReadTotalTimeoutConstant ; // Constant in milliseconds.
public long WriteTotalTimeoutMultiplier; // Multiplier of characters.
public long WriteTotalTimeoutConstant ; // Constant in milliseconds.
} ;
class Win32API{
//
http://msdn.microsoft.com/library/de...createfile.asp
[DllImport("kernel32.dll")]
public static extern long CreateFileA(
string FileName,
System.UInt32 dwDesiredAccess,
System.UInt32 dwShareMode,
System.IntPtr lpSecurityAttributes,
System.UInt32 dwCreationDistribution,
System.UInt32 dwFlagsAndAttributes,
System.IntPtr hTemplateFile);
//
http://msdn.microsoft.com/library/de...losehandle.asp
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(System.Int32 hndl);
//
http://msdn.microsoft.com/library/de...mmtimeouts.asp
[DllImport("kernel32.dll")]
public static extern bool GetCommTimeouts(long port, ref CommTimeOuts
timeOut);
//
http://msdn.microsoft.com/library/de...mmtimeouts.asp
[DllImport("kernel32.dll")]
public static extern bool SetCommTimeouts(long port, ref CommTimeOuts
timeOut);
}
public class MyApp{
public static int Main(string[] args)
{
long lHndl=0;
bool ret;
CommTimeOuts timeOut;
lHndl = Win32API.CreateFileA("\\\\.\\LEGOTOWER1",
(UInt32)(0x80000000|0x40000000),
0,(System.IntPtr)0,3,0,(System.IntPtr)0);
Console.WriteLine("CreateFileA = {0}",lHndl);
timeOut = new CommTimeOuts();
ret = Win32API.GetCommTimeouts(lHndl, ref timeOut);
Console.WriteLine("GetCommTimeouts {0}",ret,-1L);
Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n",
timeOut.ReadIntervalTimeout,
timeOut.ReadTotalTimeoutMultiplier,
timeOut.ReadTotalTimeoutConstant,
timeOut.WriteTotalTimeoutMultiplier,
timeOut.WriteTotalTimeoutConstant);
timeOut.ReadIntervalTimeout = 100 ;
timeOut.ReadTotalTimeoutMultiplier = 0 ;
timeOut.ReadTotalTimeoutConstant = 100 ;
timeOut.WriteTotalTimeoutMultiplier = 0 ;
timeOut.WriteTotalTimeoutConstant = 0 ;
Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n",
timeOut.ReadIntervalTimeout,
timeOut.ReadTotalTimeoutMultiplier,
timeOut.ReadTotalTimeoutConstant,
timeOut.WriteTotalTimeoutMultiplier,
timeOut.WriteTotalTimeoutConstant);
try {
Win32API.SetCommTimeouts(lHndl, ref timeOut) ;
}
catch (Exception e) {
Console.WriteLine("Error: "+e.Message);
}
ret = Win32API.CloseHandle((System.Int32)lHndl);
Console.WriteLine("CloseHandle {0}",ret);
return args.Length;
}
}
/*
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\cs>csc comm.cs
Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
C:\cs>comm
CreateFileA = 12884901887
GetCommTimeouts False
0
0
0
0
0
100
0
100
0
0
Error: Object reference not set to an instance of an object.
CloseHandle False
C:\cs>
*/