Sep 20, 2010 at 1:32pm UTC
You don't check pShellLink for NULL before use and you don't check CoCreateInstance for success.
If you haven't called CoInitialize (as is the case), the CoCreateInstance will fail.
Sep 20, 2010 at 8:46pm UTC
Thank you, it doesn't crash anymore. But I am still having trouble getting it to do what I want, CoCreateInstance() fails:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <windows.h>
#include <shlobj.h>
#include <iostream>
using namespace std;
int main()
{
IShellLink* pShellLink = NULL;
HRESULT hres;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,
IID_IShellLink, (void **)&pShellLink);
if (SUCCEEDED(hres))
{
pShellLink->SetPath("C:\\Python27\\python.exe" ); // Path to the object we are referring to
pShellLink->SetDescription("The Python shell" );
pShellLink->SetIconLocation("C:\\Python27\\python.exe" , 0);
IPersistFile *pPersistFile;
hres = pShellLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile);
if (SUCCEEDED(hres))
{
hres = pPersistFile->Save(L"C:\\test" , TRUE);
pPersistFile->Release();
}
else
{
cout << "Error 2" << endl;
return 2;
}
pShellLink->Release();
}
else
{
cout << "Error 1" << endl;
return 1;
}
return 0;
}
hres returns -2147221008 (shouldnt this be a hex value?). I'm looking up the codes on msdn.
Edit: I've checked all of the return codes for CoCreateInstance and none of them are returned:
http://msdn.microsoft.com/en-us/library/ms686615%28v=VS.85%29.aspx
1 2 3 4 5 6 7 8 9
IShellLink* pShellLink = NULL;
HRESULT hres;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (void **)&pShellLink);
if (hres == S_OK) cout << "OK" << endl;
if (hres == REGDB_E_CLASSNOTREG) cout << "Class not registered" << endl;
if (hres == CLASS_E_NOAGGREGATION) cout << "No Aggregation" << endl;
if (hres == E_NOINTERFACE) cout << "No interface" << endl;
if (hres == E_POINTER) cout << "Pointer" << endl;
Edit: Okay, I figured out the error code:
cout << hex << hres << endl;
This shows the return code to be 0x800401f0 (CO_E_NOTINITIALIZED), which according to the HRESULT return codes means "CoInitialize has not been called.":
http://msdn.microsoft.com/en-gb/library/cc704587.aspx
PROBLEM SOLVED: I needed to call CoInitialize(NULL):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <windows.h>
#include <shlobj.h>
#include <iostream>
using namespace std;
int main()
{
CoInitialize(NULL);
IShellLink* pShellLink = NULL;
HRESULT hres;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL,
IID_IShellLink, (void **)&pShellLink);
cout << hex << hres << endl;
if (SUCCEEDED(hres))
{
pShellLink->SetPath("C:\\Python27\\python.exe" ); // Path to the object we are referring to
pShellLink->SetDescription("The Python shell" );
pShellLink->SetIconLocation("C:\\Python27\\python.exe" , 0);
IPersistFile *pPersistFile;
hres = pShellLink->QueryInterface(IID_IPersistFile, (void **)&pPersistFile);
if (SUCCEEDED(hres))
{
hres = pPersistFile->Save(L"C:\\test.lnk" , TRUE);
pPersistFile->Release();
}
else
{
cout << "Error 2" << endl;
return 2;
}
pShellLink->Release();
}
else
{
cout << "Error 1" << endl;
return 1;
}
return 0;
}
Last edited on Sep 20, 2010 at 9:58pm UTC
Sep 22, 2010 at 1:05am UTC
kbw:
If you haven't called CoInitialize (as is the case), the CoCreateInstance will fail.
Sorry, had I paid better attention I would have figured that out sooner :).
Last edited on Sep 22, 2010 at 1:06am UTC