Hi,
I'm relatively new to C++ ad i'm trying to make a winhttp wrapper class. The problem is that I have to put a reference back from inside a function. But then the pointer goes out of scope because you can't destroy it:
And the snipper from the class:Code:int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show) { HTTPReq* tst = new HTTPReq(); HTTPReq::REQUEST_CONTEXT* context=tst->Connect(L"arevico.com",80,L"Firefox",true); tst->SendRequest(context,L"ahe",L"referer",L"?page=1"); // here i want to be able to pass context (so actually a item in the list defined in the class Sleep(10000); delete tst; return 0; }
What is the best way in C++ standards to do this?Code:list<REQUEST_CONTEXT> requestcontexes; // CRITICAL_SECTION cs; HINTERNET hSession; REQUEST_CONTEXT* Connect(LPCWSTR servername,unsigned int port,LPCWSTR useragent,bool async=false){ REQUEST_CONTEXT cpContext; EnterCriticalSection(&cs); requestcontexes.push_front(cpContext); REQUEST_CONTEXT* refContext=&requestcontexes.front(); //locally declared pointer should be destroyed later hSession= WinHttpOpen(useragent,WINHTTP_ACCESS_TYPE_NO_PROXY,NULL,NULL,async ? WINHTTP_FLAG_ASYNC : NULL); refContext->hConnect=WinHttpConnect(hSession,servername,port,0); //delete refContext; //you cant do this here LeaveCriticalSection(&cs); return refContext; }


Reply With Quote

Bookmarks