2.9.2014
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <stdio.h>
#include <windows.h>
void AppendExample (void);
int main(int argc, char **argv)
{
printf(“hello world\n”);
/* from http://support.microsoft.com/kb/100027
To open a physical hard drive for direct disk access (raw I/O) in a Win32-based application, use a device name of the form
\\.\PhysicalDriveN
where N is 0, 1, 2, and so forth, representing each of the physical drives in the system.
To open a logical drive, direct access is of the form
\\.\X:
where X: is a hard-drive partition letter, floppy disk drive, or CD-ROM drive.
Collapse imageMORE INFORMATION
You can open a physical or logical drive using the CreateFile() application programming interface (API) with these device names provided that you have the appropriate access rights to the drive (that is, you must be an administrator). You must use both the CreateFile() FILE_SHARE_READ and FILE_SHARE_WRITE flags to gain access to the drive.
Once the logical or physical drive has been opened, you can then perform direct I/O to the data on the entire drive. When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries. Call DeviceIoControl() using IOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number of sectors, sectors per track, and so forth, so that you can compute the size of the buffer that you will need.
Note that a Win32-based application cannot open a file by using internal Windows NT object names; for example, attempting to open a CD-ROM drive by opening
\Device\CdRom0
does not work because this is not a valid Win32 device name. An application can use the QueryDosDevice() API to get a list of all valid Win32 device names and see the mapping between a particular Win32 device name and an internal Windows NT object name. An application running at a sufficient privilege level can define, redefine, or delete Win32 device mappings by calling the DefineDosDevice() API.
*/
/* from http://compnetworking.about.com/od/windowsnetworking/g/unc-name.htm
* Definition: UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows. Support for UNC also appears in other operating systems via technologies like Samba. UNC names are most commonly used to reach file servers or printers on a LAN.
UNC Name Syntax
UNC names identify network resources using a specific notation. UNC names consist of three parts – a server name, a share name, and an optional file path. These three elements are combined using backslashes as follows:
\\server\share\file_path The server portion of a UNC name references the strings maintained by a network naming service such as DNS or WINS. Server names are set by a system administrator.
The share portion of a UNC name references a label created by an administrator or, in some cases, within the operating system. In most versions of Microsoft Windows, for example, the built-in share name admin$ refers to the root directory of the operating system installation (usually C:\WINNT or C:\WINDOWS).
The file path portion of a UNC name references the local subdirectories beneath the share point.
UNC Name Examples
Consider a standard Windows XP computer named teela. In addition to the built-in admin$ share, say you have also defined a share point called temp that is located at C:\temp. Using UNC names, you would connect to folders on teela as follows: • \\teela\admin$ (to reach C:\WINNT)
• \\teela\admin$\system32 (to reach C:\WINNT\system32)
• \\teela\temp (to reach C:\temp)
Using Windows Explorer or the DOS command prompt, and with proper security credentials, you can map network drives and remotely access folders on a computer by specifying the UNC names.
*/
/* CODE from http://stackoverflow.com/questions/11517566/createfile-function-in-visual-c */
/*
DWORD lastError = ERROR_SUCCESS;
BOOL bTest=FALSE;
DWORD dwNumRead=0;
HANDLE hFile=CreateFile(L”D:\\a.dat”,GENERIC_READ,FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
lastError = GetLastError();
bTest= CloseHandle(hFile);
*/
AppendExample();
return 0;
}
/* following code inspire and found by Google by Gia Ba?o playing with Thanh So+n */
/* following AppendExample code is from http://msdn.microsoft.com/en-us/library/ms900134.aspx */
void AppendExample (void)
{
HANDLE hFile, hAppend;
DWORD dwBytesRead, dwBytesWritten, dwPos;
char buff[4096];
TCHAR szMsg[1000];
// #define LPSTR char*
// #define LPCSTR const char*
// #define LPWSTR wchar_t*
// #define LPWCSTR const wchar_t*
// #define LPTSTR TCHAR*
// #define LPCTSTR const TCHAR*
// from http://www.cplusplus.com/articles/2w6AC542/, Note:
// #define LPSTR char*
// #define LPCSTR const char*
// #define LPWSTR wchar_t*
// #define LPWCSTR const wchar_t* // he means “typedef const wchar_t* LPCWSTR;” from http://msdn.microsoft.com/en-us/library/cc230352.aspx
// #define LPTSTR TCHAR*
// #define LPCTSTR const TCHAR*
// Remember that TCHAR is char or wchar_t depending on Unicode.
// WinAPI provides some other macros, _T(), T(), and TEXT(),
// all of which do the same thing. In a Unicode build, they put
// the L before the string literal to make it wide, and in
// non-Unicode, they do nothing. Therefore they will always work hand in hand with TCHARs:
// const TCHAR* d = _T(“foo”); // works in both Unicode and ANSI builds
// const TCHAR* vhdfile = TEXT(“C:\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”);
// const TCHAR* bootloaderfile = TEXT(“C:\Users\LaptopUser\My Programs\My OS\try2.bin”);
// const TCHAR* vhdfile = _TEXT(“C:\\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”);
// const TCHAR* bootloaderfile = _TEXT(“C:\\Users\LaptopUser\My Programs\My OS\try2.bin”);
// following correction to string representing vhdfile and bootloaderfile is courtesy of
// Gia Ba?o’s mother and aunts “tu+” or “di` tu+”, “di` ba” … and of To^n DDi.nh’s friend “DDa.t” who
// would resemble “Pak-Ming Ho” … MingW compiler … and who played the Eagles “Hotel California” no-escape song … Di.nh said “you made me poor all the time …” “because of you I’m poor ….” … as well as Adele and “on the road again” ca` cho+’n songs
/*from http://stackoverflow.com/questions/20630072/parse-string-to-lpcwstr
*The point is that by boost filesystem I get a string such as
string filename=”C:\Users\MyUser\Desktop\PDN.pdf”;
and I need to convert this string to a LPCWSTR.
Because of this I have done several tries which have all failed, for example:
HANDLE hFile = CreateFile((LPCWSTR)fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
But when doing this, it succeded:
HANDLE hFile = CreateFile(L”C:\\Users\\MyUSer\\Desktop\\PDN.pdf”, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
So my question is, how could I parse a string to a PWSTR using a string variable? And if possible (I guess no), is there any function that will change the original path adding a slash where finds another slash?
Thanks a lot
*/
/* also from http://gcc.gnu.org/ml/gcc-help/2002-02/msg00043.html
Kabir Patel wrote:
> warning: unknown escape sequence ‘\)’
Backslash is used as an escaping character for C strings; if you’d like
to include a character that you wouldn’t normally be able to represent
in a text file then you can use an escape code for it, for example:
\t tab (ASCII 9)
\n newline (ASCII 10)
\r carriage return (ASCII 13)
\001 character with octal value 001
\xAB character with hexadecimal value AB
but since backslash has been overloaded, an escape is necessary to
actually embed a backslash:
\\ backslash
*/
// from http://www.tenouk.com/cpluscodesnippet/createfilegetclosehandleinfo.html:
// LPCWSTR fname = L”c:\\testfile.txt”;
// LPCWSTR vhdfile = L”C:\\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”; // evidently this name is too long …
// LPCWSTR bootloaderfile = L”C:\\Users\LaptopUser\My Programs\My OS\try2.bin”; // evidently this name is too long …
// LPCWSTR vhdfile = “\\?\C:\\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”; // evidently this name is too long …
// LPCWSTR bootloaderfile = “\\?\C:\\Users\LaptopUser\My Programs\My OS\try2.bin”; // evidently this name is too long …
LPCWSTR vhdfile = L”\\\\?\\C:\\Users\\LaptopUser\\Documents\\VHDs\\My OS raw.vhd”; // evidently this name is too long …
LPCWSTR bootloaderfile = L”\\\\?\\C:\\Users\\LaptopUser\\My Programs\\My OS\\try2.bin”; // evidently this name is too long …
// from winbase.h
// WINBASEAPI HANDLE WINAPI CreateFileA(LPCSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE);
// WINBASEAPI HANDLE WINAPI CreateFileW(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE);
// Open the existing file.
// the vhd file is “C:\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”
// the bootloader file is “C:\Users\LaptopUser\My Programs\My OS\try2.bin”
/*
hFile = CreateFile (TEXT(“\\ONE.TXT”), // Open One.txt
GENERIC_READ, // Open for reading
0, // Do not share
NULL, // No security
OPEN_EXISTING, // Existing file only
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template file
*/
// hFile = CreateFileW (“\\?\C:\Users\LaptopUser\My Programs\My OS\try2.bin”, // Open One.txt
// hFile = CreateFileW (L”C:\\Users\LaptopUser\My Programs\My OS\try2.bin”, // Open One.txt
// hFile = CreateFileW (L”C:\\Users\LaptopUser\My Programs\My OS\try2.bin”, // Open One.txt
// hFile = CreateFileW (bootloaderfile, // Open One.txt
hFile = CreateFile (bootloaderfile, // Open One.txt
GENERIC_READ, // Open for reading
0, // Do not share
NULL, // No security
OPEN_EXISTING, // Existing file only
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template file
if (hFile == INVALID_HANDLE_VALUE)
{
DWORD dwErr = GetLastError();
// Your error-handling code goes here.
// wsprintf (szMsg, TEXT(“Could not open ONE.TXT”));
wsprintf (szMsg, TEXT(“Could not open bootloader file”));
return;
}
// the vhd file is “C:\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”
// Open the existing file, or, if the file does not exist,
// create a new file.
/*
hAppend = CreateFile (TEXT(“\\TWO.TXT”), // Open Two.txt.
GENERIC_WRITE, // Open for writing
0, // Do not share
NULL, // No security
OPEN_ALWAYS, // Open or create
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template file
*/
// hAppend = CreateFileW (“\\?\C:\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”, // Open Two.txt.
// hAppend = CreateFileA (“C:\\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”, // Open Two.txt.
// hAppend = CreateFileW (L”C:\\Users\LaptopUser\Documents\VHDs\My OS raw.vhd”, // Open Two.txt.
// hAppend = CreateFileW (vhdfile, // Open Two.txt.
hAppend = CreateFile (vhdfile, // Open Two.txt.
GENERIC_WRITE, // Open for writing
0, // Do not share
NULL, // No security
OPEN_ALWAYS, // Open or create
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template file
if (hAppend == INVALID_HANDLE_VALUE)
{
DWORD dwErr = GetLastError();
//wsprintf (szMsg, TEXT(“Could not open TWO.TXT”));
wsprintf (szMsg, TEXT(“Could not open raw vhd drive”));
CloseHandle (hFile); // Close the first file.
return;
}
// Append the first file to the end of the second file.
// dwPos = SetFilePointer (hAppend, 0, NULL, FILE_END);
dwPos = SetFilePointer (hAppend, 0, NULL, FILE_BEGIN);
do
{
if (ReadFile (hFile, buff, 4096, &dwBytesRead, NULL))
{
WriteFile (hAppend, buff, dwBytesRead,
&dwBytesWritten, NULL);
}
}
while (dwBytesRead == 4096);
// Close both files.
CloseHandle (hFile);
CloseHandle (hAppend);
return;
} // End of AppendExample code