我試圖從C++發送一個圖像到C#託管的C++的互操作(編組)。 image->getStream()
從字符串中返回一個const char*
。我的Marshal::Copy
函數有例外。從const char *複製到字節數組C++/c#interop Marshal ::複製
'System.AccessViolationException'
類型的未處理的異常在mscorlib.dll發生其他信息:試圖讀取或寫入保護內存。這通常表明其他內存已損壞。
我在做從const char*
複製到字節數組的正確的東西嗎?我的dll是用VS2010中的ASCII字符集編譯的。
array<System::Byte>^ OsgViewer::getLastImage()
{
array<Byte>^ byteArray;
m_ImageQueue->lock();
int index = m_ImageQueue->getCurrentImageIndex();
std::shared_ptr<Image> image = m_ImageQueue->getImage(static_cast<unsigned int>(index));
if(image && image->isValid() == true)
{
int wLen = image->getStreamSize();
char* wStream = const_cast<char*>(image->getStream());
byteArray = gcnew array<Byte>(wLen);
// convert native pointer to System::IntPtr with C-Style cast
Marshal::Copy((IntPtr)wStream ,byteArray , 0, wLen);
}
m_ImageQueue->unlock();
return byteArray;
}
圖像由C++類
class ADAPTER Image
{
public :
Image();
~Image();
const char* getStream() const;
int getStreamSize();
bool setStringStream(std::ostringstream* iStringStream);
void setIsValid(bool isValid){ m_isValid = isValid;}
bool isValid() const{return m_isValid;}
std::ostringstream* getOStringStream() {return m_StringStream;}
private:
std::ostringstream* m_StringStream;
bool m_isValid;
};
什麼類型是圖像? System.Drawing.Image,它沒有getStream()或getStreamSize()。我認爲你的問題的根源在於流不是字節數組。 – shf301
看到上面的代碼,我發佈了圖像代碼 – BuzBuza
我的猜測是'wStream'不是'wLen'字節長度。真的不能告訴你。如果你添加'char temp = wStream [wLen-1]'你會得到一個訪問衝突。 – shf301