September 15, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mastering Windows Networking (WNet)

  • October 20, 2004
  • By Alex Gusev
  • Send Email »
  • More Articles »

Establishing and Terminating Network Connections

WNet provides several fucntions to establish and to terminate a connection to a network resource. You can use either WNetConnectionDialog1 or WNetAddConnection3 to create a new connection. After a successful call, each one creates a new folder under the '\Network' directory on the device. Notice that the system does not re-connect to mapped resources after a warm reboot. Following are two code snippets to demonstrate the usage of each of these functions.

void CWNetSampleDlg::OnButtonAdd()
{
   UpdateData();

   NETRESOURCE nr;
   memset(&nr,0,sizeof(nr));
   nr.lpRemoteName = (LPTSTR)(LPCTSTR)m_sRemoteName;
   nr.dwType = RESOURCETYPE_DISK;

   CONNECTDLGSTRUCT c;
   memset(&c,0,sizeof(c));
   c.cbStructure = sizeof(CONNECTDLGSTRUCT);
   c.hwndOwner = m_hWnd;
   c.lpConnRes = &nr;

   DWORD dwRes = WNetConnectionDialog1(&c);
   CString sErr;
   sErr.Format(L"Error = %d",dwRes);
   AfxMessageBox(sErr);
}

void CWNetSampleDlg::OnButtonAdd3()
{
   UpdateData();

   NETRESOURCE nr;
   memset(&nr,0,sizeof(nr));
   nr.lpRemoteName = (LPTSTR)(LPCTSTR)m_sRemoteName;
   nr.lpLocalName  = L"MyShareTmp";
   nr.dwType       = RESOURCETYPE_DISK;

   DWORD dwRes = WNetAddConnection3(
      m_hWnd,&nr,
      NULL,
      NULL,
      CONNECT_UPDATE_PROFILE);

   CString sErr;
   sErr.Format(L"Error = %d",dwRes);
   AfxMessageBox(sErr);
}

WNetConnectionDialog1 shows a dialog to map the remote name to a local one. WNetAddConnection3 does the same job silently. Both of them may use a pre-defined NETRESOURCE entry to determine what to do. In addition, WNetAddConnection3 allows remembering this connection as persistent; in other words, it'll be remembered after a warm reboot.

There are three functions your application may use to cancel the existing connection to network resources:

  • WNetDisconnectDialog
  • WNetDisconnectDialog1
  • WNetCancelConnection2

Each function above results in dropping the connection. WNetDisconnectDialog pops up a dialog with all existing connections. The user then can the select desired resource to get disconnected. WNetDisconnectDialog1 and WNetCancelConnection2 do their job silently. The return code indicates the result of the requested operation. Besides, the last two functions may fail depending on existing open files or jobs that use this connection. The user is informed about all errors that occurred. A simple example of this is shown below:

void CWNetSampleDlg::OnButtonDisc()
{
   DWORD dwRes = WNetDisconnectDialog(m_hWnd,0);
   CString sErr;
   sErr.Format(L"Error = %d",dwRes);
   AfxMessageBox(sErr);
}

void CWNetSampleDlg::OnButtonDisc1()
{
   DISCDLGSTRUCT d;
   d.cbStructure = sizeof(DISCDLGSTRUCT);
   memset(&d,0,sizeof(d));
   d.hwndOwner   = m_hWnd;
   d.lpLocalName = L"MyShare";
   DWORD dwRes   = WNetDisconnectDialog1(&d);
   CString sErr;
   sErr.Format(L"Error = %d",dwRes);
   AfxMessageBox(sErr);
}

void CWNetSampleDlg::OnButtonCancel2()
{
   DWORD dwRes = WNetCancelConnection2(L"MyShare",
                                       CONNECT_UPDATE_PROFILE,TRUE);
   CString sErr;
   sErr.Format(L"Error = %d",dwRes);
   AfxMessageBox(sErr);
}

Reading and Writing Data

And finally, the simplest thing: How can your application conduct I/O operations via WNet? The answer is quite primitive: by using standard file APIs. Once you have detected the UNC name of the network resource and have successfully established a connection to it, you can consider it a 'file' name. Thus, a common flow may look as follows:

  • Establish a connection to the network resource by calling WNetAddConnection3
  • Proceed with all desired read/write/copy/delete operations
  • Close the connection to the network resource

As a result, the application can handle all required I/O operations on remote hosts. The most obvious usage is printing on network printers. In this case, you can either open the selected resource and then use WriteFile to send data to it or simply call CopyFile. The following sample demonstrates it:

void CWNetSampleDlg::OnButtonPrint()
{
   UpdateData();
   HANDLE hFile = CreateFile(m_sRemoteName,GENERIC_WRITE,0,0,
                             OPEN_EXISTING,0,0);
   if ( hFile != INVALID_HANDLE_VALUE )
   {
      CFile f;
      if ( f.Open(L"\\Sample.txt",CFile::modeRead) )
      {
         CByteArray baFile;
         baFile.SetSize(f.GetLength() + 1);
         f.Read(baFile.GetData(),f.GetLength());
         f.Close();

         DWORD wb;
         WriteFile(hFile,baFile.GetData(),baFile.GetSize()
                   - 1,&wb,0);

         AfxMessageBox(L"Sent data to " + m_sRemoteName);
      }
      else
      {
         AfxMessageBox(L"Failed to open local file");
      }
      CloseHandle(hFile);
   }
   else
   {
      AfxMessageBox(L"Failed to open " + m_sRemoteName);
   }
}

void CWNetSampleDlg::OnButtonPrintCopy()
{
   UpdateData();
   CopyFile(L"\\Sample.txt",m_sRemoteName,FALSE);
}

This code snippet uses WNet to print on an HP LaserJet printer available somewhere on the network. It uses "Sample.txt" as a test PCL stream to print a cover page of the PCL manual. You may easily create your own printer data by printing some document and choosing the "Print to file" option.

Conclusion

As you have seen, WNet under Windows CE is really a simple but useful set of APIs. If your applications work in a networking environment and you want to use network resources, WNet will do its best.

Download

Download the accompanying code's zip file here.

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. Now, he works at an international retail software company as a team leader of the Mobile R department, making programmers' lives in the mobile jungles a little bit simpler.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel