注: COM 返回的字符串类型 是Unicode 宽字符 wchar(2字节), 而我们用的是 Ascii 字符char_t(窄字符,一字节), 需要进行转化。 这在使用COM 字符串的时候 需要注意。
- // Returns a float array for a managed float array. // Caller must free the memory using the delete[] operator!
- inline float* GetUnmanagedArray(array<float>^ managedArray)
- {
- if (nullptr == managedArray)
- return NULL;
- if (managedArray->Length == 0)
- return NULL;
- pin_ptr<float> p = &managedArray[0];
- float* pSource = p;
- float* pDestination = new float[managedArray->Length];
- ::memcpy( pDestination, pSource, managedArray->Length * sizeof(float) );
- return pDestination;
- }
- // Returns a int array for a managed int array.
- // Caller must free the memory using the delete[] operator!
- inline int* GetUnmanagedArray(array<int>^ managedArray)
- {
- if (nullptr == managedArray)
- return NULL;
- if (managedArray->Length == 0)
- return NULL;
- pin_ptr<int> p = &managedArray[0];
- int* pSource = p;
- int* pDestination = new int[managedArray->Length];
- ::memcpy( pDestination, pSource, managedArray->Length * sizeof(int) );
- return pDestination;
- }
- inline CString FromManaged(System::String^ other)
- {
- System::IntPtr pointer = System::Runtime::InteropServices::Marshal::StringToHGlobalUni(other);
- const wchar_t* internalBuffer = (const wchar_t*)pointer.ToPointer();
- CString cstrResult( internalBuffer );
- System::Runtime::InteropServices::Marshal::FreeHGlobal( pointer );
- return cstrResult;
- }
- inline System::String^ FromUnmanaged(const CString& other)
- {
- return gcnew System::String(other);
- }
- // This function use to convert managed string to wchar unmanaged string.
- // 将托管String 转换成 非托管 Unicode String
- wchar_t * ManagedStringToUnmanagedStringA(String^ strIn)
- {
- IntPtr ip = Marshal::StringToHGlobalUni(strIn);
- const wchar_t* pTemp = static_cast<const wchar_t*>(ip.ToPointer());
- wchar_t *pOut = new wchar_t[wcslen(pTemp) + 1];
- wcscpy_s(pOut, wcslen(pTemp), pTemp);
- Marshal::FreeHGlobal(ip);
- return pOut;
- }
- // This function use to convert managed Bitmap to native byte array.
- // 通过 MemoryStream 来取道 Bitmap 的所有2进制数据,包括BitmapHeadInfo 等信息。
- BYTE* BmpToBytes_MemoryStream (Bitmap^ bmp)
- {
- System::IO::MemoryStream^ ms = gcnew System::IO::MemoryStream();
- // Save to memory using the Jpeg format
- bmp->Save (ms, System::Drawing::Imaging::ImageFormat::Bmp);
- // read to end
- cli::array<BYTE>^ bmpBytes = ms->GetBuffer();
- pin_ptr<BYTE> p = &bmpBytes[0];
- BYTE* pSource = p;
- BYTE* pDestination = new BYTE[bmpBytes->Length];
- ::memcpy( pDestination, pSource, bmpBytes->Length * sizeof(BYTE) );
- ms->Close();
- return pDestination;
- }
- // Convert BitmapData to bytes array.
- // 只是把BitmapData 转出,而不包含图片头的信息,一般Publish中 也只是需要这些。
- BYTE* BitmapDataToBytes (Bitmap^ bmp)
- {
- System::Drawing::Point point;
- System::Drawing::Rectangle rect(point, bmp->Size);
- BitmapData^ bData = bmp->LockBits(rect,
- ImageLockMode::ReadOnly,
- PixelFormat::Format24bppRgb);
- // number of bytes in the bitmap
- int byteCount = bData->Stride * bmp->Height;
- array<BYTE>^ bmpBytes = gcnew array<BYTE>(byteCount);
- // Copy the locked bytes from memory
- Marshal::Copy (bData->Scan0, bmpBytes, 0, byteCount);
- // don't forget to unlock the bitmap!!
- bmp->UnlockBits (bData);
- pin_ptr<BYTE> p = &bmpBytes[0];
- BYTE* pSource = p;
- BYTE* pDestination = new BYTE[bmpBytes->Length];
- ::memcpy( pDestination, pSource, bmpBytes->Length * sizeof(BYTE) );
- return pDestination;
- }