Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Transferring arrays from C++ library to C# application

Transferring arrays from C++ library to C# application

Scheduled Pinned Locked Moved C#
csharpc++data-structureshelpannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yooval
    wrote on last edited by
    #1

    Hi, I'm having trouble in transferring arrays of short integers from C++ library to C# application. I got the sources of a library, written in C++, that defines interface in IDL file. The original methods transfer "basic" objects, such as BOOL and BSTR. I need to add a new method to the interface for transferring two arrays of short integers. For that purpose, I create two safe arrays and populate them with the numbers. However, when I try to invoke the C# method I get TypeMismatch error code (when I pass only the string objects, it works OK). The C++ code includes the following definition in IDL file: library CodecLib { ... dispinterface _ILiveEvents { properties: methods: ... [id(4), helpstring("method DataReady")] HRESULT DataReady([in] BSTR strForward, [in] SAFEARRAY(short) FwdBuffer, [in] BSTR strReturn, [in] SAFEARRAY(short) RetBuffer); }; ... }; The following C# code implements the interface defined in the IDL file: private void DataReady(string sForward, Array ForwardData, string sRetrun, Array ReturnData) { ... } The following C++ code prepares the data in the safe arrays: void CLive::DataReady(short *pFwdBuf, short *pRetBuf, int nBufferSize) { long lIndex; SAFEARRAYBOUND ArrayBound[2]; ArrayBound[0].cElements = nBufferSize; ArrayBound[0].lLbound = 0; ArrayBound[1].cElements = nBufferSize; ArrayBound[1].lLbound = 0; m_pFwdBuffer = SafeArrayCreate(VT_I2, 1, &ArrayBound[0]); m_pRetBuffer = SafeArrayCreate(VT_I2, 1, &ArrayBound[1]); // Set data in safe arrays for (lIndex=0; lIndex<nBufferSize; lIndex++) { SafeArrayPutElement(m_pFwdBuffer, &lIndex, &pFwdBuf[lIndex]); SafeArrayPutElement(m_pRetBuffer, &lIndex, &pRetBuf[lIndex]); } // Send data to clients Fire_DataReady(m_strForward, m_pFwdBuffer, m_strReturn, m_pRetBuffer); // Release safe arrays SafeArrayDestroy(m_pFwdBuffer); SafeArrayDestroy(m_pRetBuffer); m_pFwdBuffer = NULL; m_pRetBuffer = NULL; } The following C++ code distributes the data to the clients. However, calling Invoke() returns TypeMismatch error, with uArgErr equals to 3: HRESULT Fire_DataReady(BSTR strForward, SAFEARRAY *pFwdBuf, BSTR strReturn, SAFEARRAY *pRetBuf) { HRESULT hr = S_OK; T * pThis = static_cast<T *>(this); int cConnections = m_vec.GetSize(); for (int iConnection = 0; iConnection < cConnections; iConnection++) { pThis->Lock(); CComPtr<IUnknown> punkConnection = m_vec.Get

    A 1 Reply Last reply
    0
    • Y Yooval

      Hi, I'm having trouble in transferring arrays of short integers from C++ library to C# application. I got the sources of a library, written in C++, that defines interface in IDL file. The original methods transfer "basic" objects, such as BOOL and BSTR. I need to add a new method to the interface for transferring two arrays of short integers. For that purpose, I create two safe arrays and populate them with the numbers. However, when I try to invoke the C# method I get TypeMismatch error code (when I pass only the string objects, it works OK). The C++ code includes the following definition in IDL file: library CodecLib { ... dispinterface _ILiveEvents { properties: methods: ... [id(4), helpstring("method DataReady")] HRESULT DataReady([in] BSTR strForward, [in] SAFEARRAY(short) FwdBuffer, [in] BSTR strReturn, [in] SAFEARRAY(short) RetBuffer); }; ... }; The following C# code implements the interface defined in the IDL file: private void DataReady(string sForward, Array ForwardData, string sRetrun, Array ReturnData) { ... } The following C++ code prepares the data in the safe arrays: void CLive::DataReady(short *pFwdBuf, short *pRetBuf, int nBufferSize) { long lIndex; SAFEARRAYBOUND ArrayBound[2]; ArrayBound[0].cElements = nBufferSize; ArrayBound[0].lLbound = 0; ArrayBound[1].cElements = nBufferSize; ArrayBound[1].lLbound = 0; m_pFwdBuffer = SafeArrayCreate(VT_I2, 1, &ArrayBound[0]); m_pRetBuffer = SafeArrayCreate(VT_I2, 1, &ArrayBound[1]); // Set data in safe arrays for (lIndex=0; lIndex<nBufferSize; lIndex++) { SafeArrayPutElement(m_pFwdBuffer, &lIndex, &pFwdBuf[lIndex]); SafeArrayPutElement(m_pRetBuffer, &lIndex, &pRetBuf[lIndex]); } // Send data to clients Fire_DataReady(m_strForward, m_pFwdBuffer, m_strReturn, m_pRetBuffer); // Release safe arrays SafeArrayDestroy(m_pFwdBuffer); SafeArrayDestroy(m_pRetBuffer); m_pFwdBuffer = NULL; m_pRetBuffer = NULL; } The following C++ code distributes the data to the clients. However, calling Invoke() returns TypeMismatch error, with uArgErr equals to 3: HRESULT Fire_DataReady(BSTR strForward, SAFEARRAY *pFwdBuf, BSTR strReturn, SAFEARRAY *pRetBuf) { HRESULT hr = S_OK; T * pThis = static_cast<T *>(this); int cConnections = m_vec.GetSize(); for (int iConnection = 0; iConnection < cConnections; iConnection++) { pThis->Lock(); CComPtr<IUnknown> punkConnection = m_vec.Get

      A Offline
      A Offline
      ashukasama
      wrote on last edited by
      #2

      http://msdn2.microsoft.com/en-us/library/ek1fb3c6.aspx[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups