0

I'm developing an application to randomly change the users wallpaper at a specific time interval. Everything works as expected, except for the occasional error of 0 set by SystemParametersInfo(), which results in the wallpaper being reset to a black screen.

The 0 error is supposed to denote the operation was successful, yet that's obviously not the case. This error does not occur every time a new background is set, but randomly. I've searched this website and many others for possible solutions.. to no avail.

void setBackground() {
dirSize = FindMaxFileNum(); //Reads num of .jpg's in the folder.

if(dirSize != -1)
{ 
  srand( (unsigned int)time(NULL));
  bgChoice = rand() % dirSize + 1; 

  //Ensures backgrounds don't repeat. 
  for(int i=0; i<=2; i++) 
  {
    if(lastBackground[i] == bgChoice)
    { bgChoice = rand() % dirSize + 1; }
  }
 //Tracks the last background used. 
 lastBackground[bgCount] = bgChoice; 
 bgCount++; 
 //Resets the counter so we can track the last 3 bg's used. 
 if(bgCount == 2)
 { bgCount = 0; }
}
else 
{ 
  dwLastError = GetLastError();
  ssLastError << "Error: " << dwLastError << "."; 
  MessageBox(NULL, ssLastError.str().c_str(), "Error!", MB_ICONEXCLAMATION | MB_OK); 
  ssLastError.str("");
}
ssFilePath.str("");          //Ensures the stringstream is clean. 
ssFilePath << strLocalDirectory << bgChoice << ".jpg";     

if( SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)ssFilePath.str().c_str(), 
     SPIF_UPDATEINIFILE | SPIF_SENDCHANGE) != 0)
{
  // Refresh the desktop - cleaning of PIDL is in the WM_DESTROY message.       
  SHGetSpecialFolderLocation(NULL,CSIDL_DESKTOP,&pidl);
  SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,pidl,0);
}
else 
{
  dwLastError = GetLastError();
  ssLastError << "Error: " << dwLastError << ".";
  MessageBox(NULL, ssLastError.str().c_str(), "Error!", MB_ICONEXCLAMATION | MB_OK); 
  ssLastError.str("");
} 

}

4

0 回答 0