0

我好像有问题。我试图让一个 c++ 函数在 c 代码中使用。

我试过这些方法

如果我需要完全摆脱课程,那么我会的,但我不想将下载提要丢失到控制台。

这是在 c++ 中有效但在 c 中无效的代码。

#include <tchar.h>
#include <urlmon.h>
#pragma comment(lib,"urlmon.lib")
#include <stdio.h>

class MyCallback : public IBindStatusCallback
{
public:
    MyCallback() {}

    ~MyCallback() { }

    // This one is called by URLDownloadToFile
    STDMETHOD(OnProgress)(/* [in] */ ULONG ulProgress, /* [in] */ ULONG ulProgressMax, /* [in] */ ULONG ulStatusCode, /* [in] */ LPCWSTR wszStatusText)
    {
        printf("Downloaded %i of %i byte(s) Status Code = %i\n",ulProgress, ulProgressMax, ulStatusCode);
        return S_OK;
    }

    // The rest  don't do anything...
    STDMETHOD(OnStartBinding)(/* [in] */ DWORD dwReserved, /* [in] */ IBinding __RPC_FAR *pib)
    { return E_NOTIMPL; }

    STDMETHOD(GetPriority)(/* [out] */ LONG __RPC_FAR *pnPriority)
    { return E_NOTIMPL; }

    STDMETHOD(OnLowResource)(/* [in] */ DWORD reserved)
    { return E_NOTIMPL; }

    STDMETHOD(OnStopBinding)(/* [in] */ HRESULT hresult, /* [unique][in] */ LPCWSTR szError)
    { return E_NOTIMPL; }

    STDMETHOD(GetBindInfo)(/* [out] */ DWORD __RPC_FAR *grfBINDF, /* [unique][out][in] */ BINDINFO __RPC_FAR *pbindinfo)
    { return E_NOTIMPL; }

    STDMETHOD(OnDataAvailable)(/* [in] */ DWORD grfBSCF, /* [in] */ DWORD dwSize, /* [in] */ FORMATETC __RPC_FAR *pformatetc, /* [in] */ STGMEDIUM __RPC_FAR *pstgmed)
    { return E_NOTIMPL; }

    STDMETHOD(OnObjectAvailable)(/* [in] */ REFIID riid, /* [iid_is][in] */ IUnknown __RPC_FAR *punk)
    { return E_NOTIMPL; }

    // IUnknown stuff
    STDMETHOD_(ULONG,AddRef)()
    { return 0; }

    STDMETHOD_(ULONG,Release)()
    { return 0; }

    STDMETHOD(QueryInterface)(/* [in] */ REFIID riid, /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject)
    { return E_NOTIMPL; }
};

int main()
{
    MyCallback pCallback;
    HRESULT url = URLDownloadToFile(NULL,_T("https://dl.dropboxusercontent.com/u/102222417/jediAcademy.zip"),_T("C:\\JKA\\file.zip"),0,&pCallback);

    if(url == S_OK)
    {
        printf("Successful!");
        getchar();
    }

    else if(url == E_OUTOFMEMORY)
    {
        printf("Not enough memory!");
    }

    else if (url == INET_E_DOWNLOAD_FAILURE)
    {
        printf("ERROR: INET invalid resource");
    }
    return 0;
}

在 C 代码中运行时会出现此错误

1>------ Build started: Project: Downloader++, Configuration: Debug
Win32 ------ 2012\projects\downloader++\downloader++\main.c(101):
error C2061: syntax error : identifier 'MyCallback'
2012\projects\downloader++\downloader++\main.c(101): error C2059:
syntax error : ';'
2012\projects\downloader++\downloader++\main.c(101): error C2059:
syntax error : ':'
2012\projects\downloader++\downloader++\main.c(150): error C2065:
'MyCallback' : undeclared identifier
2012\projects\downloader++\downloader++\main.c(150): error C2146:
syntax error : missing ';' before identifier 'pCallback'
2012\projects\downloader++\downloader++\main.c(150): error C2065:
'pCallback' : undeclared identifier 1>
studio 2012\projects\downloader++\downloader++\main.c(151): error
C2275: 'HRESULT' : illegal use of this type as an expression 1>       
c:\program files (x86)\windows kits\8.0\include\um\winnt.h(556) : see
declaration of 'HRESULT'
2012\projects\downloader++\downloader++\main.c(151): error C2146:
syntax error : missing ';' before identifier 'url'
1>c:\users\gamer\documents\visual studio
2012\projects\downloader++\downloader++\main.c(151): error C2065:
'url' : undeclared identifier 1>c:\users\gamer\documents\visual studio
2012\projects\downloader++\downloader++\main.c(151): error C2065:
'pCallback' : undeclared identifier
2012\projects\downloader++\downloader++\main.c(151): warning C4133:
'function' : incompatible types - from 'int *' to
'LPBINDSTATUSCALLBACK'
2012\projects\downloader++\downloader++\main.c(153): error C2065:
'url' : undeclared identifier
2012\projects\downloader++\downloader++\main.c(159): error C2065:
'url' : undeclared identifier
2012\projects\downloader++\downloader++\main.c(164): error C2065:
'url' : undeclared identifier 1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

谢谢 :)

4

1 回答 1

0

一种常见的技术是编写包装函数,其中指向类实例的指针在传递给 C 时被视为 a void*。然后,您公开一个函数,例如:

extern "C" MyClass_OnProgress(/* [in] */ void* instance, /* [in] */ ULONG ulProgress, /* [in] */ ULONG ulProgressMax, /* [in] */ ULONG ulStatusCode, /* [in] */ LPCWSTR wszStatusText)
{
    MyClass* _this = static_cast<MyClass*>(instance);
    _this->OnProgress(ulProgess, ulProgressMax, ulStatusCode, wszStatusText);
}

注意:上面缺少返回类型,因为我不知道 STDMETHOD 宏将什么声明为返回值。

另外,放弃匈牙利符号。这很烦人,已经失宠了。甚至微软也建议不要在新代码中使用它。

于 2013-04-18T00:45:57.430 回答