Give this a try:
Code:
int SetWallpaper(LPCTSTR szPath)
{
HKEY hkRoot,hSubKey;
TCHAR szItemKey[256];
TCHAR szValue[MAX_PATH];
long cb;
char szTargetName[MAX_PATH]; //target file name
char szWinPath[MAX_PATH]; //Windows path
//Get the Windows path
GetWindowsDirectory(szWinPath,MAX_PATH);
//Generate the full path of the target file
_stprintf(szTargetName, _T("%s\\WallPaper.BMP"), szWinPath);
//Copy the selected picture to Windows path
if(!CopyFile(szPath,szTargetName,FALSE))
{
::MessageBox(NULL,_T("Unable to copy file"),_T("ERROR"),MB_OK);
return 0;
}
//Update the registry
_tcscpy(szItemKey,"Wallpaper");
_tcsncpy(szValue,szTargetName);
cb=_tcslen(szValue);
if(RegOpenKey(HKEY_CURRENT_USER,NULL,&hkRoot)==ERROR_SUCCESS)
{
if(RegOpenKeyEx(hkRoot,
_T("Control Panel\\Desktop\\"),
0,
KEY_ALL_ACCESS,
&hSubKey)==ERROR_SUCCESS)
{
RegSetValueEx(hSubKey, (LPCTSTR)szItemKey,
0, REG_SZ,(CONST BYTE *)szValue,cb);
}
else
{
::MessageBox(NULL, _T("Failed to update the registry!"), _T("Error"), MB_OK);
return 0;
}
}
else
{
MessageBox(NULL, _T("Failed to open the registry!"), _T("Error"), MB_OK);
return 0;
}
//Notify Wondows to update the wallpaper
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,NULL,SPIF_SENDCHANGE);
//Close registry
RegCloseKey(hSubKey);
RegCloseKey(hkRoot);
return 1;
}