|
-
August 31st, 2009, 04:56 PM
#1
[RESOLVED] Marshalling Structure to kernel32.dll
Very simple example but I cant seem to get it to work accurately, I have created a managed version of the DCB structure within the Windows.h header file:
Code:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 8, Size = 28)]
internal struct DCB
{
public DWORD DCBlength; // sizeof(DCB)
public DWORD BaudRate; // current baud rate
public DWORD fBinary; // binary mode, no EOF check
public DWORD fParity; // enable parity checking
public DWORD fOutxCtsFlow; // CTS output flow control
public DWORD fOutxDsrFlow; // DSR output flow control
public DWORD fDtrControl; // DTR flow control type
public DWORD fDsrSensitivity; // DSR sensitivity
public DWORD fTXContinueOnXoff; // XOFF continues Tx
public DWORD fOutX; // XON/XOFF out flow control
public DWORD fInX; // XON/XOFF in flow control
public DWORD fErrorChar; // enable error replacement
public DWORD fNull; // enable null stripping
public DWORD fRtsControl; // RTS flow control
public DWORD fAbortOnError; // abort on error
public DWORD fDummy2; // reserved
public WORD wReserved; // not currently used
public WORD XonLim; // transmit XON threshold
public WORD XoffLim; // transmit XOFF threshold
public BYTE ByteSize; // number of bits/byte, 4-8
public BYTE Parity; // 0-4=no,odd,even,mark,space
public BYTE StopBits; // 0,1,2 = 1, 1.5, 2
public CHAR XonChar; // Tx and Rx XON character
public CHAR XoffChar; // Tx and Rx XOFF character
public CHAR ErrorChar; // error replacement character
public CHAR EofChar; // end of input character
public CHAR EvtChar; // received event character
public WORD wReserved1; // reserved; do not use
}
The datatypes have been coerced with "using" statements to be as close to the Win32 data types Microsoft has been using when developing Win32 C++ application. ex. DWORD = System.UInt32 and CHAR = System.SByte.
Here is my function "prototype" for the function I am attempting to use:
Code:
[DllImport("kernel32.dll", SetLastError = true)]
private static unsafe extern bool GetCommState(SafeFileHandle hFile, DCB* lpDCB);
Seems ok to me... But when you call the GetCommState function passing in a handle to an open serial port, you end up getting back a DCB structure which has the first few fields properly set (BaudRate, DCBlength) but a few of the fields are really odd. For example:
fBinary = 6161
fParity = 134217728
fOutxCtsFlow = 524800
At first I thought it may have been a data misalignment problem. But I set the LayoutKind to Explicit and went through the structure and set the FieldOffset attribute for each member manually and it STILL was messed up. I cant seem to set the Parity, StopBits, and some of the other fields using the function SetCommState either since my DCB structure is messed up. Anyone got anyone ideas?
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|