Mounted Database Example

The following code example shows how to mount the MyDBVol database volume. The example opens the database volume if the volume already exists, or it creates the database if the database does not exist. The example also shows how to enumerate all of the mounted database volumes and transfer the data into permanent storage.

void MountingDBVolume (void)
{
  PCEGUID pceguid;            // Pointer to the mounted volume
  LPWSTR lpBuf;               // Buffer to store the volume name
  TCHAR szError[100];         // String to display with error message
  DWORD dwError,              // Return value of GetLastError function
        dwNumChars;           // Length of the buffer in characters
  TCHAR * szFname = TEXT("\\Storage Card\\MyDBVol");
                              // Name of the database volume
  BOOLEAN bFinished = FALSE;  // Loop (enumeration) control

  // Allocate memory for pceguid.
  pceguid = (PCEGUID) LocalAlloc (LPTR, sizeof (CEGUID));

  // Open the database volume MyDBVol on the storage card if it exists.
  // Create it if it does not exist.
  if (!CeMountDBVol (pceguid,         // Pointer to a CEGUID.
                     szFname,         // Database volume name.
                     OPEN_ALWAYS))    // Create the database volume
                                      // if it does not exist.
  {
    // Your error-handling code goes here.

    // If the database volume was not opened or created
    wsprintf (szError, TEXT("ERROR: CeMountDBVol failed (%ld)"),
                        GetLastError());
  }

  // Allocate memory for lpBuf.
  lpBuf = (LPWSTR) LocalAlloc (LPTR, MAX_PATH);

  // Assign MAX_PATH to dwNumChars.
  dwNumChars = MAX_PATH;

  // Create an invalid pceguid as the input parameter of
  // CeEnumDBVolumes.
  CREATE_INVALIDGUID (pceguid);

  while (!bFinished)
  {
    // Enumerates database volumes
    if (!CeEnumDBVolumes (pceguid,      // Points to a mounted volume
                          lpBuf,        // Buffer to store the 
                                        // volume name
                          dwNumChars))  // Length of buffer in char

    {
      // If error occurs in enumerating

      bFinished = TRUE;    // Finished enumerating

      dwError = GetLastError();

      wsprintf (szError, TEXT("ERROR: CeMountDBVol failed (%ld)"),
                          dwError);

      if (dwError == ERROR_INSUFFICIENT_BUFFER)
      {
        // To avoid having to restart the enumeration,
        // free lpBuf and reallocate a bigger buffer.

        bFinished = FALSE;  // Continue with enumerating
        LocalFree (lpBuf);  // Free lpBuf

        // Your code to reallocate a bigger buffer goes here.
        // ...
      }
    }
    else  // Succeeded in enumerating
    {
      // Flush the database volume's data to permanent storage.

      if (!CeFlushDBVol (pceguid))
      {
        wsprintf (szError, TEXT("ERROR: CeFlushDBVol failed."));

        // Your error-handling code goes here.
      }
    }
  }
} // End of MountingDBVolume example code