interop marshalling problem for SetCommTimeouts

S

Shashank Date

/*
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/d...ml/cpconmarshalingclassesstructuresunions.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/default.asp?url=/library/en-us/fileio/base/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/default.asp?url=/library/en-us/sysinfo/base/closehandle.asp
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(System.Int32 hndl);

//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/getcommtimeouts.asp
[DllImport("kernel32.dll")]
public static extern bool GetCommTimeouts(long port, ref CommTimeOuts
timeOut);

//
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/setcommtimeouts.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>
*/
 

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