Unable to call `SetWallpaper()` due to error LNK2019 - c++

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);
}

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;
}

Related

AnsiString does not work (AnsiString identifier is not defined)

Here's the code:
AnsiString path = "BrowserBot.exe";
ShellExecute(0, TEXT("open"), path.c_str(), TEXT("-parametr"), 0, SW_SHOW);
Writes an error that the AnsiString identifier is not defined. I don't know what the problem is.
All connected libraries:
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <sstream>
AnsiString is a string class that is specific to the C++Builder compiler. If you are using that compiler, make sure you are compiling your project with C++Builder's VCL (Visual Component Library) or FMX (FireMonkey) framework enabled, and that you have a corresponding #include <vcl.h> or #include <fmx.h> statement in your C++ code.
Otherwise, if you are using any other compiler, you should use the standard C++ std::string class instead (which can also be used in C++Builder), eg:
#include <string>
std::string path = "BrowserBot.exe";
ShellExecuteA(0, "open", path.c_str(), "-parametr", 0, SW_SHOW);

How to call method EnableStatic() in C++?

Like in the title.
I'm a not very experienced with classes and I have hard time with calling the function EnableStatic() from Win32_NetworkAdapterConfiguration class in C++. So how to do it?
I would really appreciate an example or simple program with it!
Additional Info:
•I've tried use this part of calling code:
#define _WIN32_DCOM
#include <iostream>
#include <winsock.h>
#include <string>
#include <stdint.h>
#include <comdef.h>
#include <Wbemidl.h>
int maint()
{
UINT ipaddr = inet_addr("155.34.22.0");
UINT subnet = inet_addr("255.255.255.0");
Win32_NetworkAdapterConfiguration Adapter;
Adapter.EnableStatic(ipaddr,subnet);
system("pause");
}
•The method's website:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa390383(v=vs.85).aspx
If anyone interested I'm inviting for a C++ project on GitHub

C++ TlHelp32.h not working?

I've included the TlHelp32.h header properly though I keep getting the message:
"Error: identifier "CreateToolhelp32Snapshot" is undefined"
when attempting to use CreateToolhelp32Snapshot. When I used the "peek definition" feature in VS I found that there are errors within this header where in certain areas it says:
"Error expected a ';'"
Any ideas how to fix this?
#include "stdafx.h"
#include <TlHelp32.h>
#include <Windows.h>
#include <iostream>
using namespace std;
class Functions{
public:
void playerHealthPrinter(){
HANDLE hProcess;
DWORD dwPID, dwProtection, dwCaveAddress;
BOOL bPOn, bIOn, bProt;
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
Never include Win32 headers before <windows.h>. The following should work, instead:
#include <Windows.h>
#include <TlHelp32.h> // <-- include *after* windows.h
If it still doesn't work then please post an MCVE including the relevant headers and version of VS.

using a dll in ezcad with c++

I have one dll explicitly link and I would like to use.
I create this code:
#include "StdAfx.h"
#include <windows.h>
#include <iostream>
#include <tchar.h>
#include <MarkEzdDll.h>
#include < TCHAR.H >
#include<HLink.h>
typedef int (__stdcall *Thiago)();
int main()
{
HINSTANCE hEzdDLL = LoadLibrary(_T("C:\\Users\\Thiago\\Desktop\\DLL\\Debug\\MarkEzd.dll"));
Thiago lmc1_Initial = (Thiago)GetProcAddress(hEzdDLL , "lmc1_Initial");
lmc1_Initial();
}
But not fuction, the error this:
Unhandled exception at 0x10007f76 (MarkEzd.dll) in DLL.exe: 0xC0000005: Access violation reading location 0x00000004.
what I do?
Check for the return error code, if it's NULL you have an error.
According to these sources (and assuming that's the same library you're using), the signature is actually:
typedef int (*LMC1_INITIAL)(TCHAR* strEzCadPath, BOOL bTestMode, HWND hOwenWnd);

Ownership issue with Clang frontend API

I'm using the Clang C++ API. As the API does not use smart pointers correctly, I've struggled with ownership. I stomped on all the issues I found so far on my own, but this one is vexing me. When the code executes, I get an access violation. I'm fairly certain it's a double delete, but since the documentation is nonexistent, I've got little idea where to look. Fortunately, the reproducing program is rather short. Any suggestions?
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(push, 0)
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Sema/Lookup.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/Mangle.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Sema/CodeCompleteConsumer.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/DataTypes.h>
#include <llvm/Module.h>
#include <llvm/Support/Host.h>
#pragma warning(pop)
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
llvm::LLVMContext c;
llvm::Module m("", c);
clang::EmitLLVMOnlyAction emit(&c);
emit.setLinkModule(&m);
clang::CompilerInstance CI;
std::string errors;
llvm::raw_string_ostream error_stream(errors);
clang::DiagnosticOptions diagopts;
clang::TextDiagnosticPrinter printer(error_stream, &diagopts);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
clang::DiagnosticsEngine engine(diagids, &diagopts, &printer, false);
CI.setDiagnostics(&engine);
CI.createFileManager();
CI.createSourceManager(CI.getFileManager());
llvm::raw_null_ostream empty;
clang::PrintingCodeCompleteConsumer print(CodeCompleteOptions(), empty);
clang::TargetOptions target;
target.Triple = llvm::sys::getDefaultTargetTriple();
CI.setTarget(clang::TargetInfo::CreateTargetInfo(engine, &target));
CI.createPreprocessor();
CI.createASTContext();
clang::SemaConsumer* cons = new clang::SemaConsumer();
CI.setASTConsumer(cons);
CI.createSema(clang::TranslationUnitKind::TU_Complete, &print);
cons->InitializeSema(CI.getSema());
clang::FrontendInputFile f("header", clang::InputKind::IK_CXX, true);
emit.BeginSourceFile(CI, f);
emit.Execute();
emit.EndSourceFile();
emit.takeModule();
clang::LookupResult lr(CI.getSema(), clang::DeclarationName(CI.getPreprocessor().getIdentifierInfo("function")), clang::SourceLocation(), clang::Sema::LookupNameKind::LookupOrdinaryName);
auto result = CI.getSema().LookupName(lr, CI.getSema().TUScope);
std::string temp;
llvm::raw_string_ostream out(temp);
CI.getASTContext().createMangleContext()->mangleName(lr.getFoundDecl(), out);
auto fun = m.getFunction(temp);
std::cout << fun->getName().str();
return 0;
}
Edit: Also, did I mention that if I change it so that the file actually exists, even if it's a trivial program, Clang fails with an access violation whilst executing the action, even before the previous one in EndSourceFile. Whyyyyyy.
I eventually solved this by skipping the entire Frontend, as that's mostly where all of the dodgy ownership lies, and simply calling the functions I need myself.