c++ - 如何更改我的 C++ 代码以使其可在 C 中使用?

标签 c++ c file class download

我好像遇到了问题。我正在尝试获取要在 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 ==========

谢谢:)

最佳答案

一种常见的技术是编写包装函数,其中指向类实例的指针在传递给 C 时被视为 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 宏声明的返回值是什么。

另外,删除匈牙利表示法。这很烦人并且已经失宠了。甚至 Microsoft 也建议不要在新代码中使用它。

关于c++ - 如何更改我的 C++ 代码以使其可在 C 中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072078/

相关文章:

c++ - 如何在 C++ 中通过引用传递堆栈矩阵

c++ - int8_t* 和 char*

c++ - 配置:错误:找不到MySQL包含目录

c - 文件读\写中的链表

c# - 给定字体文件,如何获取字体系列的名称?

c++ - 多态 lambda 的默认参数

c++ - 是否有 g++ 或 clang++ 调试选项来指导可变参数模板

c - http 请求未收到完整信息 - C

c - 如何计算C中的执行时间?

java - 这个 android 文档中的 getSeekableFileDescriptor() 是什么?