hi i want to write a little program to change the wallpaper in windows 7
i wanted to use the following code:
#include "windows.h"
#include "wininet.h"
#include "shlobj.h"
#include "wchar.h"
#include <iostream>
void SetWallpaper(LPCWSTR file){
CoInitializeEx(0,COINIT_APARTMENTTHREADED);
IActiveDesktop* desktop;
HRESULT status = CoCreateInstance(CLSID_ActiveDesktop,NULL,CLSCTX_INPROC_SERVER,IID_IActiveDesktop,(void**)&desktop);
WALLPAPEROPT wOption;
ZeroMemory(&wOption, sizeof(WALLPAPEROPT));
wOption.dwSize=sizeof(WALLPAPEROPT);
wOption.dwStyle = WPSTYLE_CENTER;
status = desktop->SetWallpaper(file,0);
wcout << status << endl;
status = desktop->SetWallpaperOptions(&wOption,0);
wcout << status << endl;
status = desktop->ApplyChanges(AD_APPLY_ALL);
wcout << status << endl;
desktop->Release();
CoUninitialize();
}
int wmain(int argc, wchar* argv[]){
if(argc<=1){
wcout << "use: " << argv[0] <<" path_to_pic.bmp" <<endl;
}else{
wchar_t* file = argv[1];
SetWallpaper(file);
}
getchar();
return 0;
}
but this code does not change the wallpaper, it only gives me the hresult error-code 80070002 after calling ApplyChanges.
what am i doing wrong please help
Here's a promising-looking piece of code http://answers.google.com/answers/threadview/id/512662.html though I haven't tested it myself:
Please change your main entry function from
to
No casting is necessary like
wchar_t* file = (wchar_t*)argv[1];
and it will just work as your wmain arguments are already in wchar_t*I was able to use your code and my modification and change my computer wall paper
Code to change your wallpaper with two (or multiple , add more conditions) images ..