www.pudn.com > TVToolbar_demo > TVToolDlg.cpp
// TVToolDlg.cpp : implementation file // /************************************************************** ** __ __ ** __/_/__________________________________________\_\__ ** __|_ |__ ** (___O) William SerGio & Co., Inc. (O___) **(_____O) www.codearchive.com/~sergio (O_____) **(_____O) Author: Bill SerGio (O_____) ** (__O) (O__) ** |___________________________________________________| ****************************************************************/ /* * (C) Copyright 1997-1999 by William SerGio, All Rights Reserved Worldwide. * 20547 Old Cutler Road, PMB #222, Miami, FL 33189 * www.codearchive.com/~sergio sergio@codearchive.com (305)233-7654 * * This code/software may NOT be used for any purpose whatsoever by any government, * federal agency, or, any employee or agent of any government or federal agency. * This software is not designed or intended for use in on-line control of aircraft, * air traffic, aircraft navigation or aircraft communications; or in design, * construction, operation or maintenance of any nuclear facility. Licensee * represents and warrants that it will not use or redistribute the Software for * such purposes. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR FOR EDUCATIONAL PURPOSES ONLY, * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS, * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION), * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "stdafx.h" #include "TVTool.h" #include "TVToolDlg.h" //#include "LibHeader.h" #include#define INC_OLE2 //#pragma comment( lib , "OLE2") #include #include #include /////////////////////////////////// // Required for registry stuff #include #pragma comment( lib , "advapi32") ////////////////////////////////// #define INITGUID //#include #include #define MAX_BUF 256 ////////////////////////////////////////////////////////// //////////////// REGISTER SHIT /////////////////////////// ////////////////////////////////////////////////////////// #include #include #include #define FAIL_ARGS 1 #define FAIL_OLE 2 #define FAIL_LOAD 3 #define FAIL_ENTRY 4 #define FAIL_REG 5 static char szAppName[] = "Register"; static char szUsage[] = "\n\nUsage: Register [/u] dllname"; static char szDllRegSvr[] = "DllRegisterServer"; static char szDllUnregSvr[] = "DllUnregisterServer"; ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0])) DEFINE_GUID(CLSID_TVToolbar, 0x5D570CA3,0x3123,0x11D5, 0xB8,0xD0,0xD2,0xE7,0xCA,0xE9,0x0E,0x45); BOOL FileExists(const char *pszPath){ BOOL bRet; CFileStatus status; if (CFile::GetStatus( pszPath, status )) { bRet = TRUE; } else { bRet = FALSE; } return(bRet); } ///////////////////////////////////////////////////////////////////////////// // CTVToolDlg dialog CTVToolDlg::CTVToolDlg(CWnd* pParent /*=NULL*/) : CDialog(CTVToolDlg::IDD, pParent) { //{{AFX_DATA_INIT(CTVToolDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CTVToolDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CTVToolDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CTVToolDlg, CDialog) //{{AFX_MSG_MAP(CTVToolDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() static TCHAR gszExeDir[MAX_PATH]; ///////////////////////////////////////////////////////////////////////////// // CTVToolDlg message handlers BOOL CTVToolDlg::OnInitDialog() { CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here ::CoInitialize(NULL); // Check Registry Settings SetPathways(); // READ THE REGISTRY FOR DLL(EXE) PATHWAY DWORD dwType; DWORD dwSize = MAX_PATH; HKEY hKey; // Open the appropriate registry key LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("Software\\TVToolbar\\Settings"), 0, KEY_READ, &hKey ); //if( ERROR_SUCCESS != lResult ) //return E_FAIL; lResult = RegQueryValueEx( hKey, _T("ExeDir"), NULL, &dwType, (BYTE*)gszExeDir, &dwSize ); RegCloseKey( hKey ); // Check to see if Toolbar directory is installed in Program Files TCHAR szTBFile[MAX_PATH]; lstrcpy(szTBFile, gszExeDir); lstrcat(szTBFile, _T("\\tvtoolbar.dll")); // CRITICAL TO BE SURE IT EXISTS!!! if ( !FileExists(szTBFile) ) { AfxMessageBox((LPCSTR)szTBFile); return E_FAIL; } // Places button on IE toolbar to launch our toolbar ShowButton(); LPSTR pszDll = szTBFile; Register(pszDll); // Initialize OLE libraries if (!FAILED(OleInitialize(NULL))) { SetToolButton(); CString csWebpage = "http://www.codearchive.com/~sergio"; LauchIEBrowser(csWebpage); } ::SendMessage(GetSafeHwnd(), WM_CLOSE, 0, 0); return TRUE; } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CTVToolDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CTVToolDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } /** * AUTHOR: BILL SERGIO * You can't use ShellExecute to Launch IE Browser from * OnBeforeNavigate2 or OnDocumentComplere Events!!! * Because it can result in a viscious loop and freeze!!! * Use this method to launch IE Browser from WebBrowser Control */ BOOL CTVToolDlg::LauchIEBrowser(CString csUrlPage) { try { IWebBrowser2* m_pBrowserApp = NULL; // Use ColeDispatchDriver to create automation object // This also takes care of releasing IDispatch pointer COleDispatchDriver dispIE; dispIE.CreateDispatch(CLSID_InternetExplorer); // Get IWebBrowser2 interface HRESULT hr = dispIE.m_lpDispatch->QueryInterface(IID_IWebBrowser2, reinterpret_cast (&m_pBrowserApp)); if (FAILED(hr)){ AfxMessageBox("Failed to get IWebBrowser2 interface. HRESULT"); return FALSE; } // Make it visible if (m_pBrowserApp != NULL) { COleVariant vtEmpty; COleVariant vaURL(csUrlPage); m_pBrowserApp->Navigate2(vaURL, &vtEmpty, &vtEmpty, &vtEmpty, &vtEmpty); HWND hwnd = NULL; m_pBrowserApp->get_HWND((long *) &hwnd); if ( hwnd != NULL ){ //::SetForegroundWindow(hwnd); ::ShowWindow(hwnd, SW_SHOWMAXIMIZED); } ////////////////////////// CORRECT WAY TO SHOW BAND /////////////////////// // AUTHOR: BILL SERGIO // NOTE: MSDN gives an INCORRECT example of how to do this! // You MUST pass "&vtEmpty" and NOT "0" as last parameter! // // Display Explorer ToolBar VARIANT vtBandGUID, vtShow; vtBandGUID.vt = VT_BSTR; //{5D570CA3-3123-11D5-B8D0-D2E7CAE90E45} vtBandGUID.bstrVal = SysAllocString(OLESTR("{5D570CA3-3123-11D5-B8D0-D2E7CAE90E45}")); vtShow.vt = VT_BOOL; vtShow.boolVal = true; // You MUST pass "&vtEmpty" and NOT "0" as last parameter! m_pBrowserApp->ShowBrowserBar(&vtBandGUID, &vtShow, &vtEmpty); SysFreeString(vtBandGUID.bstrVal); ///m_pBrowserApp->Release(); ///////////////////////////////////////////////////////////////////////////// dispIE.m_lpDispatch->Release(); } } catch(...){return FALSE;} return TRUE; } BOOL CTVToolDlg::RegisterButton(CLSID clsid) { HKEY hKey; LRESULT lResult; DWORD dwDisp; TCHAR szCLSID[MAX_PATH]; LPWSTR pwsz; //get CLSID in string form StringFromIID(clsid, &pwsz); if(pwsz) { #ifdef UNICODE lstrcpy(szCLSID, pwsz); #else WideCharToMultiByte( CP_ACP, 0, pwsz, -1, szCLSID, ARRAYSIZE(szCLSID), NULL, NULL ); #endif // Free string CoTaskMemFree(pwsz); } lResult = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Internet Explorer\\Toolbar\\WebBrowser"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp ); if(NOERROR == lResult) { TCHAR szData[] = TEXT(""); lResult = ::RegSetValueEx( hKey, szCLSID, 0, REG_SZ, (LPBYTE)szData, lstrlen(szData) + 1 ); ::RegCloseKey(hKey); } else return FALSE; return TRUE; } BOOL CTVToolDlg::Register(LPTSTR pszDllName) { int iReturn = 0; HRESULT (FAR STDAPICALLTYPE * lpDllEntryPoint)(void); static TCHAR szMsgBuffer[_MAX_PATH*4]; BOOL bVisualC = FALSE; BOOL bSilent = TRUE; BOOL bUnregister = FALSE; LPSTR pszDllEntryPoint = szDllRegSvr; //LPSTR pszDllName = NULL; //char szCmdLineCopy[_MAX_PATH]; //strcpy(szCmdLineCopy, pszCmdLine); //LPSTR pszTmp = szCmdLineCopy; //LPSTR pszTok; /* Utility option while ((pszTok = strtok(pszTmp, " \t")) != NULL) { pszTmp = NULL; if ((pszTok[0] == '-') || (pszTok[0] == '/')) { switch (pszTok[1]) { case 'v': case 'V': bVisualC = TRUE; break; case 's': case 'S': bSilent = TRUE; break; case 'u': case 'U': bUnregister = TRUE; pszDllEntryPoint = szDllUnregSvr; break; default: wsprintf(szMsgBuffer, "Unrecognized flag: %s%s", pszTok, (LPCSTR)szUsage); if (!bSilent) ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); return FAIL_ARGS; } } else { if (pszDllName == NULL) pszDllName = pszTok; else { wsprintf(szMsgBuffer, "Yo Dude, extra argument on command line: %s%s", pszTok, (LPCSTR)szUsage); if (!bSilent) ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); return FAIL_ARGS; } } } */ if (bUnregister) { pszDllEntryPoint = szDllUnregSvr; } if (pszDllName == NULL) { if (!bSilent) { if (bVisualC) { ::MessageBox(NULL, "This command is only valid when " "an OLE Custom Control project is open.", bUnregister ? "Unregister Control" : "Register Control", MB_TASKMODAL | MB_ICONEXCLAMATION); } else { wsprintf(szMsgBuffer, _T("No DLL name specified%s"), (LPCSTR)szUsage); ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); } } return FAIL_ARGS; } if (FAILED(OleInitialize(NULL))) { if (!bSilent) ::MessageBox(NULL, "OleInitialize failed.", szAppName, MB_TASKMODAL | MB_ICONINFORMATION); return FAIL_OLE; } HINSTANCE hLib = LoadLibrary(pszDllName); if (hLib < (HINSTANCE)HINSTANCE_ERROR) { wsprintf(szMsgBuffer, "LoadLibary(\"%s\") failed.", pszDllName); ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); iReturn = FAIL_LOAD; goto CleanupOle; } (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint); if (lpDllEntryPoint == NULL) { #ifdef _WIN32 int nLen = strlen(pszDllName); if ((nLen > 4) && (stricmp(pszDllName + nLen - 4, ".dll") != 0) && (stricmp(pszDllName + nLen - 4, ".ocx") != 0)) { wsprintf(szMsgBuffer, "%s was loaded dude, but %s entry point " "was not found? So %s does not appear to be " "an .DLL or .OCX file - Try a high colonic!", pszDllName, pszDllEntryPoint, pszDllName); } else { wsprintf(szMsgBuffer, "%s was loaded dude, but %s entry point " "was not found? So %s may not be exported, " "or a corrupt version may be in memory! " "Consider trying a high colonic dude!", pszDllName, pszDllEntryPoint, pszDllEntryPoint); } #else wsprintf(szMsgBuffer, "%s was loaded dude, but %s entry point " "was not found? So %s may not be exported, " "or a corrupt version may be in memory! " "Consider trying a high colonic dude!", pszDllName, pszDllEntryPoint, pszDllEntryPoint); #endif if (!bSilent) ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); iReturn = FAIL_ENTRY; goto CleanupLibrary; } if (FAILED((*lpDllEntryPoint)())) { wsprintf(szMsgBuffer, "%s in %s failed.", pszDllEntryPoint, pszDllName); if (!bSilent) ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONEXCLAMATION); iReturn = FAIL_REG; goto CleanupLibrary; } wsprintf(szMsgBuffer, "%s in %s succeeded.", pszDllEntryPoint, pszDllName); if (! bSilent) ::MessageBox(NULL, szMsgBuffer, szAppName, MB_TASKMODAL | MB_ICONINFORMATION); CleanupLibrary: FreeLibrary(hLib); CleanupOle: OleUninitialize(); return iReturn; } void CTVToolDlg::SetToolButton() { // delete the ITBarLayout value from the following registry key: // HKEY_CURRENT_USER // A3 0C 57 5D 23 31 D5 11 B8 D0 D2 E7 CA E9 0E 45 // {5D570CA3-3123-11D5-B8D0-D2E7CAE90E45} //DWORD BarVar = (0x5D570CA3,0x3123,0x11D5, 0xB8,0xD0,0xD2,0xE7,0xCA,0xE9,0x0E,0x45); //DWORD BarVar = {0xA3,0x0C,0x57,0x5D,0x23,0x31,0xD5,0x11,0xB8,0xD0,0xD2,0xE7,0xCA,0xE9,0x0E,0x45); //DWORD BarVar = {5D570CA3-3123-11D5-B8D0-D2E7CAE90E45}; HKEY hKey; LRESULT lResult; DWORD dwDisp; lResult = ::RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Internet Explorer\\Toolbar\\WebBrowser\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp); if(NOERROR == lResult) { TCHAR szData[] = TEXT(""); lResult = ::RegSetValueEx( hKey, TEXT("ITBarLayout"), 0, REG_SZ, (LPBYTE)szData, lstrlen(szData) + 1 ); ::RegCloseKey(hKey); } RegisterButton(CLSID_TVToolbar); } void CTVToolDlg::SetPathways() { // READ THE REGISTRY FOR DLL(EXE) PATHWAY //DWORD dwType; DWORD dwSize = MAX_PATH; HKEY hKey; LONG lResult; DWORD dwDisp; // Open the appropriate registry key lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("Software\\TVToolbar\\Settings"), 0, KEY_READ, &hKey ); if( ERROR_SUCCESS != lResult ) { lResult = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\TVToolbar\\Settings\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp ); if(NOERROR == lResult) { TCHAR szData[] = TEXT("C:\\Program Files\\TVToolbar"); lResult = ::RegSetValueEx( hKey, TEXT("ExeDir"), 0, REG_SZ, (LPBYTE)szData, lstrlen(szData) + 1 ); TCHAR szData2[] = TEXT("C:\\Program Files\\TVToolbar\\tvtoolbar.dll"); lResult = ::RegSetValueEx( hKey, TEXT("ExePath"), 0, REG_SZ, (LPBYTE)szData2, lstrlen(szData2) + 1 ); ::RegCloseKey(hKey); } lResult = ::RegCreateKeyEx( HKEY_LOCAL_MACHINE, TEXT("Software\\TVToolbar\\Shell\\"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp ); if(NOERROR == lResult) { TCHAR szData[] = TEXT("C:\\Program Files\\TVToolbar\\manual.chm"); lResult = ::RegSetValueEx( hKey, TEXT("manual"), 0, REG_SZ, (LPBYTE)szData, lstrlen(szData) + 1 ); ::RegCloseKey(hKey); } } else { ::RegCloseKey(hKey); } return; } /* * One way to force Extension buttons to be visible automatically! * * This is one approach to solving the problem of showing buttons in * the I.E. toolbar. MSDN documentation states that Extension button won't * be displayed if you have customized IE toolbar before registering the component. * You can customize IE toolbar by clicking right mouse button over it and selecting * "Customize" from context menu. Customization dialog will appear and you can add or * remove toolbar buttons or reset the toolbar so that it will contain only default * buttons. * * The following approach to showing the button in I.E. is from * Tony Juricic who recently made the observation that after * some monitoring of Registry entries during toolbar customization * he noticed that IE creates and edits the following binary value: * * HKCU\SOFTWARE\Microsoft\Internet Explorer\Toolbar\{1E796980-9CC5-11D1-A83F-00C04FC99D61} * * Tony Juricic verified this behavior in both IE 5.0 on Win95 and IE 6.0 on Win2000. * * If you press Reset in customization dialog IE will delete this value. You can also * manually delete it using Regedit and the next time you start IE its main toolbar * will show only default buttons. This gives us one, somewhat crude but simple, way * to solve "automatic appearance problem" : we can simply delete this value when we * register our IE Extension component! Otherwise, user will have to look for our * Extension button in customization dialog and MANUALLY add it to the toolbar. * If you ant to do this using the ATL Regstrar component, then you must get around * the problem that the ATL regstrar can delete only Registry keys, not values. * The following script, suggested by Tony Juricic will do the job - even if it does * not delete binary value, it makes IE think that toolbar has not been customized. * * HKCU * { * SOFTWARE * { * Microsoft * { * 'Internet Explorer' * { * NoRemove Toolbar * { * val {1E796980-9CC5-11D1-A83F-00C04FC99D61} = b 00000000 * } * } * } * } * } * */ void CTVToolDlg::ShowButton() { // // AUTHOR: BILL SERGIO // How to implement Tony Juricic's approach: // // None of this will work if IE is running while user is registering Extension // component. Intaller or component startup code should check if IE is running // and CLOSE all instances of IE first before making this registry entry. // If you want to do this correctly, add code here to place // a list of pointers to the running IE Frames and CLOSE each // running instances of IE. Then execute the registry entry: // val {1E796980-9CC5-11D1-A83F-00C04FC99D61} = b 00000000 }