Convert C++ to C Using C++ in C -
i seem having problem. i'm trying c++ function used in c code.
i tried these methods
- http://www.olivierlanglois.net/idioms_for_using_cpp_in_c_programs.html
- calling "c++" class member function "c" code
if need rid of class altogether ,but don't want lose download feed console.
here code works in c++ not in c.
#include <tchar.h> #include <urlmon.h> #pragma comment(lib,"urlmon.lib") #include <stdio.h> class mycallback : public ibindstatuscallback { public: mycallback() {} ~mycallback() { } // 1 called 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; } // rest don't 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 gives error
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 type 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 - 'int *' '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 :)
a common technique write wrapper functions, pointer class instance treated void*
when passed c. then, expose function say:
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: above missing return type because i've no idea stdmethod macro declares return value.
also, drop hungarian notation. it's annoying , has fallen out of favor. microsoft recommends against using in new code.
Comments
Post a Comment