Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi Guys,

 

I am using mono right now. I know it has been discussed here and that is the reason i though i will find help for this. I use c# wrapper class for pinvokes written in c++. The problem is i get a failed to load function error for those methods in the c++ implementation. I used the dll import statement as follows.

 

[DllImport(libname,EntryPoint="functionname")]

 

Even though those methods are defined in the library it gives me this error message. Since the concept is almost similar i hope i will get some help.

 

Sorry if i have made mistake by discussing mono related issues here.

 

Regards

Yashasvi

Posted (edited)

Re:

 

Hi PlausiblyDamp,

 

I also wrote the test application which simply prints the hello and a number. I am posting the code of this test class i wrote. This also gives me the same warning failed to load function ... Here goes the c++ code

 

#include<stdio.h>

 

class test

{

private:

int x;

public:

 

test()

{

x = 0;

}

 

void printhello();

};

 

void test::printhello()

{

x++;

printf("hello %d\n",x);

}

 

 

Here goes the C# wrapper class

 

internal class testwrap

{

[DllImport("Test",EntryPoint="printhello")]

public static extern void printhello();

}

 

Since i am not passing any arguements i do not think this is the problem with marshalling either. One more thing i noticed was that the the compiler for c++ code was mangling the names when used both gcc or g++. I think this is the reason the error message was popping up.

 

And also when i tried to give the mangled name itself in dll import (I did this using the nm command on linux) it thows the nullreference exception and says a null value was found where an object instance was required.

 

How do i work around this.

 

Please help me out

 

Regards

Yashasvi

Edited by yraykar
  • Administrators
Posted

Trying to remember back to the days I did a bit of C++....

IIRC you will need to mark the public function as using a C style name i think the syntax is

 

extern "C" void test:Printhello()
{
x++;
printf("hello %d\n",x);
}

 

if that isn't correct have a search for extern, it stops the C++ compile decorating the generated function names.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Re

 

Hi PlausiblyDamp,

 

Thanks for the reply again.

 

I have not tried extern "C" block on the class members. I read it somewhere in the web that it is not possible to use it on class members.

 

In any case i will try it out and let you know.

 

Thanks again for the reply.

 

Regards

Yashasvi

Posted

Re: extern "C"

 

Hi PlausiblyDamp,

 

The changes suggested were incorporated but still i receive the warning message and the System.MissingMethodException.

 

I feel we cannot have extern "C" construct on the member methods. Even though the compile (gcc ) would not give error. It does not worl with the wrapper

 

If there is anything wrong please help me out

 

Regards

Yashasvi

Posted

Re:

 

Hi PlausiblyDamp,

 

Defining the class itself as extern "C" did not yield any result. I have found another work around for the same though. It goes like this

 

extern "C"

{

test t;

void tprinthello()

{

t.printhello();

}

}

 

And call the tprinthello method in the wrapper. This works fine.

 

Thanks again for the help and reply.

 

Regards

Yashasvi

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...