CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    333

    How To Pass A CStringArray From Application To DLL?

    Hi,

    I passed the CStringArray to dll & wrote the String in a file.

    The error File: array_s.cpp, Line No: 420 was occured when i close the .exe file.

    For your reference:

    Code:
    Function Declaration in DLL :
    
    DECLDIR void SetParName(CStringArray& ParName); 
    
    Function Definition in DLL : 
    
    DECLDIR void SetParName(CStringArray& ParName)
    
    { 
    
    ParName.Add("X1");
    
    ParName.Add("X2"); 
    
    }

    Calling Function in Application :

    Code:
    CStringArray ParName;
    
    if(_SetParNameFunc)
    
    { 
    
    _SetParNameFunc(ParName); //Function Called Using CStringArray
    
    char a[10];
    
    int size=ParName.GetSize();
    
    itoa(size,a,10);
    
    ::AfxMessageBox(a);
    
    CStdioFile file;
    
    CString strTemp;
    
    file.Open("ParName.txt",CFile::modeCreate|CFile::modeWrite); 
    
    for(int i=0;i<size;i++)
    
    {
    
    strTemp=ParName.GetAt(i);
    
    file.WriteString(strTemp+"\n");
    
    } 
    
    file.Close(); 
    
    }
    ERROR :

    File : array_s.cpp

    Line : 420

    array_s.cpp
    -----------------------

    void CStringArray::AssertValid() const
    {
    CObject::AssertValid();

    if (m_pData == NULL)
    {
    ASSERT(m_nSize == 0);
    ASSERT(m_nMaxSize == 0);
    }

    else
    {
    ASSERT(m_nSize >= 0);
    ASSERT(m_nMaxSize >= 0);
    ASSERT(m_nSize <= m_nMaxSize);

    ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(CString))); //Line : 420

    }
    }
    My doubt is, CStringArray items are allocated in the DLL. So, when i release the CStringArray object in the application.

    Is it can release memory that are allocated in the DLL ?
    Regards,

    SaraswathiSrinath

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How To Pass A CStringArray From Application To DLL?

    Quote Originally Posted by saraswathisrinath View Post
    My doubt is, CStringArray items are allocated in the DLL. So, when i release the CStringArray object in the application.

    Is it can release memory that are allocated in the DLL ?
    You shouldn't pass objects such as CStringArray across application boundaries. There are two main reasons:

    1) Can you guarantee that the object has exactly the same binary layout in the application as the DLL? What if you tried to pass a CStringArray from a VS 2008 application to a DLL created with VS 2010? Or if the CStringArray in the application was compiled with different options than the one in the DLL?

    2) Can you guarantee that the object uses the same memory heap when it needs to allocate/deallocate memory for itself? That is exactly what the error is telling you -- you are using a CStringArray created that uses one heap, and then you're taking the same CStringArray and attempting to use the other heap on it.

    These items not only pertain to CStringArray, any object, whether it is MFC, ATL, STL, or your own home-made object shouldn't be passed between application boundaries. Even simple C structs such as FILE shouldn't be passed across app boundaries because of 1) above.

    The only way to successfully pass a CStringArray between an app and a DLL is to

    1) Guarantee that the application and DLL were created with the same version of the compiler (same version, same service pack, etc.) as well as compiled with the same compiler options, same packing options, etc.

    2) Guarantee that the application and DLL use the same heap by using the same Multithreaded DLL runtime.

    Given these restriction, you may be better off redesigning your code so that you're not passing the CStringArray -- instead you create exported functions that manipulate the CStringArray inside the DLL.

    Anyway, C++ classes are designed to be used internally inside each application (I am talking about the end-programmer, and not necessarily a component library designer). Passing these objects between application and DLL as if you're passing them between functions is not what C++ objects are really designed for.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 11th, 2013 at 05:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured