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

14
  • 1
    OOP do not have pointer Commented Nov 14, 2013 at 4:42
  • 3
    @jhyap: "OOP do not have pointer" --- what does it mean? Commented Nov 14, 2013 at 4: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. Commented Nov 14, 2013 at 4:43
  • 3
    @jhyap: are you kidding? Put C# pointers into google and spend some time reading Commented Nov 14, 2013 at 4:45
  • 1
    Include the C++ function declaration in your question, otherwise an answer is random guessing. Commented Nov 14, 2013 at 4:45

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.