J
justin.kruger
I am having a problem with exchanging handles with c# and the
"CP210xManufacturing.dll" in a device wrapper that i am working on.
I have created a wrapper class to address some of the functions in the
dll, but not all of them yet. To my knowledge they should be almost
correct, however this is my first try at exchanging handles between
legacy code and a managed enviroment. It seems that I am able to get a
handle from "CP210x_Open," however, I am not able to connect to a
device using this handle for any further informaiton like
CP210x_GetDeviceVid.
the "getNumDevice(ref numDevice)" works just fine, so the assumption is
that the onlything that could be wrong in "getDevicePid(ref siRef, ref
pid)" is then in the "ref siRef". I am unsure if i am marshaling it
correctly or if i am using the right level of indirection to get a
value, but i am getting a value in "open(0, ref siRef)" for siRef so i
would think that is close. However, the value that I get in c# is
different than the value I get in c++.
In c++ the value seems to be constant, and in c# the value seems hover
around a simular value, but is different each time without a logical
progression.
in c++ i almost always get a value 0x7b8 in my handle
/* //C++ code
HANDLE myDevice = {0};
status = CP210x_Open(0, &myDevice);
//myDevice = 0x7b8;
*/
class CSepSIChipWrap
{
private const string CP210xManu = "CP210xManufacturing.dll";
[DllImport(CP210xManu, EntryPoint = "CP210x_GetNumDevices")]
public static extern int getNumDevices(
[In, Out] ref UInt32 deviceNumb);
[DllImport(CP210xManu, EntryPoint = "CP210x_Open")]
public static extern int open(
[In] UInt32 deviceNumb,
[In, Out] ref IntPtr deviceHandle);
[DllImport(CP210xManu, EntryPoint = "CP210x_Close")]
public static extern int close(
[In, Out] ref IntPtr deviceHandle);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceProductString",
CharSet = CharSet.Ansi)]
public static extern int getDeviceProductString(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref StringBuilder Product,
[In, Out] ref byte Length,
[In] bool ConvertToASCII);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceSerialNumber",
CharSet = CharSet.Ansi)]
public static extern int getDeviceSerialNumber(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref StringBuilder Product,
[In, Out] ref byte Length,
[In, Out] bool ConvertToASCII);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceVid")]
public static extern int getDeviceVid(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref ushort Vid);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDevicePid")]
public static extern int getDevicePid(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref ushort Pid);
}
void loadSettings(){
IntPtr siRef = IntPtr.Zero;
int result = 0;
uint numDevice = 0;
result = DeviceWrappers.CSepSIChipWrap.getNumDevices(ref numDevice);
result = DeviceWrappers.CSepSIChipWrap.open(0, ref siRef);
ushort pid = 0;
result = DeviceWrappers.CSepSIChipWrap.getDevicePid(ref siRef, ref
pid);
//lbDevicePID is a .net text label on a user control
this.lbDevicePID.Text = pid.ToString();
}
"CP210xManufacturing.dll" in a device wrapper that i am working on.
I have created a wrapper class to address some of the functions in the
dll, but not all of them yet. To my knowledge they should be almost
correct, however this is my first try at exchanging handles between
legacy code and a managed enviroment. It seems that I am able to get a
handle from "CP210x_Open," however, I am not able to connect to a
device using this handle for any further informaiton like
CP210x_GetDeviceVid.
the "getNumDevice(ref numDevice)" works just fine, so the assumption is
that the onlything that could be wrong in "getDevicePid(ref siRef, ref
pid)" is then in the "ref siRef". I am unsure if i am marshaling it
correctly or if i am using the right level of indirection to get a
value, but i am getting a value in "open(0, ref siRef)" for siRef so i
would think that is close. However, the value that I get in c# is
different than the value I get in c++.
In c++ the value seems to be constant, and in c# the value seems hover
around a simular value, but is different each time without a logical
progression.
in c++ i almost always get a value 0x7b8 in my handle
/* //C++ code
HANDLE myDevice = {0};
status = CP210x_Open(0, &myDevice);
//myDevice = 0x7b8;
*/
class CSepSIChipWrap
{
private const string CP210xManu = "CP210xManufacturing.dll";
[DllImport(CP210xManu, EntryPoint = "CP210x_GetNumDevices")]
public static extern int getNumDevices(
[In, Out] ref UInt32 deviceNumb);
[DllImport(CP210xManu, EntryPoint = "CP210x_Open")]
public static extern int open(
[In] UInt32 deviceNumb,
[In, Out] ref IntPtr deviceHandle);
[DllImport(CP210xManu, EntryPoint = "CP210x_Close")]
public static extern int close(
[In, Out] ref IntPtr deviceHandle);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceProductString",
CharSet = CharSet.Ansi)]
public static extern int getDeviceProductString(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref StringBuilder Product,
[In, Out] ref byte Length,
[In] bool ConvertToASCII);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceSerialNumber",
CharSet = CharSet.Ansi)]
public static extern int getDeviceSerialNumber(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref StringBuilder Product,
[In, Out] ref byte Length,
[In, Out] bool ConvertToASCII);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDeviceVid")]
public static extern int getDeviceVid(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref ushort Vid);
[DllImport(CP210xManu, EntryPoint = "CP210x_GetDevicePid")]
public static extern int getDevicePid(
[In, Out] ref IntPtr deviceHandle,
[In, Out] ref ushort Pid);
}
void loadSettings(){
IntPtr siRef = IntPtr.Zero;
int result = 0;
uint numDevice = 0;
result = DeviceWrappers.CSepSIChipWrap.getNumDevices(ref numDevice);
result = DeviceWrappers.CSepSIChipWrap.open(0, ref siRef);
ushort pid = 0;
result = DeviceWrappers.CSepSIChipWrap.getDevicePid(ref siRef, ref
pid);
//lbDevicePID is a .net text label on a user control
this.lbDevicePID.Text = pid.ToString();
}