Passing Managed ByteArray to Native C++ DLL
Anyone could help regarding passing ByteArray containing image data from axis camera to Native C++ DLL, Both compile OK, but DLL does not write ByteArray data to file?, just wondering if this is the correct way to pass ByteArray's to C++ DLL's here what we got...
[VC++]
typedef void(*img_process)(IntPtr pArray, int nSize);
img_process _img_process;
HINSTANCE hinstDLL = LoadLibrary((LPCWSTR)L"image_process.dll");
// Get current image of axAxisMediaControl1
int theFormat = 0;
Object^ theBuffer = gcnew Object();
__int32 theBufferSize;
this->axAxisMediaControl1->GetCurrentImage(theFormat, theBuffer, theBufferSize);
// Convert picture object to byte array
array<System::Byte> ^_ByteArray = ObjectToByteArray(theBuffer);
// Initialize unmanged memory to hold the array.
int size = Marshal::SizeOf(_ByteArray[0]) * _ByteArray->Length;
IntPtr pnt = Marshal::AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal::Copy(_ByteArray, 0, pnt, _ByteArray->Length);
}
finally
{
_ img_process = (img_process)GetProcAddress(hinstDLL,"img_process");
_img_process(pnt, _ByteArray->Length);
Marshal::FreeHGlobal(pnt);
}
[C++ DLL]
void DLL_EXPORT img_process(BYTE * pArray, int nSize)
{
// Save ByteArray to file
fstream imgData;
imgData.open("imagedata.jpg", ios::out | ios::app | ios::binary);
for(int I = 0; i < nSize; i++){
imgData.write(reinterpret_cast<char*>(&pArray[i]),1);
}
imgData.close();
}