0

In c++ the method that I am exporting is:

__declspec(dllexport) int __thiscall A::check(char *x,char *y,char *z)
{
  temp=new B(x,y,z);
}

In c# I am importing this method like this:

[DllImport("IDLL.dll", CallingConvention=CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
    public static extern int check(string x, string y, string z);

I am calling this method in c# like this and passing the values:

public int temp()
{
  string x="sdf";
  string y="dfggh";
  string z="vbnfg";
  int t;

  t=Class1.check(x,y,z);
  return t;
}

The problem is that when I debug in to the native code I see that the parameters x,y,z having values sdf,dfggh,vbnfg and being altered when they reach c++ dll like this even before it is entering the native c++ dll method.

x=dfggh,y=vbnfg,z=null value

and is giving me the error saying that null pointer value is passed to the function. Can any one help me out fixing this weird problem.

krishna555
  • 223
  • 5
  • 18
  • Just had a look at some code I wrote a few years ago. Try adding `[MarshalAs(UnmanagedType.LPStr)]` before each of the `string` arguments. The `UnmanagedType.LPStr` means _A pointer to a null-terminated array of ANSI characters_. – hmjd Jun 04 '12 at 13:40
  • Is that true that only z becomes null, and there is no problem with x and y? If yes, then check those parts of the ctor of B which modifies z. – kol Jun 04 '12 at 13:41
  • @kol --- even before entering the entering the constructor B itself the values are being changed. – krishna555 Jun 04 '12 at 13:45
  • @hmjd --- i tried what you mentioned but having the same problem. – krishna555 Jun 04 '12 at 13:47

1 Answers1

1

Looks like your native method is an instance(vs static) method. I guess your first parameter gets mapped to 'this' somehow.

Here is an example:

#include <fstream>
using namespace std;

class A
{
public:
__declspec(dllexport) static int __stdcall check(char *x,char *y,char *z)
{
    ofstream f;
    f.open("c:\\temp\\test.txt");
    f<<x<<endl;
    f<<y<<endl;
    f<<z<<endl;
    return 0;

    }

__declspec(dllexport) int __thiscall checkInst(char *x,char *y,char *z)
{
    ofstream f;
    f.open("c:\\temp\\testInst.txt");
    f<<x<<endl;
    f<<y<<endl;
    f<<z<<endl;
    return 0;

    }
};

see the static keyword on the first one?

Imports(i used the mangled names because I'm lazy):

[DllImport("TestDLL.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, EntryPoint = "?check@A@@SGHPAD00@Z")]
    public static extern int check(string x, string y, string z);

    [DllImport("TestDLL.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "?checkInst@A@@QAEHPAD00@Z")]
    public static extern int checkInst(IntPtr theObject, string x, string y, string z);

That makes it work just like that:

check("x", "yy", "zzz");

the instance method requires an IntPtr

IntPtr obj = IntPtr.Zero;
checkInst(obj, "1", "12", "123");

and the contents of my test.txt are:

x
yy
zzz

and testInst.txt

1
12
123
Ventsyslav Raikov
  • 6,882
  • 1
  • 25
  • 28
  • what do you mean by "native method is an instance(vs static) method" – krishna555 Jun 04 '12 at 14:50
  • A::check looks like a non static(which requires an object) method on a class A, you need to export static method or a function for P/Invoke to work – Ventsyslav Raikov Jun 04 '12 at 14:53
  • i think there is no need for a method needs to be static to use pinvoke. So as the method is of type __stdcall it needs to be static? – krishna555 Jun 04 '12 at 15:00
  • check this answer http://stackoverflow.com/a/2354222/842543 You cannot just call statically a method from C# - it requires an object on the other side? who will construct it? – Ventsyslav Raikov Jun 04 '12 at 15:08
  • ok i am getting confused here can you provide a sample code that says your point. Basic example will be good enough. – krishna555 Jun 04 '12 at 15:20
  • that worked thank you but came across another error as an extension of this can you please take a look at this as well at "http://stackoverflow.com/questions/10855149/getting-an-access-violation-error-is-c-sharp-while-calling-the-c-code" – krishna555 Jun 04 '12 at 16:08