你好,我想写一个小程序来更改 Windows 7 中的壁纸
我想使用以下代码:
#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;
}
但此代码不会更改壁纸,它仅在调用ApplyChanges后给我提供hresult错误代码80070002。
我做错了什么请帮忙
请将您的主入口函数更改为
int main(int argc, char* argv[])
to
int wmain(int argc, wchar_t* argv[] )
不需要像这样的铸造wchar_t* file = (wchar_t*)argv[1];
它会像你一样工作wmain 参数已经在 wchar_t* 中 http://msdn.microsoft.com/en-us/library/fzc2cy7w%28v=vs.80%29.aspx
我能够使用你的代码和我的修改并更改我的电脑壁纸
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)