6

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();
    }
}

c++ function declaration: funct::funct(string* sentence_array, int sentence_arraysize)

c++ wrapper: CWrap::CWrap_Class1::CWrap_Class1(string *sentence_array, int sentence_arraysize) { pcc = new funct(sentence_array, sentence_arraysize); }

Monika
  • 2,172
  • 15
  • 24
user2990515
  • 93
  • 2
  • 6
  • 1
    OOP do not have pointer – jhyap Nov 14 '13 at 04:42
  • 3
    @jhyap: "OOP do not have pointer" --- what does it mean? – zerkms Nov 14 '13 at 04:43
  • 1
    @jhyap If you're saying C# doesn't have pointers.. it most certainly does. Not quite as flexible as other languages.. but they still exist. – Simon Whitehead Nov 14 '13 at 04:43
  • If does, show me a *pointer in C# – jhyap Nov 14 '13 at 04:44
  • 3
    @jhyap: are you kidding? Put `C# pointers` into google and spend some time reading – zerkms Nov 14 '13 at 04:45
  • 1
    Include the C++ function declaration in your question, otherwise an answer is random guessing. – William Nov 14 '13 at 04:45
  • @jhyap Here's one of my answers with pointers: http://stackoverflow.com/a/15424647/1517578 – Simon Whitehead Nov 14 '13 at 04:45
  • @jhyap [Pointer types (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/y31yhkeb%28v=vs.110%29.aspx) (however pointers is not the correct solution to this, as William said, we need to see the C++ function declaration to tell you what you should do) – Scott Chamberlain Nov 14 '13 at 04:45
  • OMG... what book I have read before and miss lead me... Apologize for my statement on "C# do not have pointer". And here are some source: http://www.tutorialspoint.com/csharp/csharp_unsafe_codes.htm – jhyap Nov 14 '13 at 04:47
  • the problem is the mismatch with the `string` type in both. Find out the underlying structure for the c++ `string` (perhaps it is just `char*` if you are lucky) and then use that. – leppie Nov 14 '13 at 05:34
  • Is your "c++ wrapper" written in C++/CLI? – Alvin Wong Nov 14 '13 at 05:39
  • yes, a c++ class library – user2990515 Nov 14 '13 at 05:43
  • What are you trying to do with a `System.String` pointer? .NET Strings are immutable so it doesn't make much sense to have a .NET String pointer. – Alvin Wong Nov 14 '13 at 05:56
  • @AlvinWong - even though a .NET string is immutable, when you fix it in memory and have pointer to it, you can modify it. I am not telling you that this is a wise thing to do but it is possible. – Emond Nov 14 '13 at 06:35

1 Answers1

1

If I understand you correctly you want to call a C function with string as parameters.
For this, you usually use PInvoke (platform invoke), which uses marshalling under the hand.

This example takes a string and returns a string.

[DllImport(KMGIO_IMPORT,CallingConvention=CallingConvention.Cdecl)]     
//DLL_EXPORT ushort CALLCONV cFunction(char* sendString, char* rcvString, ushort rcvLen);
private static extern UInt16 cFunction(string sendString, StringBuilder rcvString, UInt16 rcvLen);

public static string function(string sendString){
    UInt16  bufSize = 5000;
    StringBuilder retBuffer = new StringBuilder(bufSize);
    cFunction(sendString, retBuffer, bufSize);
    return retBuffer.ToString();
}
joe
  • 8,344
  • 9
  • 54
  • 80
  • The main point in joes code here is the use of StringBuilder. Normal strings in .NET are immutable by design, which is why a pointer to such a string is not a great idea to pass to code that wants to manipulate that string. StringBuilder is different however,since it by design acts more like a buffer to a piece of memory that can be modified making it modifiable also from unmanaged code, an later can be used to produce an immutable string in .NET. Check out the dox on StringBuilder and pinvoke (pinvoke.net) and you should get some ideas. – Mahol25 Apr 22 '16 at 13:47