URLDownloadToFile QtCreator file not downloading - c++

My issue today involve the usage of URLDownloadToFile() with QtCreator. I use the following code to download:
HRESULT hRez = URLDownloadToFile(NULL, TEXT(url), TEXT("C:\\image.png"), 0, NULL);
if (hRez != S_OK)
{
return false;
}
The code run with no errors however the file ins't downloaded. I've been searching through many solutions for my issue but none have been relevant not successful. What have I done wrong?

Well the documentation states:
szFileName:
A pointer to a string value containing the name or full path of the file to create for the download.
If szFileName includes a path, the target directory must already exist.
So I would assume this parameter needs to include not only a path but a filename as well. For instance
HRESULT hRez = URLDownloadToFile(NULL, TEXT(url), TEXT("C:\\downl.tmp"), 0, NULL);

Related

How to get a PIDL from a Windows library's GUID?

How can I get the PIDL of a library from its GUID?
For example, if I have the GUID of the Documents library ("{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}"), how can I convert that into the library's ID list?
I thought SHParseDisplayName would do the job, but it returns "file not found."
Bear in mind that what I need is the PIDL of the library, not of its default folder.
This is straight C++, no .Net.
TIA
Edit: This is the code that works, from the response below (without error checks). guid is a GUID string prepended with 'shell:::', e.g., 'shell:::{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}'.
IShellFolder* pDesktop;
LPITEMIDLIST pidl;
SHGetDesktopFolder(&pDesktop);
pDesktop->ParseDisplayName(nullptr, nullptr, guid, nullptr, &pidl, 0);
Edit 2: Even easier: SHParseDisplayName works if the 'shell:::' is prepended:
SHParseDisplayName(guid, nullptr, &pidl, 0, nullptr);
According to the documentation for IShellFolder::ParseDisplayName you can simply pass it a filename in the form ::{GUID} if you are using the desktop folder.
Edit: the documentation appears to be incomplete, according to this answer you need to add shell: to the start of the string.
p->ParseDisplayName(m_hWnd, NULL, _T("shell:::{7B0DB17D-9CD2-4A93-9733-46CC89022E7C}"), NULL, &pidl, NULL);
You want the function SHGetKnownFolderIDList.
Given a KnownFolderID guid (e.g. FOLDERID_DocumentsLibrary - {7B0DB17D-9CD2-4A93-9733-46CC89022E7C})
returns you the absolute pidl of that KNOWNFOLDER
For example:
PIDLIST_ABSOLUTE pidl;
HRESULT hr = SHGetKnownFolderItem(FOLDERID_DocumentsLibrary, 0, NULL, out pidl);
Bonus Chatter
There are three shell functions for dealing with KNOWNFOLDERs
SHGetKnownFolderPath : Get the full path of a known folder (e.g. C:\Users\Chris\Documents)
SHGetKnownFolderIDList: Get the absolute pidl of a known folder
SHGetKnownFolderItem: Get the IShellItem of a known folder

Download file to temp dir in C++

I'm trying to download a file from the internet to my temp directory.
This is what I have so far:
HRESULT hr;
LPCTSTR Url = _T("linkhere"), File = _T("C:\\test.exe");
hr = URLDownloadToFile (0, Url, File, 0, 0);
This is working fine.
How do I save the file to the temp directory (using GetTempPath)
Since the directory that's used to store temporary files is configurable, you shouldn't hardcode its path into the application. Instead, use GetTempPath() to ask for the path and then use the result as a prefix, meaning you can simply append "temp.exe" to it.
(Btw, you can use "/" instead "\", since constantly having to escape backslashes is both tedious and error prone. For example, instead of "some\random\path\", you write "some/random/path/").

URLDownloadToFile to the same directory

I am trying to achieve saving a random file from the net to the same directory where the .exe is located. The problem is that I only got it working when specifying the absolute directory.
The last code I tried was:
string home;
home = system("echo %HOMEDRIVE%%HOMEPATH%/aaa.gif");
HRESULT hr = URLDownloadToFile ( NULL, _T("http://stackoverflow.com/gif.gif"), (TCHAR*)home.c_str(), 0, NULL );
Also I tried:
HRESULT hr = URLDownloadToFile ( NULL, _T("http://stackoverflow.com/gif.gif"), "/aaa.gif", 0, NULL );
But it isn't working neither.
How can I sort it out? Thanks :)
Try using _T(".\\aaa.gif") as the file name. This will use the current directory for the file storage location. Alternatively you can also use GetModuleFileName to get the execution path of the exe and work out the path name for your save file.

