CComPtr<T> System.AccessViolationException
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: CComPtr<T> System.AccessViolationException

  1. #1
    Join Date
    Nov 2013
    Location
    Milan [.NET 4.5]
    Posts
    17

    CComPtr<T> System.AccessViolationException

    Hi all,


    I have a C++/CLI console application and I should pass a CComPtr<T> to a class but when I try to access it, occours a System.AccessViolationException.


    Following the main code:

    Code:
    CComPtr<IAuga> auga;
    
    CLSID clsAuga;
    HRESULT hr = CLSIDFromProgID(L"Heimdall.Auga.1", &clsAuga);
    if (hr > -1)
    {
    	hr = CoCreateInstance(clsAuga, NULL, CLSCTX_INPROC_SERVER, __uuidof (IAuga), (void**)&auga);
    	if (hr > -1)
    	{
    		printf("Auga COM component created successfully!\n");
    	}
    	else
    	{
    	        //error
    	}
    }
    
    CameraHook^ cam = gcnew CameraHook((CComPtr<IAuga>*&)auga);
    
    cam->StartCamera();
    And following the CameraHook.h and CameraHook.cpp:

    Code:
    ref class CameraHook
    {
    private:
    	CComPtr<IAuga> *auga;
    public:
    	CameraHook(CComPtr<IAuga> *ag);
            void StartCamera();
    };
    Code:
    CameraHook::CameraHook(CComPtr<IAuga> *ag)
    {
    	auga = ag;
    }
    void CameraHook::StartCamera()
    {
    	HRESULT hr = auga->p->SetCameraIDispatch(........);
    }

    When the console reachs auga->p->SetCameraIDispatch(), it breaks.


    What's the problem?

    Thanks a lot!

    Marco

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: CComPtr<T> System.AccessViolationException

    Why did you implement CameraHook as a managed class at all? I can't see any reaon for that in what you posted, and turning it into a native class may alreafy eliminate your probem, as native/managed interop can be a quite tricky thing.

    Going even further, why is your console app a managed one in the first place? There may, however, be a reason for that lying outside the scope of your post.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Nov 2013
    Location
    Milan [.NET 4.5]
    Posts
    17

    Re: CComPtr<T> System.AccessViolationException

    Quote Originally Posted by Eri523 View Post
    Why did you implement CameraHook as a managed class at all? I can't see any reaon for that in what you posted, and turning it into a native class may alreafy eliminate your probem, as native/managed interop can be a quite tricky thing.

    Going even further, why is your console app a managed one in the first place? There may, however, be a reason for that lying outside the scope of your post.
    I've posted a partial code for simplicity.

    The entire class is the following:

    Code:
    ref class CameraHook
    {
    private:
    	int cameraOffset;
    	DWORD_PTR dwPtr;
    
    	CComPtr<IAuga> *auga;
    public:
    	CameraHook(CComPtr<IAuga> *ag);
    	HRESULT StartCamera();
    	HRESULT StopCamera();
    
    	static VCamSDKClass^ vCamSDK;
    };

    I need the managed class because VCamSDKClass is managed. It's wrong?

    I'm new in C++/CLI and probably I'm missing something.


    Thanks,

    Marco

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,588

    Re: CComPtr<T> System.AccessViolationException

    Quote Originally Posted by marco.bernasconi View Post
    I need the managed class because VCamSDKClass is managed. It's wrong?
    Well, the name of that class suggests that it's from a library you didn't implement yourself, so you may not simply change that one into a native class. If you need that class and are not accessing it via COM, that would at least mean your application needs to be managed. (... unless you write a native wrapper DLL around the SDK library that exposes a native interface and access that from your native application, but at the moment I can't see what benefit that would gain.)

    However, CameraHook::vCamSDK is that class' only managed member, and it's static as well as public, so does it really need to be a member of CameraHook? If you can move it elsewhere, that would allow you to make CameraHook native. Also, there is a way to have references to native objects as members in a native class, but that's non-trivial and I'd have to look up how that's done myself first. So if there are any simpler options, I'd think they'd be preferable.

    Finaly, perhaps it would allow us to offer better assistance if we'd know the bigger picture. What non-standard libraries (and hardware, if applicable) are you using? Perhaps someone around here has already worked with that and can give more qualified comments.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center