There should be no need to compile or test anything. Someone familiar with C++ arrays can nail this one down with a suggested code change. Thank you.
The following C++ wrapper code correctly runs in scenario of MetaTrader ==> C++ Wrapper.dll ==> C#.dll ==> C++Wrapper.dll ==>MetaTrader. The C# .dll modifies the string and passes it back up. The C++Wrapper.dll stuffs the string characters into an array and sends it back to MetaTrader. That was certainly the hard part.
But now I think there would be a memory leak because the C++ array char8UnmanArr is never deallocated. The method must return the array, and after that the array needs to be deleted. Hmmm. I don't understand how to rewrite this C++ code so that the array is sized correctly upon allocation, used, and then is deleted after it is returned.
#include "stdafx.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#ifdef _MANAGED
#pragma managed(pop)
#endif
#using "GetMarketTimeAssembly.dll
"
using namespace GetMarketTimeAssembly;
__declspec(dllexport) char* __stdcall Hello(char* name) //Use __stdcall to call the C# method Hello
{
int i = 0;
while (*name != '\0') //Count characters in name
{
i++;
name++;
}
//Assign a handle (cap, ^) to a managed object nameManArr
array<unsigned char>^ nameManArr = gcnew array<unsigned char>(i);
name -= i;
i = 0;
while (*name != '\0') //Load name into array
{
nameManArr[i] = *name;
name++;
i++;
}
array<unsigned char>^ char8ManArr = GetMarketTimeClass::Hello(
nameManArr
);
char* char8UnmanArr = new char[char8ManArr->Length + 1];
for (int i = 0; i < char8ManArr->Length; i++)
{
char8UnmanArr[i] = char8ManArr[i];
}
char8UnmanArr[char8ManArr-
>Length] = '\0';
return char8UnmanArr; //Send the array of characters back to the calling program
}
Start Free Trial