C#调用c ++“com”函数试图读取或写入受保护的内存 [英] C# calling a c++ "com" function getting attempted to read or write protected memory

查看:77
本文介绍了C#调用c ++“com”函数试图读取或写入受保护的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试调用查找并获取错误:c0000005(尝试读取或写入受保护的内存。这通常表示其他内存已损坏。)

具有所有常用的COM内容并且注册正常只是不能找到工作的调用。



对c ++和COM来说很新,但我认为它没有看到Find函数,没有编码对吗?寻找一些指示。



Test.exe

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

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 < br $>


IRock.h




test.dll COM

IRock.h

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







CRock.cpp


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





我的尝试:



多次更改STDMETHOD_(INT32,Find)()PURE的布局;只是不确定从何处开始



What I have tried:

changed layout several times of STDMETHOD_(INT32, Find)() PURE; just not sure where to go from here

推荐答案

您还必须完全实现CRock.cpp中IUnknown接口的所有功能。
You must also fully implement all function for the IUnknown interface in the CRock.cpp.


这篇关于C#调用c ++“com”函数试图读取或写入受保护的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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