简体   繁体   中英

Application crashes when creating an object

I have a class which inherits from IDirectInputA interface.

here: http://pastebin.com/QuHP02ai

so, when i try to create object of this, application crashes (calls CorExitProcess from somewhere). What i did wrong?

ps Direct input v. 7

pps

this code creates object. I deleted some code from it, except the main part

IDirectInputA** ppDI;
HRESULT hr = _DirectInputCreateA(hinst, dwVersion, ppDI, punkOuter);
xDirectInputA xDI = new xDirectInputA((IDirectInputA*)(*ppDI));

When you create your instance, you pass a pointer to IDirectInputA, right? What pointer do you pass? If you pass an uninitialized or a null pointer, you will get undefined behavior.

TBH what you are trying to do is more complicated than you think. The problem arises in what exactly you are trying to do. Are you trying to wrap IDirectInputA OR are you trying to completely re-implement it.

If you are trying to wrap it do the following:

IDirectInputA* pDI = NULL;
HRESULT hr = _DirectInputCreateA( hinst, dwVersion, &pDI, NULL );

Then create your derived class as follows:

class xDirectInputA : public IDirectInputA
{
protected:
    IDirectInputA* mpInternal;
public:
    xDirectInputA( IDirectInputA* pInternal ) :
         mpInternal( pInternal )

    HRESULT CreateDevice( REFGUID rguid, IDirectInputDevice** ppDirectInputDevice, IUknown* pOuter )
    {
        // Do what ever processing you need.
        return mpInternal->CreateDevice( rguid, ppDirectInputDevice, pOuter );
    }

    // Implement other functions.
};

Now you pass your xDirectInputA pointer around instead of the normal pointer returned from DirectInputCreate. You can now intercept every message that goes through the class.

If you are trying to do your own full re-implementation it is a LOT more complicated. You are going to need to fully implement the COM object. You'll be best off putting a DInput.DLL alongside the executable that contains your implementation. All in though this is only something you should try if you REALLY know what you are doing.

If you wish to learn COM fully I suggest purchasing Essential COM by Don Box. Its a VERY helpful book.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM