Skip to content

Commit

Permalink
allow optional values again
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimergp committed Oct 25, 2023
1 parent b8e3aa4 commit 16e64e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
2 changes: 0 additions & 2 deletions menuinst/_legacy/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,4 @@ def create(self, remove=False):
" ".join([str(arg) for arg in args]),
str(workdir),
str(icon),
0,
"",
)
62 changes: 31 additions & 31 deletions src/winshortcut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
static PyObject *CreateShortcut(PyObject *self, PyObject *args)
{
PyObject *py_path; /* path and filename */
wchar_t *path;
PyObject *py_description;
wchar_t *description;
PyObject *py_filename;
wchar_t *filename;

PyObject *py_arguments = NULL;
PyObject *py_iconpath = NULL;
Expand Down Expand Up @@ -62,41 +65,18 @@ static PyObject *CreateShortcut(PyObject *self, PyObject *args)
return NULL;
}

wchar_t *path;
path = PyUnicode_AsWideCharString(py_path, NULL);
if (path == NULL) {
return NULL;
}
wchar_t *description;
description = PyUnicode_AsWideCharString(py_description, NULL);
if (description == NULL) {
return NULL;
}
wchar_t *filename;
filename = PyUnicode_AsWideCharString(py_filename, NULL);
if (filename == NULL) {
return NULL;
}
wchar_t *arguments;
arguments = PyUnicode_AsWideCharString(py_arguments, NULL);
if (arguments == NULL) {
return NULL;
}
wchar_t *workdir;
workdir = PyUnicode_AsWideCharString(py_workdir, NULL);
if (workdir == NULL) {
return NULL;
}
wchar_t *iconpath;
iconpath = PyUnicode_AsWideCharString(py_iconpath, NULL);
if (iconpath == NULL) {
return NULL;
}
wchar_t *app_id;
app_id = PyUnicode_AsWideCharString(py_app_id, NULL);
if (app_id == NULL) {
return NULL;
}

hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER,
Expand Down Expand Up @@ -130,34 +110,53 @@ static PyObject *CreateShortcut(PyObject *self, PyObject *args)
goto error;
}

if (arguments) {
if (py_arguments) {
wchar_t *arguments = PyUnicode_AsWideCharString(py_arguments, NULL);
if (arguments == NULL) {
return NULL;
}
hres = pShellLink->SetArguments(arguments);
if (FAILED(hres)) {
PyErr_Format(PyExc_OSError,
"SetArguments() error 0x%x", hres);
goto error;
}
PyMem_Free(arguments);
}

if (iconpath) {
if (py_iconpath) {
wchar_t *iconpath = PyUnicode_AsWideCharString(py_iconpath, NULL);
if (iconpath == NULL) {
return NULL;
}
hres = pShellLink->SetIconLocation(iconpath, iconindex);
if (FAILED(hres)) {
PyErr_Format(PyExc_OSError,
"SetIconLocation() error 0x%x", hres);
goto error;
}
PyMem_Free(iconpath);
}

if (workdir) {
if (py_workdir) {
wchar_t *workdir = PyUnicode_AsWideCharString(py_workdir, NULL);
if (workdir == NULL) {
return NULL;
}
hres = pShellLink->SetWorkingDirectory(workdir);
if (FAILED(hres)) {
PyErr_Format(PyExc_OSError,
"SetWorkingDirectory() error 0x%x", hres);
goto error;
}
PyMem_Free(workdir);
}

if (app_id) {
if (py_app_id) {
wchar_t *app_id = PyUnicode_AsWideCharString(py_app_id, NULL);
if (app_id == NULL) {
return NULL;
}
hres = pShellLink->QueryInterface(IID_PPV_ARGS(&pPropertyStore));
if (FAILED(hres)) {
PyErr_Format(PyExc_OSError,
Expand All @@ -170,6 +169,7 @@ static PyObject *CreateShortcut(PyObject *self, PyObject *args)
"InitPropVariantFromString() error 0x%x", hres);
goto error;
}
PyMem_Free(app_id);
pPropertyStore->SetValue(PKEY_AppUserModel_ID, pv);
pPropertyStore->Commit();
PropVariantClear(&pv);
Expand Down Expand Up @@ -198,10 +198,6 @@ static PyObject *CreateShortcut(PyObject *self, PyObject *args)
PyMem_Free(path);
PyMem_Free(description);
PyMem_Free(filename);
PyMem_Free(arguments);
PyMem_Free(workdir);
PyMem_Free(iconpath);
PyMem_Free(app_id);

CoUninitialize();
Py_RETURN_NONE;
Expand All @@ -217,6 +213,10 @@ static PyObject *CreateShortcut(PyObject *self, PyObject *args)
pPropertyStore->Release();
}

PyMem_Free(path);
PyMem_Free(description);
PyMem_Free(filename);

CoUninitialize();
return NULL;
}
Expand Down

0 comments on commit 16e64e4

Please sign in to comment.