首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法编译Microsoft /ActiveX示例

无法编译Microsoft /ActiveX示例
EN

Stack Overflow用户
提问于 2014-08-29 16:04:35
回答 1查看 1.1K关注 0票数 0

我正试图从微软(链接到源)编译以下COM/ActiveX示例,但遇到一些无法修复的错误:

代码语言:javascript
复制
// evh_server.h
#pragma once

[dual, uuid("00000000-0000-0000-0000-000000000001")]
__interface IEvents {
    [id(1)] HRESULT MyEvent([in] int value);
};

[dual, uuid("00000000-0000-0000-0000-000000000002")]
__interface IEventSource {
    [id(1)] HRESULT FireEvent();
};

class DECLSPEC_UUID("530DF3AD-6936-3214-A83B-27B63C7997C4") CSource;


// evh_server.cpp
// compile with: /LD
// post-build command: Regsvr32.exe /s evh_server.dll
#define _ATL_ATTRIBUTES 1
#include <atlbase.h>
#include <atlcom.h>
#include "evh_server.h"

[module(dll, name = "EventSource", uuid = "6E46B59E-89C3-4c15-A6D8-B8A1CEC98830")];

[coclass, event_source(com), uuid("530DF3AD-6936-3214-A83B-27B63C7997C4")]     // <-- Error C3745: 'void VariantInit(VARIANTARG *)': only an event can be 'raised'
class CSource : public IEventSource {
public:
    __event __interface IEvents;      // <-- Error: interface types cannot be nested class types

    HRESULT FireEvent() {
        __raise MyEvent(123);         // <-- Error: identifier "MyEvent" is undefined
        return S_OK;
    }
};


// evh_client.cpp
// compile with: /link /OPT:NOREF
#define _ATL_ATTRIBUTES 1
#include <atlbase.h>
#include <atlcom.h>
#include <stdio.h>
#include "evh_server.h"

[module(name = "EventReceiver")];

[event_receiver(com)]
class CReceiver {
public:
    HRESULT MyHandler1(int nValue) {
        printf_s("MyHandler1 was called with value %d.\n", nValue);
        return S_OK;
    }

    HRESULT MyHandler2(int nValue) {
        printf_s("MyHandler2 was called with value %d.\n", nValue);
        return S_OK;
    }

    void HookEvent(IEventSource* pSource) {
        __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
        __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
    }

    void UnhookEvent(IEventSource* pSource) {
        __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
        __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
    }
};

int main() {
    // Create COM object
    CoInitialize(NULL);
    {
        IEventSource* pSource = 0;
        HRESULT hr = CoCreateInstance(__uuidof(CSource), NULL, CLSCTX_ALL, __uuidof(IEventSource), (void **)&pSource);
        if (FAILED(hr)) {
            return -1;
        }

        // Create receiver and fire event
        CReceiver receiver;
        receiver.HookEvent(pSource);
        pSource->FireEvent();
        receiver.UnhookEvent(pSource);
    }
    CoUninitialize();
    return 0;
}

另一个错误:

atlbase.h(2835):错误C2338:'CAtlDllModuleT‘必须与定义的_WINDLL或_USRDLL一起使用

由于这段代码应该是正确的(我没有修改它),所以问题可能是我试图构建它的方式。

通过选项/LD和/link,我得到了不同的错误(选项/OPT:NOREF不被识别,/link需要一个源文件名)。

  • 我被迫生成一个DLL吗?
  • 我怎样才能建立这个项目?
  • 代码中的大数字真的有必要吗?

平台: Microsoft C++ 12.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-31 12:45:27

下面是工作代码,它被洗牌成单个源代码文件(控制台应用程序):

代码语言:javascript
复制
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#define _ATL_ATTRIBUTES 1
#include <atlbase.h>
#include <atlcom.h>
#include <atlstr.h>

[dual, uuid("00000000-0000-0000-0000-000000000001")]
__interface IEvents 
{
    [id(1)] HRESULT MyEvent([in] int value);
};

[dual, uuid("00000000-0000-0000-0000-000000000002")]
__interface IEventSource 
{
    [id(1)] HRESULT FireEvent();
};

class DECLSPEC_UUID("530DF3AD-6936-3214-A83B-27B63C7997C4") CSource;

[coclass, event_source(com), uuid("530DF3AD-6936-3214-A83B-27B63C7997C4")]
class CSource : public IEventSource
{
public:
    __event __interface IEvents; 

    HRESULT FireEvent() 
    {
        __raise MyEvent(123);
        return S_OK;
    }
};

[event_receiver(com)]
class CReceiver 
{
public:
    HRESULT MyHandler1(int nValue) 
    {
        printf_s("MyHandler1 was called with value %d.\n", nValue);
        return S_OK;
    }
    HRESULT MyHandler2(int nValue) 
    {
        printf_s("MyHandler2 was called with value %d.\n", nValue);
        return S_OK;
    }
    void HookEvent(IEventSource* pSource) 
    {
        __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
        __hook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
    }
    void UnhookEvent(IEventSource* pSource) 
    {
        __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler1);
        __unhook(&IEvents::MyEvent, pSource, &CReceiver::MyHandler2);
    }
};

[ module(type=exe, name="Event") ];

int _tmain(int argc, _TCHAR* argv[])
{
    // Create COM object
    CoInitialize(NULL);
    {
        CComObject<CSource>* pSource = NULL;
        ATLVERIFY(SUCCEEDED(CComObject<CSource>::CreateInstance(&pSource)));
        CComPtr<IEventSource> pEventSource = pSource;
        CReceiver Receiver;
        Receiver.HookEvent(pEventSource);
        pSource->FireEvent();
        Receiver.UnhookEvent(pEventSource);
    }
    CoUninitialize();
    return 0;
}

或者,您可以在那里(SVN特拉克)获得VS2012项目文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25572024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档