In winapi, create a program that uses the rest sdk to communicate with the web.
It runs in the console program, but in winapi it generates an error.
The same error occurs when using the asynchronous method.
I looked for similar problems but could not find them.
There is not enough examples of errors when using restsdk in winapi.
Please help me
#include "stdafx.h"
#include <CommCtrl.h>
#include <cpprest\http_client.h>
#include <iostream>
#include <cpprest\json.h>
#include <vector>
#include <iterator>
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace std;
using namespace utility;
using namespace concurrency::streams;
void GetHttp2();
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDC_ADD_BTN: // button clicked
GetHttp2();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
void GetHttp2()
{
http_client client(U("http://en.cppreference.com/w/"));
auto resp = client.request(U("GET")).get(); // error point, xutiliy error...
}
In the 'xutility' file,
inline void _Container_base12::_Orphan_all() _NOEXCEPT
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter) /// error point
(*_Pnext)->_Myproxy = 0;
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
Exception thrown : read access violation. _Pnext was 0xCDCDCDD1.
I'm only guessing that the object returned by request
is immediately destroyed and then you call get
on a destroyed object.
try storing the task returned by request
as a local variable:
void GetHttp2()
{
http_client client(U("http://en.cppreference.com/w/"));
auto task = client.request(U("GET"));
auto resp = task.get();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.