Using connection manager and Wininet


The last two weeks I had the chance to use the wininet APIs for the first time. They seemed pretty straight-forward to me and I got my code to work. My code would not work whenever there was a proxy – so I was instructed that I needed to use the connection manager to establish the connection. That seems fair, but since I spent some time figuring out why my code was not working, I am writing this entry – hopefully it will be useful to someone else.


The main idea is that you need to use connection manager in order to get the access type and the proxy information, and then you can pass the information to the WININET InternetOpen API.


This is more or less what your code should look like. Once again this code is give as “AS IS”, it has not been tested, and it is for instructional purposes only.
Enjoy!


-Luis Cabrera [Microsoft]


#include <wininet.h>
#include <initguid.h>
#include <connmgr.h>
#include <connmgr_proxy.h>
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))


// pszDestAddr: The URL you would like to connect to: i.e. http://www.microsoft.com
//
extern “C”
HRESULT WINAPI ConnectAndDoSomething(LPTSTR pszDestAddr)
{
   
CONNMGR_CONNECTIONINFO ci = {0};
   
PROXY_CONFIG pcProxy = {0};
   
DWORD dwStatus = 0;
   
DWORD dwIndex = 0;
   
HRESULT hr = S_OK; 
   
HANDLE hConnection = NULL;
   
HANDLE hOpen = NULL;
   
LPTSTR pszProxy = NULL;
   
DWORD dwAccessType;


   // Register with the connection manager
   ci.cbSize = sizeof(CONNMGR_CONNECTIONINFO); 
   
ci.dwParams = CONNMGR_PARAM_GUIDDESTNET; 
   
ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP; 
   
ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;


   // Map the URL to a network, if we can
   hr = ConnMgrMapURL(pszDestAddr, &(ci.guidDestNet), &dwIndex);
   
//Check hr value.


   hr = ConnMgrEstablishConnectionSync(&ci, &hConnection, 25000, &dwStatus); 
   
//Check hr value.


   //Make sure dwStatus == CONNMGR_STATUS_CONNECTED
   // Get proxy information.


   hr = ConnMgrProviderMessage( hConnection, &IID_ConnPrv_IProxyExtension, NULL, 0, 0, (PBYTE)&pcProxy, sizeof(pcProxy)); 
   
   
if (S_OK == hr)
   
{
      
dwAccessType = INTERNET_OPEN_TYPE_PROXY;
      
pszProxy = (LPTSTR) LocalAlloc(LPTR, ARRAYSIZE(pcProxy.szProxyServer));
      
// Make sure pszProxy was allocated.
      
      
hr = StringCchCopyN(pszProxy, ARRAYSIZE(pcProxy.szProxyServer), 
                          
pcProxy.szProxyServer, ARRAYSIZE(pcProxy.szProxyServer));
      
//Check hr value
   
}
   
else if (E_NOINTERFACE == hr)
   
{
      
dwAccessType = INTERNET_OPEN_TYPE_DIRECT;
      
pszProxy = NULL;


      // Reset hr, since it’s not really an error here.
      
hr = S_OK;
   
}


   // Open internet connection, and get merchandise information.
   
hOpen = InternetOpen(TEXT(“Agent name”),
                        
dwAccessType,
                        
pszProxy,
                        
NULL,0);


   // Do whatever you need to do with the WININET connection here.
   
if (hConnection)
  
{
      
ConnMgrReleaseConnection(hConnection, TRUE);
   
}


    // Do any necessary cleanup.
    
return hr;
}


 


