Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:05:14+00:00 2026-05-23T23:05:14+00:00

I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward

  • 0

I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward declare then define in the file(along with some classes that contain static variables). I then define(implement/create?) the static class variables in the associated .cpp file (hook.cpp).

Finally I include the header file in my stdafx.h file so I can use it in my program.

Because I include the hook.h file twice I get a compilation error that the LRESULT CALLBACK function is defined twice, the error is:

stdafx.obj : error LNK2005: "long __stdcall LowLevelKeyboardProc(int,unsigned int,long)" (?LowLevelKeyboardProc@@YGJHIJ@Z) already defined in main.obj
1>main.obj : error LNK2001: unresolved external symbol "protected: static class LowLevelKeyboardHookEx * LowLevelKeyboardHookEx::instance" (?instance@LowLevelKeyboardHookEx@@1PAV1@A)
1>C:\Users\Soribo\Dropbox\C++ Programming\Visual C++ Programming\Key Cataloguer\Release\Key Cataloguer.exe : fatal error LNK1120: 1 unresolved externals

How can I avoid this issue?

My header file:

#ifndef KEYBOARDHOOK_H
#define KEYBOARDHOOK_H

#include "stdafx.h"

LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam );

class MyClass {
    public:
      static std::string instanceStr;
      // further down this class it refers to the function KeyboardProc() thus need for forward declaration
};

LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam )
{
  // implements function
}

#endif

My hook.cpp file:

#include "stdafx.h"
#include "hook.h"

std::string MyClass::instanceStr = "";

My stdafx.h file:

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>

// Application Specific Includes
#include <string>
#include "hook.h"       // I think this is the cause of the error because I include this file twice in compilation which means that the LRESULT function is redefined/reimplemented

I have also tried not including the hook.h file in hook.cpp & just including stdafx.h but I get the same issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:05:14+00:00Added an answer on May 23, 2026 at 11:05 pm

    Move your definition of KeyboardProc (or is it LowLevelKeyboardProc?) into hook.cpp. That is, move this code from hook.h into hook.cpp

    LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam )
    {
      // implements function
    }
    

    But leave your “declaration” of KeybaordProc in hook.h. That is, leave this line inside hook.h:

    LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam );
    

    That way, any other source file can reference KeyboardProc, but only one definition of the function is linked in.

    There are some alternative solutions including some “inline” keywords and possibly a pragma/declspec directive to make it work. Neither seems right for what you want to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added a keyboard hook for a dialog that I don't control (it
I have a script that appends some rows to a table. One of the
I am starting doing some directX programming. I am using this tutorial that I
Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal
have a table that dynamically generates text boxes in run time. I want to
Have a build process that can't be edited and need to pack another war
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a new web app that is packaged as a WAR as part
i have a input tag which is non editable, but some times i need
hello I have the following win32 program, and I have an EDITTEXT control that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.