CaptureStackBackTrace with file information - c++

I'm using CaptureStackBackTrace and SymFromAddr to identify the functions in the callstack.
Is there a way to find the source file of each symbol with this (i can't find any documentation for this)? Or i have to use StackWalk ?

The solution was to use SymGetLineFromAddr which gives the file path and the line number. In order to do this you must set SYMOPT_LOAD_LINES flag before calling SymInitialize. Also the 3'rd argument of SymGetLineFromAddr must be not NULL, otherwise it will crash.
Raxvan.

Related

Can GetFileAttributesW handle url-encoded spaces (%20) in file paths?

Function call:
OS_WRAPI::GetFileAttributesW(file_name_str); // file_name_str value is L"C:\\Test%20Tool\\test.exe"
returns INVALID_FILE_ATTRIBUTES.
Function call:
OS_WRAPI::GetFileAttributesW(file_name_str); // file_name_str value is L"C:\\TestTool\\test.exe"
returns valid attributes.
Both paths exist.
How can I get the file attributes in the case of url-encoded spaces (%20) in the file path?
Can GetFileAttributesW handle spaces in filepaths?
Yes.
Note that L"C:\\Test%20Tool\\test.exe" does not contain a space. Probably you should be passing L"C:\\Test Tool\\test.exe".
The documentation says:
If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError.
So you should, in case INVALID_FILE_ATTRIBUTES is returned, call GetLastError. I'd expect that to return ERROR_PATH_NOT_FOUND.
If the question is actually
Can GetFileAttributesW handle L"%20" in filepaths?
The answer is still yes. If that path really exists, and GetFileAttributesW is returning INVALID_FILE_ATTRIBUTES, then there must be some other problem, but the presence of L"%20" in a file name presents no problems for the Windows API. Again start by calling GetLastError.
Perhaps what's really at issue here, is that you think that Windows uses L"%20" to encode a string in the file system. It does not. On the file system, L"%20" and L" " are two distinct names.

Overcome MAX_PATH filename length

I have read a lot of documentation on this subject, but I can't seem to figure it out.
The cause is that I have to process file paths which may be longer than the MAX_PATH parameter, causing a lot of issues
I have already replaced all my ANSI-functions like GetFileAttributesA with the UNICODE equivalent (GetFileAttributesW) in order to support the extended file path length with the prefix: \\?\.
However, I also need to check whether the file path for instance is a symbolic link and I need to know the filesize, last modified date, etc.
In order to do so, I use the stat function, as shown below:
if (fstat(LongFilePath, &file_info) == 0) //THIS FAILS WITH THE ENAMETOOLONG FOR LONG FILEPATHS
So, here again, the problem is in the ENAMETOOLONG error, due to a too long filename (exceeding MAX_PATH).
So, I found out I could use fstat to access the file by its descriptor. However, to obtain the descriptor, I need to use fopen, which also has the ENAMETOOLONG limitation.
So, my question is. How can I get the file information I need (symlink, filesize, last modified, .... as the stat function offers) for file paths exceeding MAX_PATH

Use relative file path with LoadImage

I am attempting to load a file from a relative file path in win32 via the LoadImage function. The URL I am using definitely exists as I am, for testing purposes, using the same URL as an added bitmap in the resource file.
Image = (HBITMAP)LoadImage(NULL, "..\\..\\Images\\Mage default.bmp",
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
However, it is returning error code 2, indicating the file can't be found. I have googled the issue at some length, and referred to MSDN, and I can't find anything stating how to use a relative file path with LoadImage.
It seems to work fine with LoadBitmap and a pre-defined bitmap, but doesn't seem to work in this case. Any help would be much appreciated
First of all, check that the file exist and current directory is the one you expected.
Also, you can use function like GetFullPathName to convert relative path to absolute.
The relative path you have is computed based on your process/run time working directory and not on your static files location in compile time.
This may be an old post, but having stumbled across this problem (in my case, a header file) and found an answer, here it is:
If referencing a relative path, the first directory is not preceeded by a backslash.
In this example, the line would be:
(HBITMAP)LoadImage(NULL,"Images\\Mage default.bmp",...

when would failbit be set while executing a getline function call in c++

when would getline in c++ fail?
I have a big snippet of code which I am unable to paste in its entirety for multifarious reasons. I am trying to read from a file , which I know exists and contains data, using getline in C++. But getline fails returning error 123-invalid name(output of getlasterror). I looked up the error code which baffles me even more.
I do error check while opening the file. So I am positive that I have the handle to the file.
Please bear with me for not pasting the code. I am new to c++ and especially in windows. Any suggestions or insights about getline would help. I am trying to read a file which is dumped by a compiler.
This is an extract from the code
ifstream inFile("C:\...\ash.txt",ios::in);
string singleLine;
getline(inFile,singleLine);
singleLine is empty ! I am doing something silly..pls point that out to me! appreciate it
IMPORTANT EDIT:
I checked for the ios members and found that fail bit is set. Why would the fail bit be set? the file does exist and also I was wondering if how windows exposes file extensions could cause a problem . That doesnt seem to the problem. What am i missing?
GetLastError only tells you about Win32 API calls, which std::getline is not.
Check the members of the iostream that failed, e.g. rdstate().
Calling ios::exceptions(eofbit | failbit | badbit) before getline and catching the resulting exception might or might not get you a more descriptive error message.
If the fail bit is set, it is probably because you didn't successfully open the file. Check whether ifFile.is_open() returns true; if not, then then probably indicates that the file is not open correctly. You might not have permissions, or you may need to escape the string properly, or the file may be locked.
You should also check if fail is set both before and after the call to getline. If it's before, that probably means that the file isn't open. If it's after, it could mean that the file is empty.
It may be the case that Windows is hiding the true file extension from you. The file name might actually be named ash.txt.txt, for example, if you have Explorer configured to hide file extensions. That might be worth investigating if the file isn't open.
Do you need to escape the backslashes in the file path?
It failed to open the file.
This is becuase you did not specify the correct path.
This is because you used the ancient windows convention of \ as a path separator.
Which also happens to be the escape character in C.
Which is why Windows lets you use / as a path separator (and has done for over a decade) because the use of '/' is so error prone.
Your path should be:
ifstream inFile("C:\\...\\ash.txt");
// Or my preference
ifstream inFile("C:/.../ash.txt");
Or even better use boost.
Getline IIRC is meant for c_strings and the string is the first argument, not the second.
http://www.cplusplus.com/reference/iostream/istream/getline/
You're attempting to use a normal string, which (I could be wrong) doesn't work with getline.

How to get the path of the exexuter in C++?

I am using Visual studio 2008 and I want to get the absolute path of the .exe file?
meaning when the user opens the exe file, I need to know its absolute path??
thanks in advance
Under Windows try the following:
char ExeName[8192]; // or what ever max. size you expect.
if (0 != GetModuleFileName (NULL, ExeName, sizeof (ExeName)))
{
printf ("Your array was probably not large enough. Call GetLastError for details\n");
}
If you compile for unicode use wchar_t.
Using the _pgmptr or _wpgmptr global variable is probably the easiest way.* (They're in stdlib.h.)
*Note: Under some rather rare circumstances, it's possible that this won't work... in that case, use GetModuleFileName(NULL, ...);
If you want to obtain a path of the current process, you should use API function:
GetModuleFileName
But, if you want to obtain a full path of the process that is not written by you, use
GetModuleFileNameEx
Above function expects one argument more than GetModuleFileName - it is a HANDLE of a process which path is supposed to be obtained. It is explained in more details on MSDN.