Welcome to MSDN Blogs Sign in | Join | Help

Windows Mobile Team Blog

The Official Windows Mobile Team Blog
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;
}

 

Posted: Wednesday, September 14, 2005 3:20 PM by windowsmobile
Filed under:

Comments

Alex Kac said:

Where was this a month ago when I needed it... :D
# September 16, 2005 5:53 PM

Alex said:

How can I connect to wap?
# September 20, 2005 10:14 PM

windowsmobile said:

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].
# October 6, 2005 5:54 PM

Steven said:

Doesn't seem to work on a local proxy. Actually I found local proxy just doesn't work on WM 5.0 any more.
# December 13, 2005 3:18 PM

Adrian Roney said:

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.
# March 8, 2006 10:31 AM

Luis Cabrera said:

Can IE Mobile connect to your server on your WM 5.0 Emulator? You migh need to set a proxy to connect.
# March 8, 2006 12:40 PM

Adrian said:

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


# March 9, 2006 5:01 AM

Replication Guide said:

Oh, man this is great.  I can't believe something so simple is so hard to find.
# March 31, 2006 10:58 AM

Mobiler said:

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?
# April 12, 2006 8:01 AM

k.vimalkumar, module leader, said:

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,
# May 2, 2006 2:53 AM

Rajat said:

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 Comm\ConnMgr\Providers\{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
# June 6, 2006 3:45 AM

Stu said:

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

Check out \Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Samples\CPP\Mfc\CMHELPER

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

My 2c.

# July 4, 2006 5:27 PM

sriram said:

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.
 
   
# July 14, 2006 12:47 AM

wmshell said:

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.
# July 19, 2006 7:07 PM

John said:

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...
# September 27, 2006 12:08 AM

Luis Cabrera said:

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
# September 29, 2006 12:38 PM

Iain Cowper said:

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.

# November 27, 2006 6:31 AM

Lauren said:

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

# January 31, 2007 10:46 AM

laia said:

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!

# February 8, 2007 2:00 PM

Raj said:

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

# April 16, 2007 9:19 AM

nesrin said:

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#.

# June 25, 2007 3:41 AM

vikas said:

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

Is there standard API's for them in WinCE.

regards

# July 28, 2007 2:54 PM

MSJ said:

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?

# September 28, 2007 9:35 PM

Sam said:

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

# March 5, 2008 1:53 PM

Dacz Andras said:

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

(Sorry.. )

# May 6, 2008 4:04 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker