[英]How can I change my C++ code to make it usable in C?
I seem to be having a problem.我好像遇到了问题。 I'm trying to get a c++ function to be used in c code.
我正在尝试在 c 代码中使用 c++ 函数。
I tried these methods我试过这些方法
If i need to get rid of the class altogether then i will ,but i don't want to lose the download feed to the console.如果我需要完全摆脱课程,那么我会,但我不想丢失控制台的下载提要。
Here is the code that works in c++ but not in 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;
}
when running in C code it gives this error在 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 ==========
Thank you :)谢谢 :)
A common technique is to write wrapper functions, where the pointer to your class instance is treated as a void*
when passed to C. Then, you expose a function like say:一种常见的技术是编写包装函数,其中指向您的类实例的指针在传递给 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);
}
note : the above is missing the return type because I've no idea what the STDMETHOD macro declares as the return value.注意:上面缺少返回类型,因为我不知道 STDMETHOD 宏声明为返回值。
Also, drop the Hungarian notation.此外,删除匈牙利符号。 It's annoying and has fallen out of favor.
这很烦人并且已经失宠。 Even Microsoft recommends against using it in new code.
甚至 Microsoft 也不建议在新代码中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.