newntui/activex/spreadsheet/src/msflexgrid.cpp
///////////////////////////////////////////////////////////////////////
//  SAMPLE CODE - Implementation of the RequestLicenseKey function
//
//  The RequestLicenseKey function uses the IClassFactory2 interface
//  to request a license key from an object specified by its ProgID.
//
//  Parameters:
//
//	  [out] BSTR& bstrLicenseKey
//			Upon return this BSTR will contain either a valid
//			license key or an error message. It is the callers
//          responsibility to call ::SysFreeString on this BSTR.
//
//	  [in]  CString strProgID
//			Specifies the ProgID of the object to request the license
//			key from.
//
//  Return Value:
//
//    A BOOL specifying success or failure. If TRUE is returned
//    the license key was retrieved successfully and the
//    bstrLicenseKey parameter contains a valid license key. If
//    FALSE is returned the license key was not retrieved
//    successfully and the bstrLicenseKey parameter contains a
//    descriptive error string.
//
//    Regardless of the return value, it is the responsibility of
//    the caller to call ::SysFreeString on the returned bstrLicenseKey
//    parameter.
//
SAMPLE CODE - Implementation of the RequestLicenseKey function
The RequestLicenseKey function uses the IClassFactory2 interface
to request a license key from an object specified by its ProgID.



[out] BSTR& bstrLicenseKey
Upon return this BSTR will contain either a valid
license key or an error message. It is the callers
responsibility to call ::SysFreeString on this BSTR.

[in]  CString strProgID
Specifies the ProgID of the object to request the license
key from.

Return Value.

A BOOL specifying success or failure. If TRUE is returned
the license key was retrieved successfully and the
bstrLicenseKey parameter contains a valid license key. If
FALSE is returned the license key was not retrieved
successfully and the bstrLicenseKey parameter contains a
descriptive error string.

