GetProcAddress() Sample Generates C2106 Using C++

Last reviewed: July 22, 1997
Article ID: Q117869
1.00 1.50 1.51 1.52 MS-DOS kbprg

The information in this article applies to:

  • Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52

Error message C2106, "left operand must be l-value", is generated when you compile a C++ application that contains the sample code provided with the printed and online documentation for the Windows API GetProcAddress().

This is to be expected in a C++ application. According to Stroustrup's "The Annotated C++ Reference Manual," section 4.0, "Standard Conversions": "The result of a conversion is an lvalue only if the result is a reference."

The C2106 error message is generated on the following line of sample code:

   (FARPROC) lpfnTimerCount =
      GetProcAddress(hinstToolHelp, "TimerCount");

where lpfnTimerCount is defined as follows:

   BOOL (FAR * lpfnTimerCount) (TIMERINFO FAR*);

The error can be eliminated by using a typecast on the right side of the equation instead of on the left side. The following code fragment demonstrates how to cast the function pointer defined in the sample code so that the sample code compiles without error:

   lpfnTimerCount =
      (BOOL (FAR *)(TIMERINFO FAR*))GetProcAddress(hinstToolHelp,
      "TimerCount");


Additional reference words: kbinf 1.00 1.50
KBCategory: kbprg
KBSubcategory: CPPLngIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.