参数中的数据值被复制到其他参数c ++,c# [英] Data values in the parameters are beeing copied to the other parameters c++, c#

查看:159
本文介绍了参数中的数据值被复制到其他参数c ++,c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中,我正在导出的方法是:

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

在c#中我正在导入此方法:

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

我在c#中调用这个方法,并传递值:

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

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

问题是,当我调试到本机代码时,具有值sdf,dfggh,vbnfg的参数x,y,z,并且在它们进入本地c ++ dll方法之前到达c ++ dll时被改变。

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

是给我错误说,空指针值传递给函数。可以任何一个帮助我修复这个奇怪的问题。

解决方案

看起来你的本地方法是一个实例我想你的第一个参数以某种方式映射到this。



这里是一个例子:

  #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;

}
};

请参阅第一个上的静态关键字?



导入(我使用的是名字,因为我很懒惰):

  [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);

这使它像这样工作:

  check(x,yy,zzz); 

实例方法需要IntPtr

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

,我的test.txt的内容是:

  x 
yy
zzz

和testInst.txt

  1 
12
123


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.

解决方案

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

这篇关于参数中的数据值被复制到其他参数c ++,c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