2011-02-14 134 views
0

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

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

+0

阅读上不安全的C#代码。 – CodingBarfield 2011-02-14 07:11:19

+0

这是关于编组。你能否提供你正在调用的函数的C/Pascal定义。从这里我将为您提供正确的C#函数定义 – 2011-02-14 08:34:52

回答

3

你没有给确切的细节,所以这是根据您的信息猜测。你可以找到一个例子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);