How to create a file with UNICODE path on Windows with C++ - c++

I am wondering which Win32 API call is creating the files with UNICODE path. Just to make sure, I am not talking about the content here only the file path. I would appreciate if somebody would hit me with an MSDN url, my google fu failed this time.
Thanks a million in advance.

See CreateFile msdn link: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx, if you pass a unicode string to the lpFileName parameter then the unicode version of CreateFile will be used.
Also you need to open the file in binary mode see this discussion on msdn forum: http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/71fa98ca-e757-4099-8f7f-fefcfe645298 which points to this msdn article: http://msdn.microsoft.com/en-us/library/c4cy2b8e%28vs.71%29.aspx

The principal tag on this question is "c++" not "windows.
Now, unless that was just a all-too-common cheat to get a windows api question in front of a larger - mostly unrelated - audience, the answer should be slightly relevant to c++.
As such, in a C++ app, there are a number of std:: specializations that can take wchar_t. wofstream for example.

Related

Get file's Previous Versions, from WINAPI

In Windows 7 theres a possibility for getting file's previous versions like in the below image:
Is there any way to retrieve file's previous version by code? because I couldn't find any API.
Thanks advanced! =]
There are several tags listed with this question. So it is unclear if a strictly c/c++ approach is desired, or if scripting etc will work. In any case...
Here are some links that will hopefully point in the right direction:
On the MSDN site, there is documentation and example code referring
to shadow
copy
API.
Here is a Link to the
concept
of shadow copy service.
Here is a description of how you can command line, or program
script to recover
files from shadow copy.
Using the API link above with the structures found
here
will provide you with a way to get information about a particular
file, volume etc.
Finally, Here is a link talking about the Volrest utility from
Windows Server 2003 Resource Kit
tools, including
information on how you can "see a list of available previous
versions of [a] folder".
So after some searching, and thanks to #ryyker and #Ben directions I was able to find out the answer:
For example for file: C:\SomeFolder\SomeFile.exe
From cmd (run as administrator):
vssadmin list shadows for=C:\
For programmatic solution you can run it with:
CreateProcessW(NULL,L"cmd.exe /c \"vssadmin list shadows for=C:\\ > C:\\someTmpFile.txt\"",...);
Read and parse the created file.
Here above you get a list of shadow copies (kind of "Previous versions" containers).
Refer to the appropriate "Shadow Copy Volume" row (the version you want) and append the remaining file path after the volume name:
\\ Previous version path = \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy3\SomeFolder\SomeFile.exe"
wchar_t* prevPath = L"\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy3\\SomeFolder\\SomeFile.exe";
Now you can read the file with the well known WIN32API functions CreateFile and ReadFile. (Create and Read file example from MSDN: EXAMPLE)
Make sure to use the UNICODE versions of that functions as the ASCII version may lack support of "\?\" paths.
Good luck! =]

Where is LPPICTURE defined?

I'm trying to write a function to load a JPEG, and I've downloaded the sample code from MSDN Q218972, but it is using precompiled headers and I am not.
I've spent hours looking for where LPPICTURE and OleLoadPicture() are defined but can't find it anywhere.
Does anyone know which header I need to include for these two?
In future, is there any resource I can use to find this information? MSDN documents the .NET world extremely well and it's often trivial to find what I need to include, but I'm finding that it takes longer to identify headers to include for my Win32 program than to learn how to write a Win32 program!
OleLoadPicture() is defined in OleCtl.h and implemented in OleAut32.dll. The import library is OleAut32.lib.
The information is found towards the bottom of the MSDN topic.
As for LPPICTURE, that is also defined when you include OleCtl.h.

Find out character at particular console location

Is there any way to read a character from a specific location on the console? For example Read_Console_Char(10, 40);?
I don't know if there is a standard way to do this, but in Windows environment you can do that.
I've never read from arbitrary location, just writing to it with the use of SetConsoleCursorPosition and GetStdHandle. I Just looked on MSDN and there are related API's to get the contents, such as ReadConsoleOutput. Here is the link to it: ReadConsoleOutput example.
Hope that helps

Check file name is valid windows name

I wanna check if my string is valid windows file path. I was searching around and it seems that there is no reliable method to do that. Also I checked boost filesystem library , and no obvious function exist to do this check , maybe something like is_valid_windows_name
You could use _splitpath() function and parse the output (based on it, you could easily say if your path is valid or not).
See MSDN for additional information.
Note that this function is windows-specific.
I do not believe there is a standard c++ api for that.
Note that the Windows API allows more filenames than the Windows Shell (The filenames the user is allowed to use in windws explorer).
You should have a look at the windows shell api.
Another possibility is to use trial and error, this way you are truly independend of the current filesystem.
The easiest way is to disallow
\ / < > | " : ? *
and you should be fine.
Yes, there is a boost function that does what you want. Take a look at boost::filesystem::windows_name(...). You will need to include boost/filesystem/path.hpp as well as link against the correct (version- and architecture-specific) libboost_system and libboost_filesystem libraries since path is not a header-only lib.
It's a pity even the newest C++17 filesystem library doesn't have a function to verify file names.
You can use the Windows-specific Shell Lightweight Utility function PathFileExists or the Windows API GetFileAttributes and check the last error code specifically for ERROR_INVALID_NAME.
I think it's kind of a misuse (because there really should be a dedicated function for it) but serves the purpose.

On Windows, when should you use the "\\\\?\\" filename prefix?

I came across a c library for opening files given a Unicode filename. Before opening the file, it first converts the filename to a path by prepending "\\?\". Is there any reason to do this other than to increase the maximum number of characters allowed in the path, per this msdn article?
It looks like these "\\?\" paths require the Unicode versions of the Windows API and standard library.
Yes, it's just for that purpose. However, you will likely see compatibility problems if you decide to creating paths over MAX_PATH length. For example, the explorer shell and the command prompt (at least on XP, I don't know about Vista) can't handle paths over that length and will return errors.
The best use for this method is probably not to create new files, but to manage existing files, which someone else may have created.
I managed a file server which routinely would get files with path_length > MAX_PATH. You see, the users saw the files as H:\myfile.txt, but on the server it was actually H:\users\username\myfile.txt. So if a user created a file with exactly MAX_PATH characters, on the server it was MAX_PATH+len("users\username").
(Creating a file with MAX_PATH characters is not so uncommon, since when you save a web page on Internet Explorer it uses the page title as the filename, which can be quite long for some pages).
Also, sharing a drive (via network or usb) with a Mac or a Linux machine, you can find yourself with files with names like con, prn or lpt1. And again, the prefix lets you and your scripts handle those files.
I think the first thing to note is that "\\?\" does not make the path a UNC path. You were more accurate the second time when you called it a UNC-style path. But even then, the similarity only comes from having two backslashes at the start. It really has nothing to do with UNC. That's backed up by the fact that you have to use even more characters to get a UNC path with the "\\?\" prefix.
I think you've got the entire reason for using that prefix. It lifts the maximum-length limit as described in the article you cited. And it only applies to Unicode paths; non-Unicode paths don't get to avoid the limit by using that prefix.
One thing to note is that the prefix is not allowed for relative paths, only for absolute ones. You might want to double-check that your C library honors that restriction.
As well as allowing longer paths, the "\\?\" prefix also lets you use files and directory names like "con" and "aux". Normally Windows would interpret those as old-fashioned DOS devices.
I've been writing Windows code since 1995, and although I'm aware of that prefix, I've never found any reason to use it. Increasing the path length beyond MAX_PATH seems to be the only reason for it, and neither I nor any of my programs' customers have ever done so, to my knowledge.