I am calling a function (named cbFunction) from a c++ dll using c# code. I have to pass array of strings (named strArray) ,from c# code, as arguments to c++ 'cbFunction'. Inside c++ function, I have to change the array's values. The newly changed values should be read back in c# code.
Problems I face:
- The base address of strArray in c# and the one received in arguments in c++ dll are totally different.
- I can read the array of strings in c++ dll. But, I have to modify the array's values inside c++ function. When I change those values in c++ function, the change is not reflected in c# code. `
C# code
public static class Call_API
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate bool CallBackFunction(string[] argv, int argc);
private static string[] strArray;
public static void ChangeValue()
{
IntPtr pDll = NativeMethods.LoadLibrary("DllFunctions.dll");
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "cbFunction");
string args = "Hello Disney World";
strArray = args.Split(' ');
CallBackFunction cbFunction = ((CallBackFunction)Marshal.GetDelegateForFunctionPointer(
pAddressOfFunctionToCall, typeof(CallBackFunction));
if(false == cbFunction(strArray, strArray.Count()))
{
//some task
}
}
}
c++ dll code (DllFunctions.dll) :
bool cbFunction(char** argv, int argc)
{
//Some task
char VarValue[256] = { 0 };
std::string s = "Jetix";
sprintf_s(VarValue, s.c_str());
strcpy_s(argv[1], sizeof(VarValue), VarValue);
return false;
}
Based on this code, my expected values at the end in strArray (in c#) are {"Hello", "Jetix", "World"}.
Thanks in advance!