Link Errors in Static Link 
Author Message
 Link Errors in Static Link

Hi.

Paul DiLascia writes a column at www.microsoft.com/msj.  He wrote a small
static link control - it's just a hyperlink that sits in the middle of a
dialog box.  It's rather snazzy, so I decided to include it in my work.
All I did was start a new class with the same name as Paul's class, then
copied the code over into my .cpp and .h file.  I also copied an extra .h
file that was required.  I cleaned up a bit of the #ifdef business.  It
now doesn't link.  It says:

StaticLink.obj : error LNK2001: unresolved external symbol "public: struct
HINSTANCE__ * __thiscall CHyperlink::Navigate(void)"

Now, in the sample code, his project settings include rasapi32.lib and
oldnames.lib in the linker bit.  I've copied them into my own project
settings.  What's the problem?

The header file that has the problem is here:

////////////////////////////////////////////////////////////////
// 1998 Microsoft Systems Journal
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// CHyperlink implements a simple text hyperlink
//
#ifndef _HYPRILNK_H
#define _HYPRILNK_H

//////////////////
// Simple text hyperlink derived from CString
//
class CHyperlink : public CString {
public:
        CHyperlink(LPCTSTR lpLink = NULL) : CString(lpLink) { }
        ~CHyperlink() { }
        const CHyperlink& operator=(LPCTSTR lpsz) {
                CString::operator=(lpsz);
                return *this;
        }
        operator LPCTSTR() {
                return CString::operator LPCTSTR();
        }
        HINSTANCE Navigate();

Quote:
};

#endif

The other two files are StatLink.cpp:

////////////////////////////////////////////////////////////////
// PixieLib(TM) Copyright 1997 Paul DiLascia
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
// CStaticLink implements a static control that's a hyperlink
// to any file on your desktop or web. You can use it in dialog boxes
// to create hyperlinks to web sites. When clicked, opens the file/URL
//
#include "StdAfx.h"
#include "StatLink.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

COLORREF CStaticLink::g_colorUnvisited = RGB(0,0,255);           // blue
COLORREF CStaticLink::g_colorVisited   = RGB(128,0,128);
// purple

HCURSOR CStaticLink::g_hCursorLink = NULL;

IMPLEMENT_DYNAMIC(CStaticLink, CStatic)

BEGIN_MESSAGE_MAP(CStaticLink, CStatic)
        ON_WM_NCHITTEST()
        ON_WM_CTLCOLOR_REFLECT()
        ON_WM_LBUTTONDOWN()
        ON_WM_SETCURSOR()
END_MESSAGE_MAP()

///////////////////
// Constructor sets default colors = blue/purple.
// bDeleteOnDestroy is used internally by PixieLib in CPixieDlg.
//
CStaticLink::CStaticLink(LPCTSTR lpText, BOOL bDeleteOnDestroy)
{
        m_link = lpText;
// link text (NULL ==> window text)
        m_color = g_colorUnvisited;                             // not
visited yet
        m_bDeleteOnDestroy = bDeleteOnDestroy;  // delete object with
window?

Quote:
}

//////////////////
// Normally,    a static control does not get mouse events unless it has
// SS_NOTIFY. This achieves the same effect as SS_NOTIFY, but it's fewer
// lines of code and more reliable than turning on SS_NOTIFY in OnCtlColor
// because Windows doesn't send WM_CTLCOLOR to bitmap static controls.
//
UINT CStaticLink::OnNcHitTest(CPoint point)
{
        return HTCLIENT;

Quote:
}

//////////////////
// Handle reflected WM_CTLCOLOR to set custom control color.
// For a text control, use visited/unvisited colors and underline font.
// For non-text controls, do nothing. Also ensures SS_NOTIFY is on.
//
HBRUSH CStaticLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
        ASSERT(nCtlColor == CTLCOLOR_STATIC);
        DWORD dwStyle = GetStyle();

        HBRUSH hbr = NULL;
        if ((dwStyle & 0xFF) <= SS_RIGHT) {

                // this is a text control: set up font and colors
                if (!(HFONT)m_font) {
                        // first time init: create font
                        LOGFONT lf;
                        GetFont()->GetObject(sizeof(lf), &lf);
                        lf.lfUnderline = TRUE;
                        m_font.CreateFontIndirect(&lf);
                }

                // use underline font and visited/unvisited colors
                pDC->SelectObject(&m_font);
                pDC->SetTextColor(m_color);
                pDC->SetBkMode(TRANSPARENT);

                // return hollow brush to preserve parent background color
                hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
        }
        return hbr;

Quote:
}

