473,386 Members | 1,819 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,386 software developers and data experts.

SetCommState fail to work in c#! Help!

Hi there,

SetCommState fail to work in C#, i keep gettting the error of 87, invalid
parameter.

I use this GetLastError to get the value 87.

There is a difference between GetLastError and GetLastWin32Error

Marshal.GetLastWin32Error - returns 0 (means fail to open)

[DllImport("kernel32.dll")]
internal static extern uint GetLastError(); - returns 87 (invalid parameter)

But i did the same thing for SetCommState

[DllImport("kernel32.dll")]
internal static extern bool SetCommState(IntPtr hFile, [In] ref Dcb lpDcb);

Wonder why it fails? GetCommState and others works pretty fine!

I had updated the Dcb structures values when i debug it, and past into the
functions, and it dies off! Why?

C programming works fine not C#.

Any help? Thanks.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
2 6161
I use this GetLastError to get the value 87.
You can't call GetLastError from managed code, so the error code it
returns is irrelevant.

Marshal.GetLastWin32Error is the right way to do it. You must also set
SetLastError=true in the DllImport attribute for the function.

Wonder why it fails?


That's impossible to say without seeing your code.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Dear Mattias,

Yeah, i had modified like what you had suggested. I still receive 87, no
difference with GetLastWin32Error.

But i am wondering:

Can't work:

if (!ClassA.SetCommState(instStruct.handle, ref instStruct.port[0].dcb))
{
Debug.WriteLine(Marshal.GetLastWin32Error());

}

Can work:

if (ClassA.SetCommState(instStruct.handle, ref instStruct.port[0].dcb))
{
Debug.WriteLine(Marshal.GetLastWin32Error());

}

But if most of my codes, i use ! in front of the function, if return false,
will throw an exception. But why the 2nd one without the ! works fine? No
more error 87.

Any idea?

Thanks.

"Mattias Sjögren" wrote:
I use this GetLastError to get the value 87.


You can't call GetLastError from managed code, so the error code it
returns is irrelevant.

Marshal.GetLastWin32Error is the right way to do it. You must also set
SetLastError=true in the DllImport attribute for the function.

Wonder why it fails?


That's impossible to say without seeing your code.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3

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

Similar topics

8
by: Boris | last post by:
Hi folks! Look at the code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
18
by: Dilip | last post by:
This thing is driving me nuts. why would a simple output file stream like this: ofstream ofs; ofs.open("c:\temp\somefile.txt") set the fail bit? calling ofs.fail() right after the open...
2
by: kudruu | last post by:
Hello, I am having a problem configuring a certain port on my computer. I want to loop through a list of active ports and listen to the one that is giving me the data packets I need. Right now...
34
by: niranjan.singh | last post by:
This is regarding to test an SDK memory stuff. In what situation malloc gets fail. any comment/reply pls.... regards
1
by: asadjahangir | last post by:
I need ur help regarding Serial communication in Win32. The problem, i m having is quite strange. It is related to fParity member of DCB structure After setting the fparity=True with...
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
6
by: bbmmzz | last post by:
Here is my program: int main() { int ival; while(cin >ival, !cin.eof()) { if(cin.bad()) return 0; if(cin.fail())
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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.