How to make a direct entry into a DLL that requires a point to an unsigned FAR?

C

Chucker

Although the particular DLL is for the National Instruments
flexmotion32.dll I've convinced myself that it has something to do
with the 'u16 FAR' data type. This is the particular Dll function that
is giving me a headache:

FLEXFUNC flex_read_port_rtn (BOARD, u8 port, u16 FAR *portData);

Is there anything special I have to do to call this function since the
DLL wants a pointer to an u16 FAR?

What exactly does FAR mean and what are the implications when calling
from C#? Can I ignore FAR and just use a reference to an ushort
(Uint16)? I have had success calling flex_set_port and changing the
state of output bits. I've also successfully used the DllImport to
communicate with the PCI-GPIB board using the National Instruments
gpib-32.dll.

Here's what I've tried so far, but the portData always comes back as
‘69' regardless of the bit is ON or OFF.

Method 1:
// Read I/O Port
[DllImport("c:/WINNT/system32/FlexMotion32.dll",
EntryPoint="flex_read_port_rtn")]
static extern int flex_read_port_rtn(byte boardID, byte port,
[MarshalAs(UnmanagedType.LPArray)] uint[] portData);

…In the CdigitalIO object I have the following method:

public int readBitState(digInputNames index)
{
ushort[] portData = new ushort[1];
portData[0] = 0;
byte boardID = 1; //Motion card ID

//Make call to the Flexmotion32.dll function
status = flex_read_port_rtn(inputBit.boardID, inputBit.port,
portData);
}

I now run the program with all the bits on port 1 off except for bit
0. When I run the program the port data comes back as ‘69' regardless
of whether the input is ON or OFF.

Method 2:
// Read I/O Port
[DllImport("c:/WINNT/system32/FlexMotion32.dll",
EntryPoint="flex_read_port_rtn")]
static extern int flex_read_port_rtn(byte boardID, byte port,
ref ushort portData);

Same problem, I get portData as ‘69'.

Any suggestions would be greatly appreciation.
 
M

Mattias Sjögren

What exactly does FAR mean and what are the implications when calling
from C#? Can I ignore FAR and just use a reference to an ushort
(Uint16)?

Yes, you should be able to ignore FAR, it has no meaning in the 32-bit
flat address space.

Method 1:
// Read I/O Port
[DllImport("c:/WINNT/system32/FlexMotion32.dll",
EntryPoint="flex_read_port_rtn")]
static extern int flex_read_port_rtn(byte boardID, byte port,
[MarshalAs(UnmanagedType.LPArray)] uint[] portData);

I assume you really have the last parameter as ushort[], at least
that's what you're passing in at the call site.

Method 2:
// Read I/O Port
[DllImport("c:/WINNT/system32/FlexMotion32.dll",
EntryPoint="flex_read_port_rtn")]
static extern int flex_read_port_rtn(byte boardID, byte port,
ref ushort portData);

This one should perform somewhat better if you're only going to pass
in a single ushort to the portData parameter.

Same problem, I get portData as ‘69'.

I don't know why you get back unexpected results, the code looks
correct as far as I can tell.



Mattias
 

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