none
Returning String Array from C# DLL into C++/CLI code RRS feed

  • Question

  •  

    Hi

     

      I would like to call a function within a C# dll that returns an array of strings of unknown count(i.e. Array<String>, ListArray<String> or String[]) to a caller from C++/CLI code. Can some one show me the syntax mapping, in other words how would I represent the return value in C++/CLI. I would like to know the mapping for each type, if there is one (i.e. Array<String>, ListArray<String> or String[])

    Wednesday, April 23, 2008 5:37 AM

Answers

All replies

  • array<string^> in C++ should give you a string[] on the C# side.

     

    Wednesday, April 23, 2008 7:50 AM
    Moderator
  •  

    array<string^> doen't work

    error C3149: 'cli::array<Type>' : cannot use this type here without a top-level '^'

    I had to use array<String^>^ however when I try and use the returned array from the C# function call inside of my C++/CLI code I get an access violation.

     

    C# Code

     

    public class SecurityManager
        {
            public String Initialize(String Username,IntPtr BufferSize)
            {
                Array.CreateInstance(typeof(String), 10);

                String[] T = new String[10];
                BufferSize = (IntPtr)10;
                //MessageBox.Show(Username);
                T[0] = "Author1";
                T[1] = "Author2";
                T[2] = "Author3";
                T[3] = "Author4";
                return T;
            }
        }

     

     

    C++/CLI code

     

    IntPtr pBufferSize;

    String^ pUserName = gcnew String(UserName);

    array<String^>^ T;

    T = m_pManagedWrapper->InitSecurityManager(pUserName,pBufferSize);

    MessageBox:Tongue Tiedhow(T->Length.ToString()); Access Violation

    Wednesday, April 23, 2008 4:45 PM
  •  

    mapping from String[] to array<String^>^  worked
    Wednesday, April 23, 2008 10:41 PM