Regardless of the return value, it is the responsibility of
the caller to call ::SysFreeString on the returned bstrLicenseKey
parameter.
SAMPLE CODE - Implementation of the RequestLicenseKey function The RequestLicenseKey function uses the IClassFactory2 interface to request a license key from an object specified by its ProgID . [ out ] BSTR& bstrLicenseKey Upon return this BSTR will contain either a valid license key or an error message . It is the callers responsibility to call :: SysFreeString on this BSTR . [ in ] CString strProgID Specifies the ProgID of the object to request the license key from . Return Value . A BOOL specifying success or failure . If TRUE is returned the license key was retrieved successfully and the bstrLicenseKey parameter contains a valid license key . If FALSE is returned the license key was not retrieved successfully and the bstrLicenseKey parameter contains a descriptive error string . Regardless of the return value it is the responsibility of the caller to call :: SysFreeString on the returned bstrLicenseKey parameter .
BOOL RequestLicenseKey(BSTR& bstrLicenseKey, CString strProgID)
{
	USES_CONVERSION;
	LPCLASSFACTORY2 pClassFactory;
	CLSID clsid;
	BOOL bValidKeyReturned = FALSE;
	if (SUCCEEDED(CLSIDFromProgID(T2OLE(strProgID), &clsid)))
	{
		if (SUCCEEDED(CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL,
			IID_IClassFactory2, (LPVOID *)(&pClassFactory))))
        {
			LICINFO licinfo;
            if (SUCCEEDED(pClassFactory->GetLicInfo(&licinfo)))
			{
				if (licinfo.fRuntimeKeyAvail)
				{
					HRESULT hr;
					hr = pClassFactory->RequestLicKey(0, &bstrLicenseKey);
					if (SUCCEEDED(hr))
					{
						if(bstrLicenseKey == NULL)
                            bstrLicenseKey = ::SysAllocString(
                                L"<Object returned a NULL license key>");
						else
							bValidKeyReturned = TRUE;
					}
					else
						switch(hr)
						{
							case E_NOTIMPL:
								bstrLicenseKey = ::SysAllocString(
                                    L"<The object's class factory does not support"
                                    L" run-time license keys>");
								break;
							case E_UNEXPECTED:
								bstrLicenseKey = ::SysAllocString(
                                    L"<An unexpected error occurred when requesting"
                                    L" the run-time license key>");
								break;
							case E_OUTOFMEMORY:
								bstrLicenseKey = ::SysAllocString(
                                    L"<The object's class factory was unable to"
                                    L" allocate the license key>");
								break;
							case CLASS_E_NOTLICENSED:
								bstrLicenseKey = ::SysAllocString(
                                    L"<The object's class factory supports run-time"
                                    L" licensing, but the current machine\r\nitself"
                                    L" is not licensed. Thus, a run-time key is not"
                                    L" available on this machine>");
								break;
							default:
								bstrLicenseKey = ::SysAllocString(
                                    L"<An unknown error occurred when requesting"
                                    L" the license key>");
						}
				}
				else
					bstrLicenseKey = ::SysAllocString(
                        L"<The object has no runtime license key>");
			}
			else
				bstrLicenseKey = ::SysAllocString(
                    L"<Unable to get the licensing capabilities of the"
                    L" object's class factory>");
			pClassFactory->Release();
		}
		else
			bstrLicenseKey = ::SysAllocString(
                L"<Unable to get the IClassFactory2 interface from the specified object>");
	}
	else
		bstrLicenseKey = ::SysAllocString(
            L"<Unable to get the CLSID of the specified object>");
	return bValidKeyReturned;
}
BOOL RequestLicenseKey ( BSTR & bstrLicenseKey , CString strProgID ) { USES_CONVERSION ; LPCLASSFACTORY2 pClassFactory ; CLSID clsid ; BOOL bValidKeyReturned = FALSE ; if ( SUCCEEDED ( CLSIDFromProgID ( T2OLE ( strProgID ) , & clsid ) ) ) { if ( SUCCEEDED ( CoGetClassObject ( clsid , CLSCTX_INPROC_SERVER , NULL , IID_IClassFactory2 , ( LPVOID * ) ( & pClassFactory ) ) ) ) { LICINFO licinfo ; if ( SUCCEEDED ( pClassFactory -> GetLicInfo ( & licinfo ) ) ) { if ( licinfo . fRuntimeKeyAvail ) { HRESULT hr ; hr = pClassFactory -> RequestLicKey ( 0 , & bstrLicenseKey ) ; if ( SUCCEEDED ( hr ) ) { if ( bstrLicenseKey == NULL ) bstrLicenseKey = :: SysAllocString ( L" " ) ; else bValidKeyReturned = TRUE ; } else switch ( hr ) { case E_NOTIMPL : bstrLicenseKey = :: SysAllocString ( L" " L" " ) ; break ; case E_UNEXPECTED : bstrLicenseKey = :: SysAllocString ( L" " L" " ) ; break ; case E_OUTOFMEMORY : bstrLicenseKey = :: SysAllocString ( L" " L" " ) ; break ; case CLASS_E_NOTLICENSED : bstrLicenseKey = :: SysAllocString ( L" " L" \r \n " L" " L" " ) ; break ; default : bstrLicenseKey = :: SysAllocString ( L" " L" " ) ; } } else bstrLicenseKey = :: SysAllocString ( L" " ) ; } else bstrLicenseKey = :: SysAllocString ( L" " L" " ) ; pClassFactory -> Release ( ) ; } else bstrLicenseKey = :: SysAllocString ( L" " ) ; } else bstrLicenseKey = :: SysAllocString ( L" " ) ; return bValidKeyReturned ; }
SAMPLE CODE - Implementation of the RequestLicenseKey function
The RequestLicenseKey function uses the IClassFactory2 interface
to request a license key from an object specified by its ProgID.
SAMPLE CODE - Implementation of the RequestLicenseKey function The RequestLicenseKey function uses the IClassFactory2 interface to request a license key from an object specified by its ProgID .
// Get the CLSID of the specified ProgID // Create an instance of the object and query it for //  the IClassFactory2 interface. // Check to see if this object has a runtime license key // The object has a runtime license key so request it // We got the license key! // Requesting the license key failed // Make sure we release the reference to the class factory // Return a BOOL specifying whether or not we were able to get a //  valid license key