//////////////////////////////////////////////////////////////////////////// // // Written by George Cross, Borland C++ Developer Support, April 1997 // // Copyright (c) 1997 Borland International Inc. All Rights Reserved. // #include #include #include #include #define BUFFERSIZE 256 //////////////////////////////////////////////////////////////////////////// // I put this GUID in since it wasn't in the import libs I was linking to. // Remove it if the linker complains. extern "C" const GUID IID_IClassFactory2 = {0xB196B28F,0xBAB4,0x101A, {0xB6,0x9C,0x00,0xAA,0x00,0x34,0x1D,0x07}}; //////////////////////////////////////////////////////////////////////////// void OfferHelp(_TCHAR** ppszCmdLine) { if (ppszCmdLine[1] != NULL && (0 == lstrcmpi(ppszCmdLine[1],_TEXT("-?")) || 0 == lstrcmpi(ppszCmdLine[1],_TEXT("?")) || 0 == lstrcmpi(ppszCmdLine[1],_TEXT("/?")) || 0 == lstrcmpi(ppszCmdLine[1],_TEXT("/help")) || 0 == lstrcmpi(ppszCmdLine[1],_TEXT("-help")))){ _tprintf(_TEXT("\nOCX License Generator. \n\n \ Given an OLE server's progid, this utility extracts any \n \ license information and saves it to a binary file. \n \ Syntax: ocxlicen \n \ eg. ocxlicen \n \ - you will be prompted for the progid, \n \ default filename is OCX.LIC\n \ \n \ eg. ocxlicen VSPELLER.VSpellCtrl.1 \n \ - progid used as specified; default filename is OCX.LIC \n \ \n \ eg. ocxlicen VSPELLER.VSpellCtrl.1 vspeller.lic \n \ - progid used as specified; output filename as specified\n")); exit(EXIT_SUCCESS); } } //////////////////////////////////////////////////////////////////////////// void HandleError(_TCHAR* pszMsg = 0, DWORD dwMsgID = GetLastError()) { if (pszMsg){ _tprintf(pszMsg); } else{ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwMsgID, 0, (LPTSTR)&pszMsg, 0, NULL); _tprintf(pszMsg); LocalFree(pszMsg); } _gettchar(); OleUninitialize(); return; } //////////////////////////////////////////////////////////////////////////// _TCHAR* GetProgID(_TCHAR* pszPID) { _tprintf(_TEXT("What's the ProgID? ")); _tscanf(_TEXT("%s"),pszPID); fflush(stdin); return pszPID; } //////////////////////////////////////////////////////////////////////////// extern "C" void _tmain(int, _TCHAR* argv[]) { OfferHelp(argv); if (FAILED(OleInitialize(NULL))){ _tprintf(_TEXT("OleInitialize() failed.\n")); } CLSID clsid; OLECHAR pszProgID[BUFFERSIZE*sizeof(_TCHAR)]; if (argv[1] != NULL){ lstrcpy(pszProgID, argv[1]); } else{ GetProgID(pszProgID); } HRESULT hr; hr = CLSIDFromProgID(pszProgID, &clsid); if (FAILED(hr)){ _TCHAR* buf = new _TCHAR[BUFFERSIZE*sizeof(_TCHAR)]; _stprintf(buf, _TEXT("CLSIDFromProgID(%s) failed.\n"), pszProgID); HandleError(buf); delete[] buf; return; } IClassFactory2* pCF; hr = CoGetClassObject(clsid, CLSCTX_ALL, NULL, IID_IClassFactory2, (void**) &pCF); if (FAILED(hr)){ HandleError(0, hr); return; } OLECHAR szLic[BUFFERSIZE*sizeof(OLECHAR)]; BSTR bstrKey = SysAllocString(szLic); if (FAILED(pCF->RequestLicKey(NULL, &bstrKey))){ SysFreeString(bstrKey); HandleError(_TEXT("No License info obtained")); return; } HANDLE hfile = CreateFile(argv[2] ? argv[2] : _TEXT("ocx.lic"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0); if (hfile == INVALID_HANDLE_VALUE){ SysFreeString(bstrKey); HandleError(); return; } unsigned long int nbytes; if (!WriteFile(hfile, bstrKey, SysStringByteLen(bstrKey), &nbytes, NULL)){ SysFreeString(bstrKey); HandleError(); return; } OleUninitialize(); return; }