简体   繁体   English

在winapi中,使用restsdk时出错

[英]In winapi, using restsdk, error

In winapi, create a program that uses the rest sdk to communicate with the web. 在winapi中,创建一个使用其余sdk与网络通信的程序。

It runs in the console program, but in winapi it generates an error. 它在控制台程序中运行,但在winapi中会生成错误。

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. 在winapi中使用restsdk时,没有足够的错误示例。

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, 在“ xutility”文件中,

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. _Pnext为0xCDCDCDD1。

Error Image Capture 错误图像捕获

I'm only guessing that the object returned by request is immediately destroyed and then you call get on a destroyed object. 我只是在猜测request返回的对象会立即被销毁,然后在被销毁的对象上调用get

try storing the task returned by request as a local variable: 尝试将request返回的任务存储为局部变量:

void GetHttp2()
{
    http_client client(U("http://en.cppreference.com/w/"));
    auto task = client.request(U("GET")); 
    auto resp = task.get(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM