Wrapper problem

yraykar

Active member
Joined
Oct 13, 2003
Messages
42
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
 
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
 
Last edited by a moderator:
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

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

if that isnt correct have a search for extern, it stops the C++ compile decorating the generated function names.
 
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
 
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
 
Youre probably correct on the member function bit - this is pushing my C++ knowledge a bit, only ever dabbled with it.
You could declare the class as extern "C" though, does that have any effect?
 
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
 
Back
Top