# Highlight line numbers in listings, conflict with hyperref package

I want to highlight the line numbers of the lines of the Listing to which I refer. See the question Highlight line numbers in Listings for details about how to highlight one line number.

Now I want to highlight multiple line numbers. The pasted code works, unless you uncomment the inclusion of the hyperref package.

Does anybody know how to get this to work?

BTW: Hyperref conflicts also with the solution of 1.

BTW2: Without the highlighting hyperref causes no problems.

\documentclass{article}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\usepackage{listings}

% define C++ style
\lstdefinestyle{cppStyle}
{
basicstyle=\footnotesize,
tabsize=2,
captionpos=b,
frame=lines,
breaklines=true,
% language related
language=C++,
keywordstyle=\color{blue},
stringstyle=\color{mauve},
% numbering
numbers=left,
numberstyle=\tiny
}

\makeatletter
\newcommand\labelline[1]{%
\def\@currentlabel{\thelstnumber}\label{#1}}

\newcommand\lineref[1]{
\@ifundefined{r@#1}{0}{\ref{#1}}
}
\makeatother

%\usepackage{hyperref}

\begin{document}
IOdictionary ppProperties
(
IOobject
(
"ppProperties",
runTime.constant(),
mesh,
IOobject::NO_WRITE (*@\label{code:ioObjectWriteFlag}@*)
)
);
\end{lstlisting}
\end{document}


The key is to use \getrefnumber from refcount instead of your \lineref, because \ref doesn't expand to a number, but is much more complicated when hyperref is involved, as it wants to create links.

\documentclass{article}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\usepackage{listings}
\usepackage{refcount}
\usepackage{hyperref}

% define C++ style
\lstdefinestyle{cppStyle}
{
basicstyle=\footnotesize,
tabsize=2,
captionpos=b,
frame=lines,
breaklines=true,
% language related
language=C++,
keywordstyle=\color{blue},
stringstyle=\color{mauve},
% numbering
numbers=left,
numberstyle=\tiny
}

\makeatletter
\newcommand\labelline[1]{%
\def\@currentlabel{\thelstnumber}\label{#1}}
\makeatother

\begin{document}
\begin{lstlisting}[
style=cppStyle,
escapeinside={(*@}{@*)},
caption={Definition of a dictionary in \texttt{readPPProperties.H}},
label=lst:createDictionaryIOobject,
numberstyle={%
\color{green}%
\else
\ifnum\value{lstnumber}=\getrefnumber{code:ioObjectWriteFlag}%
\color{green}
\fi
\fi
}
]
IOdictionary ppProperties
(
IOobject
(
"ppProperties",
runTime.constant(),
mesh,