带有指针和C#的C ++代码 [英] C++ code with pointers and C#
问题描述
大家好!
我正在使用第三方ActiveX组件从IP摄像机获取图像.
因此,它们具有以下功能:
Hi guys!
I''m working with third party ActiveX component to get image from IP-camera.
So they have this function:
LONG GetCurrentImage( BYTE* pBuffer, LONG nBufferLen )
但是在我使用它的C#程序中,它看起来像这样:
but in my C# program when I''m using it, it looks like this:
supadupaXcontrol.GetCurrentImage(ref byte , int len)
他们的支持向我发送了C ++实例示例如何使用此功能:
Their support sent me C++ cote example how to use this function :
afx_msg void CsampleConnectDlg::OnNewImage( long nID )
{
//AfxMessageBox(_T("Event"));
srand(time(NULL));
CString csVersion = m_ocxCamera.GetVersion();
DWORD dwLen = 40960000;
BYTE *pBuffer = new BYTE[40960000] ;
m_ocxCamera.GetCurrentImage(pBuffer, dwLen);
LPBITMAPINFO lpBit = (LPBITMAPINFO)pBuffer;
HDC hDC = ::GetDC( NULL );
HBITMAP hBMP = CreateDIBitmap( hDC, &( lpBit->bmiHeader ), CBM_INIT, pBuffer + sizeof( lpBit->bmiHeader ), lpBit, DIB_RGB_COLORS );
::ReleaseDC( NULL, hDC );
CImage cImage;
cImage.Attach( hBMP );
delete [] pBuffer;
pBuffer = NULL;
cImage.Save(_T("D:\\testCurrentImage.jpg"));
}
我正在尝试使用Unsafe代码块在C#中实现此功能,但我无法使其正常工作.有人可以帮助我了解我在做什么错.
I''m trying to implement this function in C# using Unsafe code block but I cant get it work. Can someone help me to understand what I''m doing wrong.
private void button7_Click(object sender, EventArgs e)
{
unsafe
{
byte packetArray = new byte();
byte* pBuf =(byte*)&packetArray;
int len = 40960000;
retval = oMedia.GetCurrentImage( ref packetArray, len);
}
}
推荐答案
如果您要将缓冲区的地址传递给函数,并且要完整说明其长度,那么我强烈建议您至少分配要告诉您的字节数,而不是单个字节.如果要填充缓冲区,难道您不认为它实际上有一个缓冲区可以填充会更好吗?他们的示例确实...
If you are passing the address of a buffer through to a function, complete with it''s length, then I would strongly recommend that you assign at least the number of bytes you are telling it you have, rather than a single byte. If it is going to fill a buffer, don''t you think it would work better if it actually had a buffer to fill? Their example does...
tlbimp与这种类型的数组参数混淆.我认为您真正想要的签名是GetCurrentImage(byte [] data,int len),带有一些编组指令(属性),但是当我想将字节数组传递给COM组件时,我自己进行了一些研究,但我无法找不到实现此目的的好方法.
如果您仍然使用不安全的设备,则可以使用诸如
之类的东西
tlbimp gets confused with this type of array parameter. I think the signature you actually want is GetCurrentImage(byte[] data, int len), with some marshalling instructions (attributes), but I investigated this a bit myself when I wanted to pass a byte array to a COM component and I couldn''t find a good way to make this work.
If you''re using unsafe anyway you may be able to get away with something like
fixed(byte* data = allocate buffer){
retval = oMedia.GetCurrentImage( ref *data, len);
}
...但是我不确定是否可以将C#允许您将ref传递给这样的指针取消引用.
我认为您将必须手动运行tlbimp,编辑生成的C#以修改签名(更改为byte []或byte *),然后将其包含在您的项目中.
... but I''m not sure if it''s possible to con C# into allowing you to pass a ref to a pointer dereference like that.
I think you will have to run tlbimp manually, edit the generated C# to modify the signature (either to byte[] or byte*), and then include that in your project.
这篇关于带有指针和C#的C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!