Comments (27)

  1. Alex Kac says:

    Where was this a month ago when I needed it… 😀

  2. Alex says:

    How can I connect to wap?

  3. windowsmobile says:

    How can I connect to wap?

    Although I have not used WAP before, I don’t think Wininet should be used to talk to WAP. Instead you should use the WAP functions at

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mobilesdk5/html/mob5conwapfunctions.asp

    Please let me know if you have a more specific question.

    Luis Cabrera[Microsoft].

  4. Steven says:

    Doesn’t seem to work on a local proxy. Actually I found local proxy just doesn’t work on WM 5.0 any more.

  5. Adrian Roney says:

    Hi,

    I am trying to connect my application to a server on my internal network, using the WM 5.0 smartphone emulator.

    I have managed to get Internet connectivity enabled in the emulator,  But my app does not seem to want to connect to the server.

    The same Connection Manager Code worked fine on the smartphone 2003 emulator.

    Any ideas as to what I need to do to get this working?

    adrian.

  6. LuisCa says:

    Can IE Mobile connect to your server on your WM 5.0 Emulator? You migh need to set a proxy to connect.

  7. Adrian says:

    Hi,

    The server I am trying to connect to is not web server.

    But IE can connect to the internet.

    This is an internal development server inside my firewall.

    Adrian

  8. Oh, man this is great.  I can’t believe something so simple is so hard to find.

  9. Mobiler says:

    Hi, I used your code in my application on IPaq hx2190 with WM5. I use the Bluetooth GPRS connection. With your code I have succesfully established the connection, but when I reset my  pocket pc during the active connection (while downloading data actually) I couldn’t reestablish it again. Even PIE doesn’t connect anymore. I had to soft reset my machine few times and it worked again… until I soft reset  my pocket pc. Could you help me with that? What am I doing wrong?

  10. k.vimalkumar, module leader, says:

    hai,

     i want to active GPRS connection, using source code.

      By clicking a button, i have to activate my GPRS connection. i am working in PocketPC and SmartPhone. provide me the solution, to connect GPRS from PocketPC and SmartPhone.

    waiting fro your reply,

  11. Rajat says:

    Hi,

    I am using UTstarcom SPRINT 6700 with WM 5.0. I am facing a problem of IE. After connecting VPN, my application loads virtual adapter and makes an entry in CommConnMgrProviders{f792e23c-dc19-4668-9be4-f8688b4c18d6}VA with all the required registry values. I also delete original adapter entry TIACXWLN1 from there. After this if i try to open a web-page from IE it stay in "locating" state and after some time gives error "The page cannot be displayed or downloaded because the connection was lost. Check the connection and try again". I use the same logic to solve the IE problem in K-Jam. But somehow its not working in SPRINT 6700. Does anybody faced such problem before. Hope you can help me.

    Thanks in advance

    Rajat

  12. Stu says:

    Imho there is a better example of Connection Manager usage in the Windows Mobile SDK sample applications.

    Check out Windows CE Toolswce500Windows Mobile 5.0 Pocket PC SDKSamplesCPPMfcCMHELPER

    And in particular the Connection.h and Connection.cpp files.

    My 2c.

  13. sriram says:

    Hi,

    Generally we Find that when Wi-fi is enabled ,the Connection Manager automatically

    Connects to Wi-fi Hotspot.  

    Is there  any setting or api to stop Connection Manager from automatically connecting to the Wi-fi Hotspot.

    I have tried searching configuration Manager API and Wi-Fi Provisioning  using DXMLProcessConfig.But I dont seem to find one.

    Any help in this regard would be appreciated.

     

    Thanx in Advance.

     

       

  14. wmshell says:

    In Magneto AKU 1.1 and later there is a new Wi-Fi networks notification with a "Don’t ask me again" checkbox. Prior to that, I think you’re out of luck.

  15. John says:

    Hello,

    I was wondering whether you had any information or examples using the connMgrRegisterForStatusNotification() function? We have been unable to receive any messages from the connection manager, even after registering for them. An example, or a clue as to how to use this part of the connection manager API would be very helpful.

    Thanks,

    John…

  16. LuisCa says:

    I have a snippet:

    First you need to register a new Window Message

    UINT g_NotifyMsg = RegisterWindowMessage( NOTIFY_MSG_STRING );

    Then register for the notification

       HRESULT Res = ConnMgrRegisterForStatusChangeNotification( TRUE, Wnd );

       if ( FAILED( Res ) )

       {

           OutputMessage( TEXT("Error: Registration for status change notifications failed, ")

                  TEXT("result 0x%x.n"), GetLastError() );

           DestroyWindow( Wnd );

           Wnd = NULL;

           UnregisterClass( wc.lpszClassName, 0 );

           return -1;

       }

    and then in the WndProc you can handle the notification:

    //

    // Window message handler for status change notifications.

    //

    LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

    {

       if ( uMsg == WM_CLOSE )

       {

           DestroyWindow( hWnd );

           return 0;

       }

       else if ( uMsg == WM_DESTROY )

       {

           PostQuitMessage( 0 );

           return 0;

       }

       else if ( uMsg == g_NotifyMsg )

       {

           OutputMessage( TEXT("Message 0x%X: "%s" wParam = %d received.n"),

               uMsg, NOTIFY_MSG_STRING, wParam );

           PrintQuery( 0, NULL );

           return 0;

       }

       return DefWindowProc(hWnd, uMsg, wParam, lParam);

    }

    I hope this helps.

    Remember this codes is "AS IS" and grants no rights or privileges.

    Good luck,

    -Luis Cabrera

  17. Iain Cowper says:

    On a different but related subject can anyone tell me why WM5 on an HTC TyTN does not work with proxy servers? You can enter your proxy details fine, but then when you exit it forgets everything! Nice.

  18. Lauren says:

    I am able to connect with the Windows CE Tools sample application to a Hayes compatible modem on COM1 using WM5 for a pocket pc but whenever i attempt to disconnect it stays connected, any ideas?  I have used the release function for the connection manager and the disconnect in the CE Tools but with no luck.  Any help or ideas would be greatly appreciated

  19. laia says:

    I was not able to connect to the Internet from an application in a PocketPc and this code solved the problem. Lots of thanks, it saved my live!

  20. Raj says:

    i am able to connect GPRS as shown in the code,

    but when I send a HttpRequest,through HttpSendRequest API, the call is unsuccessful.

    before sending the request I call InternetConnect, and HttpOpenrequest, which are successfull.

    can any one suggest me how to use HTTP on GPRS.

    thanks in advance

  21. nesrin says:

    hi, i am trying on a project which is about pocket pc.(sending and receiving an email) but i cant find the source code of connecting to the internet with c#.

  22. vikas says:

    I want to connect to Wifi/GPRS/ActiveSync points.

    Is there standard API’s for them in WinCE.

    regards

  23. MSJ says:

    I have a VB.Net (VS.Net version 2005) application using Windows Mobile 5. I am trying to FTP files from the handheld device (Intermec) to the FTP server. I want to use WinINet to do the file transfer. With this configuration, I can open and get a handle to the internet but I can’t connect to the server. I rebuilt the application as a windows application trying to connect to the FTP server and the windows application can. How do I solve the issue with the handheld device running Windows Mobile 5?

  24. Sam says:

    I have included two library: cellcore.lib and wininet.lib. But when I compiled it, I got link errors as below:

    wm6win32TestConMana.obj : error LNK2019: unresolved external symbol "long __cdecl ConnectAndDoSomething(wchar_t *)" (?ConnectAndDoSomething@@YAJPA_W@Z) referenced in function WinMain

    Windows Mobile 6 Professional SDK (ARMV4I)Debug/wm6win32TestConMana.exe : fatal error LNK1120: 1 unresolved externals

    Do I need to add dll file? If so where?

    I am using Windows Mobile 6 SDK with Visual Studio 2005 C++.

    thanks in advanced for any reply.

    Sam

  25. Dacz Andras says:

    Hy, sorry for distubing everyone, i’m quite a newby in this thinhs. Could anyone translat this script for me into VB .NET ?

    (Sorry.. )

  26. leesoft says:

    Does the connection manager bypass the local routing table? I add couple routes locally to forwarding the traffic to a specific ip range to an virtual adapter, it works fine with the regular socket based applications, but for the connection manager based app such as pocket IE, the packet just doesn’t hit the routing table. is there anyway to resolve this?

  27. Sweve says:

    (Using windows mobile 5/ pocketPC) When a gprs connection is established with connection manager, it will remain active, even when there is nothing using the connection. However, when a gprs connection is automatically established with internet explorer, it will only remain active until its cache timeout(default 10mins-see HKLM/Comm/ConnMgr/Planner/Settings —> CacheTime) has time left. Does anyone know why or how to get it so that a connection established via internet explorer remains active regardless, just as if it was initiated from connection manager?