S
S
Sergey2014-02-23 22:48:48
C++ / C#
Sergey, 2014-02-23 22:48:48

How to create a shortcut using winapi in C++?

It does not work to create a shortcut using winapi in C ++. Below is the code and error. Please tell me what to do?

#pragma comment(lib, "Ole32.lib")
#include <iostream>
#include <shlobj.h>
#include <objidl.h>
#include <objbase.h>
#include <windows.h>
 
using namespace std;
 
int main()
{
 
    HRESULT hres;
    IShellLink *psl = NULL;
    IPersistFile *pPf = NULL;
 
    CoInitialize(NULL);
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,reinterpret_cast<void**>(&psl));
    if (SUCCEEDED(hres))
    {
        psl->SetPath("C:\\test\\SmcServer.exe");
        psl->SetWorkingDirectory("C:\\test");
        psl->SetDescription("desc ololo");
        psl->SetArguments("");
        psl->SetShowCmd(SW_SHOWNORMAL);
 
        psl->QueryInterface(IID_IPersistFile, (void**)&pPf);
        pPf->Save(reinterpret_cast<LPCOLESTR>("C:\\test.ink"), true);
    }
 
    CoUninitialize();
    return 0;
}

14bf39b3a2ca452abfc552ab59df1b2f.PNG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Komarov, 2014-02-23
@maNULL

Of course, I'm not strong in C, but if you take a quick look, it also swears at line 28, in which the path to the shortcut. labels in the windows have the extension lnk, not ink. try it, maybe the dog is buried here)

D
Dmitry, 2014-02-24
@peleron

The linker does not find implementations of the specified procedures - CoInitialize, CoCreateInstance, etc.
1. Option: You need to add the path to the Ole32.lib library in the compiler options.
But as far as I understand, in the case of the mingw compiler, you will not be able to statically link this library, so option 2 or build in Visual Studio
2. Do dynamic compilation and during launch the application, slip it the library Ole32.dll

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question