Attempted to read or write protected memory while importing function from c++ dll

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bltfr
    New Member
    • Feb 2010
    • 2

    Attempted to read or write protected memory while importing function from c++ dll

    hi,
    I have a native c++ dll and i am trying to import a function of the class in the dll to use it in c# so i wrote a wrapper :
    Code:
    public struct myclassUnman{
    	
    	public:
    		[DllImport("mydll.dll", 
    				  EntryPoint="??0myclass@@QAE@XZ", 
    				  CallingConvention=CallingConvention::ThisCall)]
    		static void ctor(myclassUnman*);
    
    		[DllImport("mydll.dll", 
    				  EntryPoint="??1myclass@@QAE@XZ", 
    				  CallingConvention=CallingConvention::ThisCall)]
    		static void dtor(myclassUnman*);	
    
    		[DllImport("mydll.dll", 
    				  EntryPoint="?getboolvalue@myclass@@QAE_NPAEH@Z", 
    				  CallingConvention=CallingConvention::ThisCall)]
    		static bool getboolvalue(myclassUnman*,unsigned char*,int);
    
    	};
    	public ref class myclassWrap
    	{
    	public: 
    		myclassWrap(){
    			tv = new myclassUnman();
    			myclassUnman::ctor(mu);
    		}
    		~myclassWrap(){
    			myclassUnman::dtor(mu);
    		}
    		bool getboolvalue(unsigned char* a,int b){
    			return myclassUnman::getboolvalue(mu,a,b);
    		}
    		
    	private:
    		myclassUnman *mu;
    	};
    the prototype of function in the dll :
    Code:
    public: bool __thiscall myclass::getboolvalue(unsigned char *,int)
    and finally in my c# code i call the wrapper but it gives an exception in the following line:
    Code:
    return myclassUnman::getboolvalue(tv,a,b);
    Exception is:
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
    my c# code:
    Code:
     unsafe
                {
                    myclassWrap mw = new myclassWrap();
                    Console.WriteLine(mw.getboolvalue((byte*)11, 2));
    
                 }
    please help me.
Working...