void CGUISetupFrame::writeDeskTop(const wxString& name, const wxString& dir) { TCHAR folder[MAX_PATH]; BOOL res = ::SHGetSpecialFolderPath(NULL, folder, CSIDL_DESKTOP, FALSE); if (!res) { ::wxMessageBox(_("Cannot get the Desktop folder from Windows"), _("GUISetup Error"), wxICON_ERROR); return; } wxString linkName = name + wxT(".lnk"); wxString linkPath = wxString(folder) + wxT("\\") + linkName; wxString exePath = dir + wxT("\\UWSDR.exe"); wxString args = name; HRESULT hRes = ::CoInitialize(NULL); if (!SUCCEEDED(hRes)) { ::wxMessageBox(_("Cannot initialise the COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } IShellLink* pShellLink; hRes = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink); if (!SUCCEEDED(hRes)) { ::CoUninitialize(); ::wxMessageBox(_("Cannot create a COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } pShellLink->SetPath(exePath.c_str()); pShellLink->SetArguments(args.c_str()); pShellLink->SetWorkingDirectory(dir.c_str()); TCHAR wszLinkfile[MAX_PATH]; ::MultiByteToWideChar(CP_ACP, 0, linkPath.mb_str(), -1, wszLinkfile, MAX_PATH); IPersistFile* pPersistFile; hRes = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile); if (!SUCCEEDED(hRes)) { pShellLink->Release(); ::CoUninitialize(); ::wxMessageBox(_("Cannot query the COM interface"), _("GUISetup Error"), wxICON_ERROR); return; } hRes = pPersistFile->Save(LPCOLESTR(wszLinkfile), TRUE); if (!SUCCEEDED(hRes)) ::wxMessageBox(_("Cannot save the shortcut file"), _("GUISetup Error"), wxICON_ERROR); pPersistFile->Release(); pShellLink->Release(); ::CoUninitialize(); }
//***************************************************************************** //* Function Name: ThrowComErrorException //* Description: //***************************************************************************** void ThrowComErrorException ( LPCTSTR p_lpszFile, UINT p_uLine, HRESULT p_hr, const _bstr_t& p_sbstrDescription) { USES_CONVERSION; CLSID l_clsid = GUID_NULL; IID l_iid = GUID_NULL; CActiveMethodGUIDs::Get (l_clsid, l_iid); // AtlReportError() creates and populates a standard COM error object // and calls SetErrorInfo() to pass the COM error object to the COM // run-time. (void) AtlReportError ( l_clsid, // rclsid p_sbstrDescription.operator LPCOLESTR (), // lpszDesc l_iid, // riid p_hr); // hRes // At this point, the COM run-time has the only reference to the error object. IErrorInfoPtr l_spErrorInfo; // From MSDN re GetErrorInfo(): // // "This function returns a pointer to the most recently set IErrorInfo // pointer in the current logical thread. It transfers ownership of the // error object to the caller, and clears the error state for the thread." if (SUCCEEDED (::GetErrorInfo (0 /* dwReserved */, &l_spErrorInfo))) { // At this point, the COM run-time's reference to the error object has // been transferred to l_spErrorInfo. Specify true for the last parameter // of the _com_error constructor to make sure that it calls AddRef() on // the error object. This last parameter is optional and defaults to // false. If true is *not* specified then the _com_error constructor // won't call AddRef() so the only reference will be the one in // l_spErrorInfo which will be released when l_spErrorInfo goes out of // scope i.e. when the _com_error exception is thrown. This will result // in the error object being prematurely destroyed. By passing true, // the only outstanding reference to the error object will be inside // the _com_error exception object which is as it should be. throw _com_error (p_hr, l_spErrorInfo, true /* fAddRef */); } // There was no rich error information so just use the HRESULT. throw _com_error (p_hr); }
JNIEXPORT jlong JNICALL Java_com4j_Native_createInstance( JNIEnv* env, jclass __unused__, jstring _progId, jint clsctx, jlong iid1, jlong iid2 ) { MyGUID iid(iid1,iid2); CLSID clsid; HRESULT hr; JString progId(env,_progId); hr = CLSIDFromProgID(progId,&clsid); if(FAILED(hr)) { if(FAILED(CLSIDFromString( const_cast<LPOLESTR>(LPCOLESTR(progId)),&clsid))) { error(env,__FILE__,__LINE__,hr,"Unrecognized CLSID"); return 0; } } void* p; if(clsctx&(CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER)) { IUnknown* pUnk = NULL; hr = CoCreateInstance(clsid,NULL,clsctx,__uuidof(IUnknown),(void**)&pUnk); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"CoCreateInstance failed"); return 0; } hr = OleRun(pUnk); if(FAILED(hr)) { pUnk->Release(); error(env,__FILE__,__LINE__,hr,"OleRun failed"); return 0; } hr = pUnk->QueryInterface(iid,&p); pUnk->Release(); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"QueryInterface failed"); return 0; } } else { // just the plain CoCreateInstance hr = CoCreateInstance(clsid,NULL,clsctx,iid,&p); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"CoCreateInstance failed"); return 0; } } return reinterpret_cast<jlong>(p); }