473,385 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

interop marshalling problem for SetCommTimeouts

/*
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>
*/

Nov 15 '05 #1
0 2981

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: spammy | last post by:
Hi all, Im attempting to use a COM class in C# via .NET interop. The class has two modes - synhrounous and asynchronous. The mode is determined by the use (or not) of an optional out parameter:...
3
by: Charles Law | last post by:
I have defined LVITEM as follows: <StructLayout(LayoutKind.Sequential)> Private Structure LVITEM Dim mask As Int16 Dim iItem As Int16 Dim iSubItem As Int16 Dim state As Int16 Dim stateMask As...
0
by: Chad Myers | last post by:
*** I posted this in the interop newsgroup, but I'm not sure if it'd get answered there. I didn't want to cross post and possible bug everyone, so sorry if I made a faux-paus. *** I'm writing...
1
by: ivang | last post by:
Hello, All! Please help me to marshal following win32 structure: typedef struct _STARTUPINFOW { DWORD cb; LPWSTR lpReserved;
2
by: Kirk Marple | last post by:
i'm attempting to interop from C# to a COM DLL, and have found some trouble with a non-standard interface (at bottom of post - followed by my C# version). typically COM methods return an HRESULT...
2
by: Ebrahim | last post by:
Hello NG I’m developing software for my hardware which is connected via USB to my computer, I already have written the necessary firmware and a mini driver (by Windws2000 DDK) which both works ...
12
by: Lloyd Dupont | last post by:
I have an application which use has a DLL with 100+ (auto-generated) Managed C++ wrapper around some native API. Compare to a purely version my application has some performance issue and I just...
2
by: Fredo | last post by:
First of all, I apologize for cross-posting. I posted this on the framework.interop group as well, but haven't received a response yet, so I thought I'd try here... I'm trying to write an app...
6
by: Fredo | last post by:
I've got a C method I'm trying to call and I seem to be having problems with a reference parameter. The C method is: void CalculateValues(int nInputs, float* pInputs, int nOutputs, float*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.