-2

Intro

First of all, I would like to say that I have read through the previous answers for this type of question, including this excellently written one.

However, I do not understand enough about C++ to be able to use the more "advanced" fixes.

I have ensured that the right type of console has been selected (Console (/SUBSYSTEM:CONSOLE) for those interested), and have the required imports with the possible exception of an IDL mentioned somewhere (that falls into the lack of understanding category).

If this is a duplicate, I would be more than happy to use the post I duplicated, but I have not been able to find anything that can help someone of my skill level.

Technical Information

IDE: Visual Studio

Platform: Windows

Code

headers.h

#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <Shobjidl.h>
#include <time.h>
#include <stdlib.h>
#include <tchar.h>

main.cpp

#include "headers.h"
using namespace std;

int main() {
    string x = "C://Users/student/Desktop/i-should-buy-a-boat.jpg";
    x.c_str();

    wstring tempx = std::wstring(x.begin(), x.end());
    LPCWSTR sw = tempx.c_str();
    HRESULT SetWallpaper(
        LPCWSTR monitorID,
        LPCWSTR wallpaper
        );
    SetWallpaper(NULL, sw);
}
Community
  • 1
  • 1
Koga
  • 523
  • 4
  • 13
  • 1
    This isn't advanced. You declared a function and never defined it. This is explained in the Q&A you linked to. COM supports late-binding. COM does not, however, support guess-based binding. Your pseudo-COM declaration is bogus. You need to read [The Component Object Model](https://msdn.microsoft.com/en-us/library/windows/desktop/ms694363.aspx). All of it. And never, **never ever** again must you convert an MBCS string to a wide character string by simply widening that character data type, completely ignoring character encoding. – IInspectable Jul 20 '16 at 18:02
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – IInspectable Jul 20 '16 at 18:02
  • @IInspectable, thank you for the resource! I'll have a read through it and see if that helps. Thanks! – Koga Jul 20 '16 at 18:05
  • And by the way, the path separator on Windows is "\". "//" is meaningless, too. – IInspectable Jul 20 '16 at 18:14
  • All your path separators are wrong. – Jonathan Potter Jul 20 '16 at 18:29
  • 1
    Bizarre that you declare the function and hope that somebody else might implement it for you – David Heffernan Jul 20 '16 at 19:26

1 Answers1

3

SetWallpaper() is not a standalone function exported by the Win32 API. It is a method of the IDesktopWallpaper interface (see here).

So you need to use code that is more like this instead:

#include "headers.h"

int main()
{
    std::wstring x = L"C:\\Users\\student\\Desktop\\i-should-buy-a-boat.jpg";

    CoInitialize(NULL);

    IDesktopWallpaper *p;
    if (SUCCEEDED(CoCreateInstance(__uuidof(DesktopWallpaper), 0, CLSCTX_LOCAL_SERVER, __uuidof(IDesktopWallpaper), (void**)&p)))
    {
        p->SetWallpaper(NULL, x.c_str());
        p->Release();
    }

    CoUninitialize();
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
RbMm
  • 31,280
  • 3
  • 35
  • 56