Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
trying to call Find and get error:c0000005 (Attempted to read or write protected memory. This is often an indication that other memory is corrupt.)
has all the usual COM stuff and register's fine just cant get the find call to work.

pretty new to c++ and COM but im thinking its not seeing the Find function, not coded right? looking for some pointers.

Test.exe
C#
public partial class Form1 : Form
    {

        private CRock foo = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create COM
            this.foo = (CRock)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("DA798219-043F-4B3A-B0E2-78232DA3FF4F"))); //<-- Loads Good
            int num = foo.Find();//<-- returns Attempted to read or write protected memory
            if(num == 1)
            {
                //Find was found
            }
        }

        [ComImport, CoClass(typeof(object)), TypeIdentifier, CompilerGenerated, Guid("0D77311E-D13A-485A-8206-F99422E34E82")]
        public interface CRock : IRock
        {
        }

        [ComImport, TypeIdentifier, CompilerGenerated, Guid("0D77311E-D13A-485A-8206-F99422E34E82")]
        public interface IRock
        {
            int Find();
        }

        }



test.dll COM

IRock.h
C++
typedef [uuid("0D77311E-D13A-485A-8206-F99422E34E82")]
struct IRock : public IUnknown
{
	// IUnknown
	STDMETHOD_(ULONG, AddRef)() PURE;
	STDMETHOD_(ULONG, Release)() PURE;
	STDMETHOD(QueryInterface)(REFIID riid, void** ppv) PURE;

	STDMETHOD_(INT32, Find)() PURE;
};

CRock.h
#include "IRock.h" 
class CRock : public IRock
{
public:
	CRock();
	virtual ~CRock();

	// IUnknown
	STDMETHOD_(ULONG, AddRef)();
	STDMETHOD_(ULONG, Release)();
	STDMETHOD(QueryInterface)(REFIID riid, void** ppv);


	STDMETHOD_(INT32, Find)();
	
protected:
	ULONG m_uRefCount;
};


CRock.cpp


C++
#include "stdafx.h"
#include "CRock.h"
#include "uuid.h"         // for __uuidof info

//////////////////////////////////////////////////////////////////////
// Construction/Destruction

CRock::CRock()
{
	m_uRefCount = 0;
	g_uDllLockCount++;
}

CRock::~CRock()
{
	g_uDllLockCount--;
}


//////////////////////////////////////////////////////////////////////
// IUnknown methods

STDMETHODIMP_(ULONG) CRock::AddRef()
{
	

	return ++m_uRefCount;               // Increment this object's reference count.
}

STDMETHODIMP_(ULONG) CRock::Release()
{
	ULONG uRet = --m_uRefCount;             // Decrement this object's reference count.

	if (0 == m_uRefCount)             // Releasing last reference?
		delete this;

	return uRet;
}

STDMETHODIMP CRock::QueryInterface(REFIID riid, void** ppv)
{
	HRESULT hrRet = S_OK;

	// Check that ppv really points to a void*.

	if (IsBadWritePtr(ppv, sizeof(void*)))
		return E_POINTER;

	// Standard QI initialization - set *ppv to NULL.

	*ppv = NULL;

	// If the client is requesting an interface we support, set *ppv.

	if (InlineIsEqualGUID(riid, IID_IUnknown))
	{
		*ppv = (IUnknown*) this;
	
	}
	else if (InlineIsEqualGUID(riid, __uuidof(IRock)))
	{
		*ppv = (IRock*) this;
	
	}
	else
	{
		hrRet = E_NOINTERFACE;
		
	}

	// If we're returning an interface pointer, AddRef() it.

	if (S_OK == hrRet)
	{
		((IUnknown*)*ppv)->AddRef();
	}

	return hrRet;
}

STDMETHODIMP_(INT32) CRock::Find()
{
	return 1;
}


What I have tried:

changed layout several times of STDMETHOD_(INT32, Find)() PURE; just not sure where to go from here
Posted
Updated 16-Feb-16 4:18am
v8
Comments
Richard MacCutchan 16-Feb-16 11:28am    
Use your debugger to trap where the code is at the time of the exception.

1 solution

You must also fully implement all function for the IUnknown interface in the CRock.cpp.
 
Share this answer
 
Comments
JEEPRFAM 16-Feb-16 10:21am    
already had all functions for IUnknown just left them out of the post, I updated my question with there code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900