How to i execute application in vc++ that reads data from the installation directory

How to execute an .exe from c++ console application. I have try the following methods none have worked.
I want to run a application i have created a while back "Radio.exe". this application reads an xml file at startup, if I manually run this application it works fine, but if run it via console application "Radio.exe" cant find the xml file?
void execute( char* path)
{
// cant find xml ?
ShellExecuteA( NULL, NULL, path, NULL, NULL, SW_SHOW );
// cant find xml ?
ShellExecute(NULL, NULL, path, NULL, NULL, SW_SHOWNORMAL);
// and also cant find xml ?
SHELLEXECUTEINFO rSEI ={0};
rSEI.cbSize=sizeof( rSEI );
rSEI.lpVerb= NULL;
rSEI.lpFile= "C:\\Users\\me\\Documents\\Radio.exe"; // = path
rSEI.lpParameters= 0;
rSEI.nShow = SW_NORMAL;
rSEI.fMask = SEE_MASK_NOCLOSEPROCESS;
ShellExecuteEx( &rSEI );
}
You need to set your working directory, lpDirectory:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx
You should probably set this up to be path relative too so you don't have to provide an absolute path.
Example:
C:\projects\Radio
C:\projects\Radio\radio.exe
C:\projects\Radio\radio.xml
Assuming your program, which spawns radio.exe, is in the projects directory. YOu can provide the Radio directory as the relative path.

Why is CreateFile failing to open a file across a network share?

I wrote a small program which frequently opens small, user text files and until now haven't encountered any problems with read/write access or any sorts of conflict. The files are selected in another piece of software which I have no control over, and are passed to me as a string.
When attempting to open a file from a mapped network drive I am getting a "The system cannot find the path specified" error (GetLastError() = 3).
The call is shown below, *iNCfileName = "z:\\Validation\\Sample Files\\1_1-4 120MM.CC", where Z: is a mapped folder on our domain.
iNCfile = CreateFile( iNCfileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if ( iNCfile == INVALID_HANDLE_VALUE )
{
string msg; // lots of better ways to get this printed ... but ...
dw = GetLastError();
msg = iNCfileName;
msg += ": ";
msg += _com_error(dw).ErrorMessage();
print_error(dw , (char*)msg.c_str() );
return 102;
}
The file opens from my program if I copy it to the local hard drive. It also opens in notepad from the mapped drive.
Could this be a problem between the "Z:\whatever.txt" mapped representation and the true file name (\mydomain\Validation\S....??)?
If so, how can I convert from one to the other in a programmatic way (assume I won't know the domain/share names ahead of time)?
If it makes any difference I use VS2010 and the application executes on a Win XP machine.
Related: my follow up question
I've encountered this before. When using a path like \\DOMAIN\PATH\FILE.TXT I had to first call WNetAddConnection2().
Here is my code (of course you can exclude the NULL members):
NETRESOURCE nr = {0}; //new structure for network resource
nr.dwType = RESOURCETYPE_ANY; //generic resource (any type allowed)
nr.lpLocalName = NULL; //does not use a device
nr.lpRemoteName = "\\\\DOMAIN\\PATH\\FOLDER"; //For me, this pointed to an account's documents folder, and from there I could use a subfolder
nr.lpProvider = NULL; //no provider
DWORD ret = WNetAddConnection2 (&nr, NULL, NULL, CONNECT_TEMPORARY); //add connection
Don't forget the header and library.
I just had the same issue; trying to create a file using API CreateFileW under a mapped drive ( Z:\folder ) did not worked; howerver, after researching this subject i tried to create the file using the real path ( \\Shared_computer_name\folder\ ) immediately worked successfully.
Now I have to work a function to retrieve the real name of a mapped drive, to use it when necessary... just found WNetGetUniversalName, have to make it to work.