0

[C/C++ Code]

extern "C" __declspec(dllexport) int Analyze_input_text(char* input_text, char *ppArray){
   int size;
   // code...
   return size;
}

[C# Code]

[DllImport("PP_TextAnalyzer.dll",CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Unicode)]
extern public static int Analyze_input_text(IntPtr data, [MarshalAs(UnmanagedType.LPArray, SizeConst = 100)]  string[] ppArray);

public int GetResultData(string input_text)
{
    IntPtr pStr = Marshal.StringToCoTaskMemUni(input_text);
    List<string> ppArray = new List<string>();
    int size = **Analyze_input_text(pStr,ppArray.ToArray());** //Array(List) is still null.
    return size;
}

I don't know how to fix this problem anymore...

Anyone help?

1
  • 1
    What's the contract around the ppArray parameter? who's responsible for allocating it? freeing it? If it's a char*, it's not an array of char*. Commented Apr 14, 2013 at 10:54

1 Answer 1

3

CharSet=CharSet.Unicode

Well, it isn't. This function takes a char*, an 8-bit character type. It is also rather unclear how ppArray could be an array of strings, that would be char**. As declared, the proper pinvoke declaration is:

[DllImport("PP_TextAnalyzer.dll", CallingConvention = CallingConvention.Cdecl, 
              CharSet=CharSet.Ansi)]
extern public static int Analyze_input_text(string data, string ppArray);

If you truly meant to pass an array of strings then you will have to modify the C function to at least:

extern "C" __declspec(dllexport) 
int Analyze_input_text(char* input_text, char** ppArray, int arraySize)

And declare the ppArray argument as string[] in your C# code. Do note that you really ought to use Unicode in your C code, a wchar_t*. Trying to "analyze" text that got whacked from Unicode to the local 8-bit system code page is a lossy proposition from the get-go. And of course make sure that this code wouldn't already be fast enough and less lossy when you write it in C#. .NET has rather good support for Unicode.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Actually I treat Korean character (I think it's unicode..) with .NET.

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.