The Win32 Network Management APIs 27

 

 

 

 

 

NetApiBufferAllocate(), NetApiBufferSize(),  NetApiBufferReallocate() and NetApiBufferFree() Program Example

 

The NetApiBufferAllocate() function allocates memory from the heap. Use this function only when compatibility with the NetApiBufferFree() function is required. The NetApiBufferSize() function returns the size, in bytes, of a buffer allocated by a call to the NetApiBufferAllocate() function. The NetApiBufferReallocate() function changes the size of a buffer allocated by a previous call to the NetApiBufferAllocate() function. The NetApiBufferFree() function frees the memory that the NetApiBufferAllocate() function allocates. Applications should also call NetApiBufferFree() to free the memory that other network management functions use internally to return information. The following code sample demonstrates how to use the network management ApiBuffer functions. The sample first calls the NetApiBufferAllocate() function to allocate memory and then the NetApiBufferSize() function to retrieve the size of the allocated memory. Following this, the sample calls NetApiBufferReallocate() to change the size of the memory allocation. Finally, the sample calls NetApiBufferFree() to free the memory. In each case, the sample prints a message indicating success or failure.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetApiBufferAllocate(), NetApiBufferSize(),  NetApiBufferReallocate() and NetApiBufferFree() Program Example: Creating new Win32 C++ console application project

 

Then, add the source file and give it a suitable name.

 

NetApiBufferAllocate(), NetApiBufferSize(),  NetApiBufferReallocate() and NetApiBufferFree() Program Example: Adding new C++ dource file for the source code

 

Then, add the following source code.

 

#include <windows.h>

#include <lm.h>

#include <stdio.h>

 

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

 

void PrintError(LPSTR lpszApi, DWORD res);

 

int wmain()

{

   PUSER_INFO_10 p;

   DWORD res, dwSize;

 

   // Call the NetApiBufferAllocate() function

   //   to allocate the memory. If successful, print a message.

   res = NetApiBufferAllocate(1024, (LPVOID *) &p);

   if(res == NERR_Success)

   {

      wprintf(L"NetApiBufferAllocate():   Allocated 1024 bytes.\n");

 

      // Call the NetApiBufferSize() function

      //   to retrieve the size of the allocated buffer. If successful, print the size.

      res = NetApiBufferSize(p, &dwSize);

      if(res == NERR_Success)

      {

         wprintf(L"NetApiBufferSize():       Buffer has %u bytes.\n", dwSize);

 

         // Call the NetApiBufferReallocate() function

         //   to change the size of the allocated memory.

         //   If successful, print the new size of the buffer.

         res = NetApiBufferReallocate(p, dwSize * 2, (LPVOID *) &p);

 

         if(res == NERR_Success)

            wprintf(L"NetApiBufferReallocate(): Re-Allocated %u bytes.\n", dwSize * 2);

         else

            PrintError("NetApiBufferReallocate()", res);

 

         // Call the NetApiBufferFree() function

         //    to free the allocated memory. If successful, print a message.

         res = NetApiBufferFree(p);

 

         if(res == NERR_Success)

            wprintf(L"NetApiBufferFree():       Freed Buffer\n");

 

         else

            PrintError("NetApiBufferFree()", res);

      }

      else

         PrintError("NetApiBufferSize()", res);

   }        

   else

      PrintError("NetApiBufferAllocate()", res);

   return 0;

}

 

void PrintError(LPSTR lpszApi, DWORD res)

{

   wprintf(L"%s: Error %u\n", lpszApi, res);

   return;

}

 

Build and run the project. The following screenshot is a sample output.

 

NetApiBufferAllocate(), NetApiBufferSize(),  NetApiBufferReallocate() and NetApiBufferFree() Program Example: A sample console output

 

 

 

 

< Win32 Network Management APIs 26 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 28 >