Thread: 'Access Violation' in CreateInstance()??

  1. #1
    rhinoishere
    Guest

    'Access Violation' in CreateInstance()??

    Am working through Andrew Troelsen's "COM and ATL 3.0" and am having a problem when trying to run a C++ client (even the ones that he wrote and distributed with the book), which uses coclasses from a COM object. I have reason to believe that I am seeing this based upon some setting on my system, so I'm hoping that one of you can tell me what I need to do. I'm posting the code from the client (it's from the books CD) so you can see which line I am getting the error on. If anyone can help, I'd appreciate it. Thanks in advance, Ryan

    ps - the offending line is commented as such. Look for '@@@@@'


    Code:
    #include "interfaces.h"
    #include "iid.h"
    #include <iostream.h>
    
    int main()
    {
    	CoInitialize(NULL);	// This parameter is reserved, and should be NULL
    
    	HRESULT hr;
    	IClassFactory* pCF = NULL; 	
    	ICreateCar* pICreateCar = NULL;
    	IStats* pStats = NULL;
    	IEngine* pEngine = NULL;
    
    	cout << "*********************************" << endl;
    	cout << "The Amazing CoCar Client" << endl;
    	cout << "*********************************" << endl;
    	// Get the class factory pointer of CoCar:
    	hr = CoGetClassObject(CLSID_CoCar, CLSCTX_ALL,
    				   NULL, IID_IClassFactory, (void**)&pCF);
    
    	// Make a CoCar & get ICreateCar.
            // @@@@@ The error is thrown on the following line @@@@@
    	hr = pCF->CreateInstance(NULL, IID_ICreateCar, (void**)&pICreateCar);
    
    	pCF->Release();
    	
    	if(SUCCEEDED(hr))
    	{
    		pICreateCar->SetMaxSpeed(30);
    		BSTR petName = SysAllocString(L"Shazzam!");
    		pICreateCar->SetPetName(petName);
    		SysFreeString(petName);
    
    		// Now get IStats
    		hr = pICreateCar->QueryInterface(IID_IStats, (void**)&pStats);
    		pICreateCar->Release();
    	}
    	
    	if(SUCCEEDED(hr))
    	{
    		// Show my car!
    		pStats->DisplayStats();
    		hr = pStats->QueryInterface(IID_IEngine, (void**)&pEngine);
    	}
    
    	if(SUCCEEDED(hr))
    	{
    		int curSp = 0;
    		int maxSp = 0;		
    		pEngine->GetMaxSpeed(&maxSp);
    
    		do	// Zoom!
    		{	
    			pEngine->SpeedUp();
    			pEngine->GetCurSpeed(&curSp);
    			cout << "Speed is: " << curSp << endl;
    
    		}while(curSp <= maxSp);
    
    		// Gotta convert to char array.
    		char buff[80];
    		BSTR bstr;
    		pStats->GetPetName(&bstr);
    		WideCharToMultiByte(CP_ACP, NULL, bstr, -1, buff, 
    							80, NULL, NULL);
    		cout << buff << " has blown up! Lead Foot!" << endl << endl;
    		SysFreeString(bstr);
    
    		if(pEngine) pEngine->Release();
    		if(pStats) pStats->Release();
    	}
    
    	CoUninitialize();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Smile I registered...

    if that makes a difference between some of you helpin me out or not.... thought I'd give it a try anyway...

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    >hr = CoGetClassObject(CLSID_CoCar, CLSCTX_ALL, NULL, IID_IClassFactory, (void**)&pCF);

    You're not checking the return code hr from CoGetClassObject(), pCF may not be created, so causing a crash when used to call CreateInstance().

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    That makes sense... I changed the line below the one you posted to check to see if(SUCCEEDED(hr)) and sure enough, it jumps over that if block. So then, my next question is, why would if be that pCF is not being created?

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Check what type of error is being returned from CoGetClassObject() that should give some idea what the problem is.

    Return Values:

    S_OK
    Location and connection to the specified class object was successful.

    REGDB_E_CLASSNOTREG
    CLSID is not properly registered. Can also indicate that the value you specified in dwClsContext is not in the registry.

    E_NOINTERFACE
    Either the object pointed to by ppv does not support the interface identified by riid, or the QueryInterface operation on the class object returned E_NOINTERFACE.

    REGDB_E_READREGDB
    Error reading the registration database.

    CO_E_DLLNOTFOUND
    In-process DLL or handler DLL not found (depends on context).

    CO_E_APPNOTFOUND
    EXE not found (CLSCTX_LOCAL_SERVER only).

    E_ACCESSDENIED
    General access failure (returned from LoadLib/CreateProcess).

    CO_E_ERRORINDLL
    EXE has error in image.

    CO_E_APPDIDNTREG
    EXE was launched, but it didn't register class object (may or may not have shut down).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  2. Access violation... can't figure it out...
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2007, 10:52 AM
  3. access violation in int array
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-02-2007, 11:28 PM
  4. FtpFileFind access violation with MS VC++ 6.0
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 07:02 PM
  5. 0xC0000005: Access Violation
    By Strider in forum Windows Programming
    Replies: 3
    Last Post: 11-07-2001, 02:46 PM