3

I already successful implement a sync way, but how can I do it in async way?

Because there is no way to add parameter to winhttp callback.

WINHTTP_STATUS_CALLBACK theCallback =
    WinHttpSetStatusCallback
    (
    hSession,
    (WINHTTP_STATUS_CALLBACK)HttpCallback,
    WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS,
    NULL
    );

I tried save the lua_State as a variable and use it in HttpCallback, but it cause access violation.

here is my code:(not full code)

C

static int callback_reference = 0;
static lua_State *Ltemp;
static int lua_registerCallback(lua_State *L) 
{
    callback_reference = luaL_ref(L, LUA_REGISTRYINDEX);
    return 0;
}
void call_callback(lua_State* L) 
{
    lua_rawgeti(L, LUA_REGISTRYINDEX, callback_reference);
    lua_pushstring(L, buf);
    if (0 != lua_pcall(L, 1, 0, 0)) {
       printf("Failed to call the callback!\n %s\n", lua_tostring(L, -1));
       return;
    }
}
void CALLBACK HttpCallback(HINTERNET hInternet, DWORD * dwContext, DWORD  dwInternetStatus, void * lpvStatusInformation, DWORD dwStatusInformationLength)
{
    //when done
    call_callback(Ltemp);
}
static int doHttp(lua_State *L)
{
    Ltemp = L;
    //init winhttp code here
}

lua

function callback( result )
    print("ok")
    print(result)
end
lua_registerCallback(callback)
doHttp("http://stackoverflow.com/")

EDIT: I also tried WinHttpSetOption and WinHttpQueryOption, but no luck.Am I did something wrong?

//save L
bool result = WinHttpSetOption(
    hRequest,
    WINHTTP_OPTION_CONTEXT_VALUE,
    &L,
    sizeof(struct lua_State *)
    );

//get L, but I think the L is broken here
lua_State* L;
bool result = WinHttpQueryOption(
            hInternet,
            WINHTTP_OPTION_CONTEXT_VALUE,
            &L,
            sizeof(struct lua_State *)
            );
min
  • 953
  • 1
  • 11
  • 23
  • I'm not sure how to fix the access violation, but you can add a parameter to the callback with [WinHttpSetOption](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384114.aspx), with option [WINHTTP_OPTION_CONTEXT_VALUE](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384066.aspx). – ssbssa Sep 27 '15 at 16:15
  • @ssbssa I edited my post, can you check it? – min Sep 28 '15 at 08:26
  • dwBufferLength doesn't expect a pointer, so remove the '&' from Lsize. – ssbssa Sep 28 '15 at 10:52
  • @ssbssa, like this? but if I remove the "&", I got access violation when run WinHttpQueryOption – min Sep 28 '15 at 12:59
  • You shouldn't need the WinHttpQueryOption, HttpCallback gives you dwContext. – ssbssa Sep 28 '15 at 16:58
  • @ssbssa, ok, but same error. How can I know the dwContext is the lua_State? Is there anyway I can debug? – min Sep 28 '15 at 21:00

0 Answers0