在回调函数中使用函数时,C ++未知的覆盖说明符

时间:2018-07-17 06:51:15

标签: c++ windows

我现在正在使用C ++和Windows API一段时间。自从昨天Windows API要求我安装低级鼠标事件挂钩(link to hook function)以来,我从未在C ++中使用过回调。为了使我的代码更加简洁,我想将一些代码外包给一个函数,然后遇到一个我不理解的问题。我认为这与我没有完全了解回调范围之类的事情有关。我的代码看起来像这样,我使用C ++ 11和VS2017:

MouseHandler.h

#ifndef __MOUSEHANDLER_H_INCLUDED__
#define __MOUSEHANDLER_H_INCLUDED__

#include <Windows.h>

class MouseHandler
{
private:
public:
    static _bstr_t coordinatesToString(POINT point);
MouseHandler();
~MouseHandler();
};
#endif // !__MOUSEHANDLER_H_INCLUDED__

MouseHandler.cpp

#include "stdafx.h"
#include "MouseHandler.h"
#include <comutil.h>
#include <string>

using namespace std;

HHOOK mouseHook = NULL;

_bstr_t MouseHandler::coordinatesToString(POINT point)
{
    _bstr_t coordinates = "(";
    _bstr_t xcoordinate = to_string(point.x).c_str();
    _bstr_t ycoordinate = to_string(point.y).c_str();
    coordinates += xcoordinate + "," + ycoordinate + ")";

    return coordinates;
}

LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT event = *((MSLLHOOKSTRUCT *)lParam);

    if (nCode == HC_ACTION)
    {
        _bstr_t message = "Mouse action happened at position: ";
        _bstr_t mouseCoordinates = MouseHandler::coordinatesToString(event.pt);
        message += mouseCoordinates;
    }
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);
}

MouseHandler::MouseHandler()
{
    mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0);
    while (GetMessage(NULL, NULL, 0, 0) != 0);
}

MouseHandler::~MouseHandler()
{
}

当我尝试编译时,出现错误C3646 'coordinatesToString': unknown override specifier。我上下搜索,问我的同事,但是我无法获得帮助,所以希望这里有人可以帮助我。预先感谢!

2 个答案:

答案 0 :(得分:0)

  1. 确保指定正确的数据类型。
    to_string((long double)point.x).c_str();

  2. 加载库。
    #pragma comment (lib, "comsuppw.lib")

    #pragma comment (lib, "comsuppwd.lib")

  3. 在标题comutil.h中包含CMouseHandler.h

我在Win10 1803 x64上的VS2010 C ++中测试了您的代码。


CMouseHandler.h

#pragma once
#include <Windows.h>
#include <comutil.h>
#include <string>

using namespace std;

#pragma comment (lib, "comsuppw.lib")

class CMouseHandler
{
public:
    CMouseHandler(void);
    ~CMouseHandler(void);
    static _bstr_t coordinatesToString(POINT point);
};

CMouseHandler.cpp

#include "CMouseHandler.h"

HHOOK mouseHook = NULL;

_bstr_t CMouseHandler::coordinatesToString(POINT point)
{
    _bstr_t coordinates = "(";
    _bstr_t xcoordinate = to_string((long double)point.x).c_str();
    _bstr_t ycoordinate = to_string((long double)point.y).c_str();
    coordinates += xcoordinate + "," + ycoordinate + ")";

    return coordinates;
}

LRESULT CALLBACK MouseHookCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
    MSLLHOOKSTRUCT event = *((MSLLHOOKSTRUCT *)lParam);

    if (nCode == HC_ACTION)
    {
        _bstr_t message = "Mouse action happened at position: ";
        _bstr_t mouseCoordinates = CMouseHandler::coordinatesToString(event.pt);
        message += mouseCoordinates;
    }
    return CallNextHookEx(mouseHook, nCode, wParam, lParam);
}

CMouseHandler::CMouseHandler()
{
    mouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookCallback, NULL, 0);
    while (GetMessage(NULL, NULL, 0, 0) != 0);
}


CMouseHandler::~CMouseHandler(void)
{
}

答案 1 :(得分:0)

_bstr_t未定义,因为Visual Studio无法识别它假定为替代说明符的类型。您需要包含documentation中指定的comutil.h