0

I have a windows form application in c#, that uses a function in c++. This was done using a c++ wrapper. However, the function requires the use of pointers and c# does not allow the use of pointers with string arrays. What can be done to overcome this? I have read up on using marshal but i am not sure if this is suitable for this case and if so how should it be integrated with my code. The following is the C# code:

`

int elements = 10;
string [] sentence = new string[elements];

unsafe
{
    fixed (string* psentence = &sentence[0])
    {
        CWrap.CWrap_Class1 contCp = new CWrap.CWrap_Class1(psentence, elements);
        contCp.getsum();
    }
}

`

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Maybe you could create a C++/CX wrapper for the C++ library. – Tóth Tibor Mar 17 '17 at 08:07
  • You can't take pointers of managed classes, only value types. Are you sure you didn't want `char*` or `char**` instead of `string*`? – Luaan Mar 17 '17 at 08:11
  • 1
    `string*` is a C# class, I doubt if it has *direct* correspondence in the `C++` (`std::string` doesn't have to be implemented in the same way). You probably want either *marshalling* or `char*` representation – Dmitry Bychenko Mar 17 '17 at 08:14
  • 1
    The signature of the C method is necessary to give a response. – xanatos Mar 17 '17 at 08:16
  • @ТухбатоваЗухра: I'd still recommend you to consider marshaling approach once again since it may be easier to implement. Depending on your C++ function signature it may be as easy as declaring `[In] string[] sentenceArray` parameter in an `extern` function in C#. What is your C++ function signature? Also, does the C++ function change the passed array of strings anyhow? – Dmitry Egorov Mar 17 '17 at 08:17
  • Try this http://stackoverflow.com/a/6514531/4146066 – Hans Mar 17 '17 at 11:43

0 Answers0