0

I'm trying to call a function in a C++/CLR dll from a C# dll. -> this is working pretty well.

The issue I'm facing is the parameters which should be passed from C# to the function in C++/CLR are not arriving correctly in C++.

In the end I need to pass an array of strings and some boolean parameters from C# to C++/CLR.

What I have done/tried so far:

My C++/CLR part Looks like this:

__declspec(dllexport) void __cdecl BCFConsumer::Translatehelper(char **IDs, int len, bool blinking, bool highlight, bool showOnly, bool zoom){
    vector<std::wstring> IDsVec;
    std::wstring tmp;
    msclr::interop::marshal_context context;

    for (int i = 0; i < len; i++) {
        IDsVec.push_back(std::wstring(IDs[i], IDs[i] + strlen(IDs[i])));
    }

    ShowElements(IDsVec, blinking, highlight, showOnly, zoom);
}

My C# (dll-import) part looks like this:

[DllImport("BCF.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?Translatehelper@BCFConsumer@@QEAAXPEAPEADH_N111@Z")]
        public static extern void Translatehelper(string[] IDs, int size, bool blinking, bool highlight, bool showOnly, bool zoom);

The function call in C# looks like this:

    public void btnshow_Click(object sender, RoutedEventArgs e)
            {
                IDs.Add("0znBcjLrbFBxuK9yivEUEO");
                IDs.Add("0cLtxHLKfA9x58$Mi2DnUp");
                Translatehelper(IDs.ToArray(), IFCIDs.Count, _blinking, _highlight, _showOnly, _zoom);
            }

IDs is defined like this:

public List<string> IFCIDs = new List<string>();
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Take a look at [Marshal.StringToHGlobalAuto](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.stringtohglobalauto?view=netframework-4.8). It might help. – Louis Go Mar 24 '20 at 01:03
  • you could take a look at https://stackoverflow.com/questions/16447506/pass-an-array-of-strings-from-c-sharp-to-a-c-dll-and-back-again – SiBrit Mar 24 '20 at 01:41
  • 1
    You can't pinvoke a C++ class member function, no value for *this*. The function must be a static member. Use C++/CLI to create a wrapper. – Hans Passant Mar 24 '20 at 18:33
  • @Hans Passant: Thanks for your tip function has to be static. This worked out! – user13112723 Apr 11 '20 at 11:01

0 Answers0