Writing to a file, giving an incorrect name - c++

I'm creating a file in the following manner:
if ((BmpFile = CreateFile((LPCWSTR)"Test.bmp",
GENERIC_WRITE,
0, NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
But the file that gets created has name 敔瑳戮灭.
Clearly not what I'm looking for! I'm in the process of trying to learn the windows API, could anyone tell me what I have to change to make it output what I think it should? I've looked at http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx and it hasn't helped me too much-- I think because I don't know what I'm looking for.
Premature edit: everything else works as expected in the function.

You don't create a WSTR by simply casting. If you want your fixed text to be a wide string, apply the L in front of the literal:
... = CreateFile( L"Test.bmp", ...

Related

Issue with CreateFileA method

I am having an issue with my application as while reading a file that consists of Unicode characters too. As I am using the CreateFileA method to get the data but it doesn't get the Unicode characters properly for which I am facing a lot of issues. Also, I don't know the difference between CreateFileA and CreateFileW.
I'm sorry I couldn't able to share my code. I will share my that portion of code with you.
HANDLE systemFileHandle = INVALID_HANDLE_VALUE;
systemFileHandle = CreateFileA(Filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
char* inBuffer=new char[totalFileSize+2];
memset(inBuffer, 0, totalFileSize+2);
ReadFile(systemFileHandle, inBuffer, totalFileSize, &bytesRead, nullptr);
And, I am getting the results on inBuffer array be like : Fernw�rmestationSW Au�en.
Can't I get it the original way they are?
So can you please help me out with this. It can be very helpful.
CreateFileA takes an ANSI-based file name, while CreateFileW takes a Unicode-based file name. There's nothing to say about the content of the file, both will return a HANDLE to the file where you can then read/write Unicode content as needed.

C++ Insert text at specific position in append with Windows API

I'm having troubles with insertion of text, in append, in a classical text file. What I want to do is simple : insert a single character in front of some lines. I know the exact offset of each line beginning. I have one restriction, I have to use the Windows API : CreateFile(), WriteFile(), SetFilePointer()...
I can't insert text, whatever I do, the program write to the end, or if it writes at the good offset, it erase the existing text.
Here is my code (I just simplified some checks to be more readable here) :
HANDLE handleFile = CreateFile (filename,
FILE_APPEND_DATA,
FILE_SHARE_READ, //SHARE
NULL, //SecurityAttibute
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (handleFile != INVALID_HANDLE_VALUE) {
if (SetFilePointer (handleFile, 12345, NULL, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
DWORD written = 0;
WriteFile (handleFile, "$", 1, &written, NULL);
}
}
When I use FILE_APPEND_DATA, the SetFilePointer() doesn't work and my character is written to the end.
When I use GENERIC_WRITE, or even FILE_GENERIC_WRITE, the character is written at the good offset, but it erase the present character :'(
What is the good parameter to really insert please ?
PS : this code is for very large files, so read / write the whole file is not possible, it would be too long.
Thanks a lot !
You cannot insert text into a file in the way you are attempting. You can append data to the end, or you can overwrite existing data. In order to effect an insertion you have to re-write all the contents that follow the point of insertion.

How can I capture File directory from edit control(textbox)

so I am new to the whole c++ windows API. I'm creating a simple dialogbox in which the user types in a directory into a textbox for a time file which has already been created. the program will then read the file and display the time in another edit control. Im having a few problems making the directory entered the parameter for CreateFile(). If I hard code the directory in, the program will work correctly. But I cant figure out how to take the textbox data and plug it into the CreateFile() function. if this doesn't make sense i can try an explain differently. Ive searched an can't seem to find anything.
Thanks
for example:
if the user types c:\test\time.txt into the text box I want "c:\test\time.txt" to be the put into CreateFile();
CHAR temp[20] = "";
HANDLE hFile;
GetDlgItemText(hDlg, IDC_TEXTIN, temp, 20);//IDC_TEXTIN is name of edit control
//open file
hFile = CreateFile(
temp,
GENERIC_READ | GENERIC_WRITE,
0, // no sharing
NULL, // no security
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL // no template
);

CreateFile() and CopyFile() share permissions

I wrote a simple XOR crypter and need help with fixing the crypted file's sharing permissions.
Basically, the output file from Builder.exe has a state of Shared. I have to change the file's share permissions to Nobody before I can run it.
Is there something wrong with the way I'm using Windows API?
This is the code for the handle that I wrote:
HANDLE efile = CreateFile(name, GENERIC_ALL,FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Full source code on GitHub: https://github.com/Jyang772/XOR_Crypter
Error message:

Executable reading itself

I need to read data added to the end of an executable from within that executable .
On win32 I have a problem that I cannot open the .exe for reading. I have tried CreateFile and std::ifstream.
Is there a way of specifying non-exclusive read access to a file that wasn't initially opened with sharing.
EDIT- Great thing about stackoverflow, you ask the wrong question and get the right answer.
Why not just use resources which are designed for this functionality. It won't be at the end, but it will be in the executable.
If you are adding to the .exe after it is built -- you don't have to add to the end, you can update resources on a built .exe
http://msdn.microsoft.com/en-us/library/ms648049(VS.85).aspx
We do this in one of our projects. What's the problem with it? If the EXE is running, then it's already held open for reading, and you can continue to open it read-only multiple times. I just checked our code, we just use:
HANDLE file=CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
This works without problem on all versions of 32- and 64-bit Windows to date.
I have no problem opening the executable image of a process using either of these statements:
FILE* f = fopen( fname, "rb");
hFile = CreateFile( fname, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
What's your code?