Why the InternetOpenUrl() alway returns NULL on a sub-thread?


  • here's my cpp code:

    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include <wininet.h>
    #include <process.h>

    #pragma comment(lib,"wininet.lib")

    typedef unsigned (__stdcall *PTHREAD_START) (void *);

    #define chBEGINTHREADEX(psa, cbStack, pfnStartAddr, pvParam, fdwCreate, pdwThreadID) \
    ((HANDLE) _beginthreadex(                          \
    (void *)(psa),                                     \
    (unsigned)(cbStack),                               \
    (PTHREAD_START)(pfnStartAddr),                     \
    (void *)(pvParam),                                 \
    (unsigned)(fdwCreate),                             \
    (unsigned *)(pdwThreadID)))

    HANDLE g_hThread;
    BOOL g_fbThreadRunning;

    //unsigned __stdcall GetHttpContentThread( void* param );
    DWORD WINAPI GetHttpContentThread( LPVOID lpParameter);

    int _tmain(int argc, _TCHAR* argv[])
        g_fbThreadRunning = TRUE;

        DWORD dwThreadId = 0;
        SECURITY_ATTRIBUTES sa = {0};
        sa.bInheritHandle = TRUE;
        sa.nLength = sizeof(sa);
        g_hThread = CreateThread( &sa, 0, GetHttpContentThread, NULL, 0, &dwThreadId );
        //g_hThread = chBEGINTHREADEX( NULL, 0, GetHttpContentThread, NULL, CREATE_SUSPENDED, NULL );
        //g_fbThreadRunning = TRUE;
        //SetThreadPriority( g_hThread, THREAD_PRIORITY_LOWEST );
        //ResumeThread( g_hThread );

        WaitForSingleObject( g_hThread, INFINITE );
        CloseHandle( g_hThread );

        return 0;

    DWORD WINAPI GetHttpContentThread( LPVOID lpParameter )
    //unsigned __stdcall GetHttpContentThread( void* param )
        HINTERNET hInternetSession = ::InternetOpen( _T("MyAgent"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
        if ( hInternetSession == NULL )
            return 0xdead;

        TCHAR szHttpHeader[] = _T("Accept: */*\r\n\r\n");

        HINTERNET hUrl = ::InternetOpenUrl( hInternetSession,
            _T(""), szHttpHeader, sizeof(szHttpHeader),
            INTERNET_FLAG_DONT_CACHE, 0 );

        if ( NULL == hUrl )
            return 0xdead;

        // here's my thread code
        while ( g_fbThreadRunning )


        return 0xdead;

    Somebody pls help me that! I have been to died as soon for do that... - -

    • Edited by dreamerate Saturday, August 16, 2008 8:36 AM has code
    Saturday, August 16, 2008 8:36 AM



    Dear dreamerate,


    Thanks for participating on the MSDN Forum! But this forum is reserved for Visual C++ General Questions. For WinInet programming question, I suggest you post it on win32.programmer.networks newsgroup, there you will find WinInet experts and your question should be answered in no time.


    Please post your question at

    If you cannot find an appropriate answer in that newsgroup or still believe this forum is the best fit for your questions, please feel free to mark the thread as not resolved and we will look into it again.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Yan-Fei Wei Thursday, August 21, 2008 9:47 AM
    Tuesday, August 19, 2008 10:08 AM