编辑: 我正在使用 C# 中的 ocx 控件。此控件具有包含数据缓冲区长度和指向该缓冲区的指针的属性。如何在 C# 中访问/获取/使用该数据。我正在使用 Visual Studio 2008。
我在 C# 中使用 .ocx 控件。该 .ocx 有一个属性,其中包含数据缓冲区的 len 和指向数据缓冲区的指针。我如何在 c# 中使用该指针获取数据?我使用 VS C# 2008
您没有提供确切的详细信息,因此这是根据您的信息进行的猜测。你可以在这里找到一个例子。我将引用(并简化)相关部分:
// 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);
编辑:使用您的信息:
// .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));
至于到达事件:
// 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);