This is a piece of my code:
#include <stdio.h>
#include <windows.h>
#include <iostream.h>
#include <signal.h>
#include <tchar.h>
#include <stdarg.h>
int main(int argc, char * agrv[]) {
//Convert argument 2 to wide char pointer
wchar_t w[MAX_PATH];
size_t size_of_w = sizeof(w);
mbstowcs_s(&size_of_w, w, argv[1], MAX_PATH);
LPWSTR pFile = w;
//Copy original file to temp file
if (!CopyFile(w, L"temp.out", False))
{
printf("Error: could not copy file.");
printf("CopyFile Errorcode %d\n", GetLastError());
return 1;
}
pFile = L"temp.out";
HANDLE hFile = CreateFile(pFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Error: could not create handle to file");
printf("CreateFile Errorcode %d\n", GetLastError());
return 1;
}
printf("Successfully created Handle");
return 0;
}
Im trying to open a handle for the newly copied file "temp.out", but an error is thrown and it doesnt work.
My debug print (printf("CreateFile Errorcode %d\n", GetLastError());
)
prints "CreateFile Errorcode: 2" (File not found) but it is found, becasue CopyFile works just well.
EDIT: I've used absolute path, now, when Im trying to map the file to the memory using the handle, it throws '6', which means invalid handle:
HANDLE pMap = CreateFileMapping(hFile, NULL, PAGE_EXECUTE_READWRITE,0 ,0 ,NULL);
LPVOID lpBase MapViewOfFile(pMap, FILE_MAP_ACCESS| FILE_MAP_EXECUTE, 0, 0, 0);
printf("CreateFileMapping Errorcode %d\n", GetLastError());
if (!lpBase)
{
printf("Error: could not map file to memory");
printf("CreateFileMapping Errorcode %d\n", GetLastError());
return 1;
}
printf("Successfully mapped file");
EDIT 2:
Ive added error handling to CopyFile, but it works OK and DOES copy the file. the output is now:
program.exe shit.txt Successfully created Handle Error: could not map file to memory createMapFile errorcode: 6