【问题标题】:working with pointers from an ocx control in c# csharp在 c# csharp 中使用来自 ocx 控件的指针
【发布时间】:2011-06-26 18:03:49
【问题描述】:

编辑: 我正在使用 C# 中的 ocx 控件。此控件具有包含数据缓冲区长度和指向该缓冲区的指针的属性。如何在 C# 中访问/获取/使用该数据。我正在使用 Visual Studio 2008。

我在 C# 中使用 .ocx 控件。该 .ocx 有一个属性,其中包含数据缓冲区的 len 和指向数据缓冲区的指针。我如何在 c# 中使用该指针获取数据?我使用 VS C# 2008

【问题讨论】:

  • 一切都与编组有关。您能否提供您正在调用的函数的 C/Pascal 定义。由此我将为您提供正确的 C# 函数定义
  • .ocx 有函数:Public Function WriteData(ByVal devIndex As Long, ByVal lpOutData As Long, ByVal cntData As Long) As Long。我怎么能调用这个方法? ocx 也有:事件公共事件 DataArrival(ByVal devIndex As Long,ByVal lpDataBufer As Long,ByVal lenDataBufer As Long)。在c#中这个事件:private void AX_DataArrival(object sender, AxUSBBridge.__FT245R_DataArrivalEvent e),e变量有devIndex、lenDataBufer、lpDataBufer属性。 lenDataBufer 是缓冲区的大小,lpDataBufer 是指向数据数组的指针。如何获取缓冲区?

标签: c# .net pointers ocx


【解决方案1】:

您没有提供确切的详细信息,因此这是根据您的信息进行的猜测。你可以找到一个例子here。我将引用(并简化)相关部分:

// 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);

【讨论】:

  • 很好的推理。但是我们不知道缓冲区是输入还是输出。
  • 如果我使用 ocx,我如何使用 DllImport?我将它添加到我的工具箱并放入表单中,所有导入都是自动进行的?对于到达事件错误:'System.Runtime.InteropServices.Marshal.Copy(int[], int, System.IntPtr, int)' 的最佳重载方法匹配有一些无效参数。你有一篇关于 c# 中的 Marshaling 和 IntPtr 的好文章吗?
  • 你从哪里得到“公共功能......”部分?我没用过ocx控件,所以不知道在你的项目中是怎么显示的。至于 Marshal.Copy(),我假设你会使用这个重载:msdn.microsoft.com/en-us/library/ms146631.aspx
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2011-08-30
  • 2013-12-06
  • 2011-03-14
  • 1970-01-01
  • 2018-06-12
  • 2013-10-23
  • 2015-04-07
  • 1970-01-01
相关资源
最近更新 更多