URLDownloadToFile to the same directory - c++

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.

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

URLDownloadToFile QtCreator file not downloading

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

c++ GetPrivateProfileString read ini file from current directory

I'm creating a dll on c++. It is a Visual Studio project. The dll reads some data from ini file. I have decided to use GetPrivateProfileString function. It works almost completely. It does not see file in current directory. How can I provide this parameter (variable called path)?
How can I pass last parameter (path)
Code:
LPCTSTR path = L"\\test.ini";
TCHAR protocolChar[32];
int a = GetPrivateProfileString(_T("Connection"), _T("Protocol"), _T(""), protocolChar, 32, path);
String from test.ini:
[Connection]
Protocol = HTTP
I also tried this:
LPCTSTR path = L"test.ini";
But it did not help me
LPCTSTR path = _T(".\\test.ini");
. symbolises current directory. Hope this will work for you.
WCHAR cfg_IniName[256];
GetCurrentDirectory (MAX_PATH, cfg_IniName );
wcscat ( cfg_IniName, L"\\test.ini" );
way to get full path

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/").

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.