本文介绍了[解决]具有DllImport自定义属性的函数无法返回类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本机c ++ dll,我想从c#中使用它,所以我写了一个包装器.我正在尝试在dll中导入该类的功能.
我的包装器

I have a native c++ dll and i want to use it from c# so i wrote a wrapper. I am trying to import a function of the class in the dll.
My wrapper

public  struct myclassUnman{    
        
    public:
        int size;        
        unsigned char* data;

        [DllImport("somedll.dll", 
                  EntryPoint="??0myclass@@QAE@H@Z", 
                  CallingConvention=CallingConvention::ThisCall)]
        static void ctor(myclassUnman*,int);

        [DllImport("somedll.dll", 
                  EntryPoint="??1myclass@@QAE@XZ", 
                  CallingConvention=CallingConvention::ThisCall)]
        static void dtor(myclassUnman*);    
        
        [DllImport("somedll.dll", 
                  EntryPoint="?getSize@myclass@@QAEHXZ", 
                  CallingConvention=CallingConvention::ThisCall)]
        static int getSize(myclassUnman*);    

        [DllImport("somedll.dll", 
                  EntryPoint="?getValueAsString@myclass@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ", 
                  CallingConvention=CallingConvention::ThisCall)]        
        static string getValueAsString(myclassUnman*);            
        
    };
    public ref class myclassWrap
    {
    public: 
        
        myclassWrap(int a)
        {
            tv = new myclassUnman();    
            tv->size = a;
            myclassUnman::ctor(tv,a);
        }
        
        ~myclassWrap()
        {
            myclassUnman::dtor(tv);
        }          
        
        int  getSize()
        {
          return myclassUnman::getSize(tv);            
        }   

        string getValueAsString(){    
            return myclassUnman::getValueAsString(tv);        
        }
        }        
    private:
        myclassUnman *tv;
    };




当我构建此代码时,它给出错误:
错误C3385:"myclassUnman :: getValueAsString":具有DllImport自定义属性的函数无法返回类的实例.
它不允许返回字符串".

dll中定义了"getValueAsString"函数:




when i build this code it gives the error:
error C3385: ''myclassUnman::getValueAsString'' : a function that has a DllImport custom attribute cannot return an instance of a class.

it doesn''t allow to return ''string''.

''getValueAsString'' function is defined in the dll :

string myclass::getValueAsString()
{
  string res = string(size,''0'');
  for (int i=0;i<size;i++)
	  res[i]=data[i];
  return res;
}




我该怎么办?请帮忙..




what should i do?please help..

推荐答案






当我构建此代码时,它给出错误:
错误C3385:"myclassUnman :: getValueAsString":具有DllImport自定义属性的函数无法返回类的实例.
它不允许返回字符串".

dll中定义了"getValueAsString"函数:




when i build this code it gives the error:
error C3385: ''myclassUnman::getValueAsString'' : a function that has a DllImport custom attribute cannot return an instance of a class.

it doesn''t allow to return ''string''.

''getValueAsString'' function is defined in the dll :

string myclass::getValueAsString()
{
  string res = string(size,''0'');
  for (int i=0;i<size;i++)
	  res[i]=data[i];
  return res;
}




我该怎么办?请帮忙.




what should i do?please help..


这篇关于[解决]具有DllImport自定义属性的函数无法返回类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 04:18