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

2
  • 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 Commented Feb 14, 2011 at 8: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? Commented Feb 14, 2011 at 9:29

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Good inference. But we do not know if the buffer is Input or Output.
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#?
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: msdn.microsoft.com/en-us/library/ms146631.aspx
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.