This is a creation in Article, where the information may have evolved or changed.
Before the installation of the Ubuntu 14.04, also wrote a small program, can boot update desktop wallpaper-"Ubuntu through Bing Wallpaper Automatic Update." As a result, who knows what the Ubuntu version is, a development environment is configured well, will not be able to open the machine ... I can't handle this kind of slag, I can only change Windows 8.
Changed Windows 8 or was thinking about my updated wallpaper program. Windows systems and Linux are different, and there is no command to modify the wallpaper, only to provide programmatic modifications to the Windows API. The development of this science game I am still more familiar with this.
#include "stdafx.h"#include <windows.h>#include <iostream>#include <string>using namespace std;int main(int argc, char** argv){LPWSTR file_path = L"C:\\wallpaper.jpg";int result;result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, file_path, SPIF_UPDATEINIFILE);if (result == TRUE){cout << "Wallpaper set" << result;}else{cout << "Wallpaper not set";cout << "SPI returned" << result;}return 0;}
The changes are also good to do, and then is to get Bing daily updated wallpaper. You need to send an HTTP request to get the list and parse the RSS XML file. I have only used the library of QT has been processed, do this thing do not need to do interface, with such a library meaning is not big. Decisively thought of my big Golang and CGO. My big Golang was designed by the father of C, CGO supports calling C, but does not support C + +.
CGO need MinGW provide GCC compiler, if it is 64-bit system also need mingw64, this can go to download compiled good, the default official website download is 32 bit.
The next is to rewrite the above code to call the C-language package's Golang syntax. The difficulty of calling is to pass in the wallpaper file address. The large Golang language level implements the string, while the C language only uses a character array to represent the string. Starting with a string, and then passing in a pointer to a string, has been unsuccessful.
This function systemparametersinfo
has only four parameters, SPI_ Setdeskwallpaper
is used to set the wallpaper, the second parameter represents the wallpaper location (as if it were ...). ), spif_updateinifile
indicates a direct display after the modification. These three parameters should be correct, all are plastic to the inside pass. The possible error is the file path, which is the lpwstr
type. Check it out, it's defined typedef _null_terminated_ WCHAR *nwpstr, *lpwstr, *pwstr;
, which is typedef wchar_t wchar;
. It is a 2-byte Unicode character type. That is, lpwstr
is a character array, and the function call is a pointer to the array of characters passed in. Then I converted the string to the array []byte
type (the file path will not be in Chinese, so use [] byte
or []rune
have no effect). Then, like the C language, the array name is passed in (in C, the character array name is the pointer to the array). The result is still not good, later found that the Golang array name is not an address ... The large Golang gets the array address should be such &path[0]
.
The big Golang JSON parsing is very simple, I was in the original JSON to parse the XML, the result is not. Later, I made reference to "Go Web programming" by Xie da. I didn't know the exact reason. The back of the XML is then mended.
Compile the program:
go build go_code/wallpaper/win32apigo install go_code/wallpaper/win32apigo build go_code/wallpaper/appgo install go_code/wallpaper/app
Finally, put the compiled program under the directory C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
, you can.
The code covered in this article can be found here. If it is a 64-bit system, you can directly use the compiled files. Mainly in the process of talking about my ideas, concrete implementation, such as CGO these, you can directly see my code, I do not write in detail here.
Reference documents
- "1" Using Go invoke Windows API
- "2" Go WEB Programming-XML processing
- "3" Windows manually add boot entry
- "4" Print the Address of slice in Golang
Original link: With Golang wrote a updated wallpaper small program, reproduced please indicate the source!