0

Edit: I'm working with an ocx control in C#. This contrl has properties which contain the length of a data buffer and a pointer to that buffer. How can access/get/use that data with possibly a point in C#. I'm using Visual Studio 2008.

i work with .ocx control in c#. That .ocx have a properties which contains len of data buffer and pointer to data buffer. How i can get data, using that pointer in c#? I use VS C# 2008

gideon
  • 19,329
  • 11
  • 72
  • 113
Xaver
  • 991
  • 2
  • 19
  • 37
  • It is all about Marshalling. Could you please provide the C/Pascal definition of the function that you are calling. From that I will provide you with the correct C# function definition – Richard Schneider Feb 14 '11 at 08:34
  • .ocx have function: Public Function WriteData(ByVal devIndex As Long, ByVal lpOutData As Long, ByVal cntData As Long) As Long. How can i call this method? Also ocx have: event Public Event DataArrival(ByVal devIndex As Long, ByVal lpDataBufer As Long, ByVal lenDataBufer As Long). In c# this event: private void AX_DataArrival(object sender, AxUSBBridge.__FT245R_DataArrivalEvent e), the e variable have devIndex, lenDataBufer, lpDataBufer properties. lenDataBufer is size of buffer, lpDataBufer is pointer to data array. How to get buffer? – Xaver Feb 14 '11 at 09:29

2 Answers2

3

You didn't give exact details, so this is a guess based on your info. You can find an example here. I'll quote (and simplify) the relevant parts:

// C/C++
int ncWrite(unsigned long DataSize, void* DataPtr)

// C#
[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint DataSize, byte[] DataPtr);

byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int status = ncWrite(Marshal.SizeOf(DataWrite), DataWrite);

EDIT: With your info:

// .ocx
Public Function WriteData(ByVal devIndex As Long, ByVal lpOutData As Long, ByVal cntData As Long) As Long

// C#
[DllImport("TheOcxControl.dll")]
static extern int WriteData(int index, byte[] outputData, int outputDataLength);

byte[] DataToWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int status = WriteData(index, DataToWrite, Marshal.SizeOf(DataToWrite));

As for the arrival event:

// the e variable have devIndex, lenDataBufer, lpDataBufer properties.
// lenDataBufer is size of buffer, lpDataBufer is pointer to data array.
byte[] destination;
Marshal.Copy(lpDataBufer, destination, 0, lenDataBufer);
Daniel Rose
  • 17,233
  • 9
  • 65
  • 88
  • Good inference. But we do not know if the buffer is Input or Output. – Richard Schneider Feb 14 '11 at 08:34
  • how can i Use DllImport if i use ocx? i add that on my toolbox and drop on form, and all import took place automatically? for arrival event error: The best overloaded method match for 'System.Runtime.InteropServices.Marshal.Copy(int[], int, System.IntPtr, int)' has some invalid arguments. Have you a good article about Marshaling and IntPtr in c#? – Xaver Feb 14 '11 at 12:10
  • Where did you get the "public function..." part? I haven't used ocx controls, so I don't know how it shows up in your project. As for the Marshal.Copy(), I assumed you would use this overload: http://msdn.microsoft.com/en-us/library/ms146631.aspx – Daniel Rose Feb 14 '11 at 14:16
0

check this one out. How can I pass a pointer to an array using p/invoke in C#?

Community
  • 1
  • 1
Mubashir Khan
  • 1,464
  • 1
  • 12
  • 17