/////////////////
// Handle mouse click: navigate link
//
void CStaticLink::OnLButtonDown(UINT nFlags, CPoint point)
{
        if (m_link.IsEmpty()) {         // if URL/filename not set..
                GetWindowText(m_link);  // ..get it from window text
                if (m_link.IsEmpty())
                        return;
        }

        // Call ShellExecute to run the file.
        // For an URL, this means opening it in the browser.
        //
        HINSTANCE h = m_link.Navigate();
        if ((UINT)h > 32) {
// success!
                m_color = g_colorVisited;                        // change
color
                Invalidate();
// repaint
        } else {
                MessageBeep(0);         // unable to execute file!
                TRACE(_T("*** WARNING: CStaticLink: unable to navigate
link %s\n"),
                        (LPCTSTR)m_link);
        }

Quote:
}

//////////////////
// Set "hand" cursor to cue user that this is a link. If app has not set
// g_hCursorLink, then try to get the cursor from winhlp32.exe,
// resource 106, which is a pointing finger. This is a bit of a kludge,
// but it works.
//
BOOL CStaticLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
        if (g_hCursorLink == NULL) {
                static bTriedOnce = FALSE;
                if (!bTriedOnce) {
         CString windir;
         GetWindowsDirectory(windir.GetBuffer(MAX_PATH), MAX_PATH);
         windir.ReleaseBuffer();
         windir += _T("\\winhlp32.exe");
         HMODULE hModule = LoadLibrary(windir);
                        if (hModule) {
                                g_hCursorLink =
                                        CopyCursor(::LoadCursor(hModule,
MAKEINTRESOURCE(106)));
                        }
                        FreeLibrary(hModule);
                        bTriedOnce = TRUE;
                }
        }
        if (g_hCursorLink) {
                ::SetCursor(g_hCursorLink);
                return TRUE;
        }
        return FALSE;

Quote:
}

//////////////////
// Normally, a control class is not destoyed when the window is;
// however, CPixieDlg creates static controls with "new" instead of
// as class members, so it's convenient to allow the option of destroying
// object with window. In applications where you want the object to be
// destoyed along with the window, you can call constructor with
// bDeleteOnDestroy=TRUE.
//
void CStaticLink::PostNcDestroy()
{
        if (m_bDeleteOnDestroy)
                delete this;

Quote:
}

and StatLink.h:

////////////////////////////////////////////////////////////////
// 1998 Microsoft Systems Journal
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
//
//
// CStaticLink implements a static control that's a hyperlink
// to any file on your desktop or web. You can use it in dialog boxes
// to create hyperlinks to web sites. When clicked, opens the file/URL
//
#ifndef _STATLINK_H
#define _STATLINK_H

#include "HyprLink.h"

class CStaticLink : public CStatic {
public:
        DECLARE_DYNAMIC(CStaticLink)
        CStaticLink(LPCTSTR lpText = NULL, BOOL bDeleteOnDestroy=FALSE);
        ~CStaticLink() { }

        // Use this if you want to subclass and also set different URL
        BOOL SubclassDlgItem(UINT nID, CWnd* pParent, LPCTSTR
lpszLink=NULL) {
                m_link = lpszLink;
                return CStatic::SubclassDlgItem(nID, pParent);
        }

        // Hyperlink contains URL/filename. If NULL, I will use the window
text.
        // (GetWindowText) to get the target.
        CHyperlink      m_link;
        COLORREF                m_color;

        // Default colors you can change
        // These are global, so they're the same for all links.
        static COLORREF g_colorUnvisited;
        static COLORREF g_colorVisited;

        // Cursor used when mouse is on a link--you can set, or
        // it will default to the standard hand with pointing finger.
        // This is global, so it's the same for all links.
        static HCURSOR   g_hCursorLink;

protected:
        CFont                   m_font;                                 //
underline font for text control
        BOOL                    m_bDeleteOnDestroy;     // delete object
when window destroyed?

        virtual void PostNcDestroy();

        // message handlers
        DECLARE_MESSAGE_MAP()
        afx_msg UINT    OnNcHitTest(CPoint point);
        afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
        afx_msg void    OnLButtonDown(UINT nFlags, CPoint point);
        afx_msg BOOL    OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT
message);

Quote:
};

#endif _STATLINK_H

-------------------------------------------------------------

-------------------------------------------------------------



Wed, 14 Jul 2004 14:54:38 GMT  
 Link Errors in Static Link
Sorry.

The problem was simply that I hadn't included the implementation of the
function, just the declaration.

For those who are interested, the MSJ has a lot of neat things in it.  And
thanks, Paul, wherever you are.

-------------------------------------------------------------

-------------------------------------------------------------



Wed, 14 Jul 2004 18:30:15 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Link errors during static link

2. VC 4.2 link error linking static libraries from VC 6.0

3. Error linking static MFC and static C/C++ runtime

4. Linking Error - Static vs. Non-Static

5. dynamic link to static link...

6. dynamic link -> static link

7. New vs.net static library project generates LINK errors

8. Linking Error when using a static variable from a DLL

9. help: link error for static data type

10. New vs.net static library project generates LINK errors

11. Error when use static linking library ?

12. Static library link error in VC++ V5

 

 
Powered by phpBB® Forum Software