Hi to all. I'm trying to use hidapi.dll,if needed i'll post a sources or link to authors page.
Here's what i've have in my dll:
#define HID_API_EXPORT __declspec(dllexport)
#define HID_API_CALL
...
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
...
and this is the part in c#:
[DllImport("hidapi.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern unsafe int hid_write(IntPtr device, StringBuilder data, uint length);
[DllImport("hidapi.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int hid_get_serial_number_string(IntPtr dev, StringBuilder stringt, uint maxlen);
...
Byte[] buf = new Byte[256];
buf[0] = 0x3F;
buf[1] = 7;
buf[2] = Convert.ToByte('L');
buf[3] = Convert.ToByte('E');
buf[4] = Convert.ToByte('D');
buf[5] = Convert.ToByte(' ');
buf[6] = Convert.ToByte('O');
buf[7] = Convert.ToByte('N');
buf[8] = Convert.ToByte('!');
...
// this is the first attempt to send data,with changes in dllimport function to accept pointer,not working
IntPtr ptrHandle = Marshal.AllocHGlobal(Marshal.SizeOf(handle));
IntPtr intPtr = Marshal.AllocHGlobal(9);
Marshal.Copy(buf, 0, intPtr, 9);
StringBuilder ms = new StringBuilder(255);
// this returns serial as expected, pointer to handler is ok
hid_get_serial_number_string(ptrHandle, ms, 255);
// this is the second attempt to send data using stringbuilder, with changes in dllimport, not working
// and third attempt to send data using string instead of stringbuilder, not working.
string send_string = System.Text.Encoding.UTF8.GetString(buf, 0, 9);
StringBuilder outBuf = new StringBuilder(send_string.Length);
outBuf.Append(send_string);
//this returns -1, error on writeFile as i understand
res = hid_write(ptrHandle, outBuf/*intPtr*/ /*send_string*/, 9);
Marshal.FreeHGlobal(intPtr);
as i mentioned in code, it seems i can correctly connect to the device, get the serial, but i can't
write to it. there is no problems on the device side, because a've tested hidapi from primitive cpp app and works great with sending very same buf (0x3F,7,LED ON!), so the problems is in c# marshalling related code i assume. sorry for the messy code, but it's better to post all variants i've tryed. what am i doing wrong and what is the correct code to pass string to const char*? on the other side is MSP430 MCU configured as usb datapipe.