1

I'm having trouble passing in an array of strings from my c# code to a function from my c++ dylib.

C# code:

[DllImport("array2d.dylib", EntryPoint = "process_array", CallingConvention = CallingConvention.Cdecl)]
    public static extern int process_array(String[] a, int b);
    static void Main(string[] args)
    {

        String[] list = new String[] { "Abc" , "def", "ghi", "jkl"};
        int josh = process_array(list, 2);                          
     }

My C++ code:

 #include <string>
#include <iostream>



int process_array(char** array, int rows)
{

    std::string s1 ("Array : [");

        for (int i = 0; i < 6; ++i){
                s1.append(array[i]);
                s1.append(", ");

        }
        s1.append("] \n");


        return 1;

}

int main()
{

}

And the error I have been getting is:

Unhandled Exception: System.EntryPointNotFoundException: Unable to find an entry point named 'process_array' in DLL 'array2d.dylib'. at JoshServer.Program.process_array(String[] a, Int32 b)

Any help is appreciated, thanks.

1
  • Does your cpp file export any function? There must be some keywords like dllexport. Read about creating dlls in cpp. Commented Jul 19, 2017 at 13:38

1 Answer 1

1

The function in your C++ program is not being exported:

int process_array(char** array, int rows)

You must mark it with dllexport, like this:

extern "C" int process_array(char** array, int rows)

Update: This project contains the examples used in a talk I gave a while ago about PInvoke, I hope it helps.

Some corrections.

 for (int i = 0; i < 6; ++i){

Should be:

 for (int i = 0; i < rows; ++i){

And

int josh = process_array(list, 2);  

Should be

int josh = process_array(list, list.Length);  

Updated: Removed __declspec(dllexport) (osx) and added some corrections.

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

2 Comments

I'm on a mac and the compiler goes crazy when I add that. From what I read it says to take it out? stackoverflow.com/questions/6838222/…
Hi there, sorry I didn't realize you were in a mac. Would you mind trying to add the extern "C" expression to the function?

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.