다음을 통해 공유


Mounted Database Example (Windows CE 5.0)

Send Feedback

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 volume if the volume 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)
{
  CEGUID ceguid;            // Identifier of the mounted volume
  LPWSTR lpBuf;               // Buffer to store the volume name
  TCHAR szError[100];         // String that contains the error message
  DWORD dwError,              // Return value of the 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

  // Use an invalid CEGUID to tell CeMountDBVol to search only on
  // the volume name
  CREATE_INVALID_GUID(&ceguid);

  // Open the MyDBVol database volume on the storage card, if it exists.
  // Create it if it does not exist.
  if (!CeMountDBVol (&ceguid,         // Receives the CEGUID of the mounted volume.
                     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;

  // Use an invalid CEGUID to tell
  // CeEnumDBVolumes to enumerate all volumes.
  CREATE_INVALIDGUID (&ceguid);

  while (!bFinished)
  {
    // Enumerates database volumes.
    if (!CeEnumDBVolumes (&ceguid,      // Receives the CEGUID of the mounted volume
                          lpBuf,        // Buffer to store the database-
                                        // 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 (&ceguid))
      {
        wsprintf (szError, TEXT("ERROR: CeFlushDBVol failed."));

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

See Also

Databases

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.