// OpenFile.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include #include using namespace std; void ErrorMessage (LPTSTR lpszFunction) { // Retrieve the system error message for the last-error code LPVOID lpMsgBuf; LPVOID lpDisplayBuf; DWORD dw = GetLastError(); FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); // Display the error message and exit the process lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf); MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); LocalFree(lpMsgBuf); LocalFree(lpDisplayBuf); // ExitProcess(dw); } int main(int argc, char * argv[]) { BOOL ok; char * env = "P1=H1\0P2=H2\0"; STARTUPINFO info; memset(&info, 0, sizeof (info)); info.cb = sizeof (info); PROCESS_INFORMATION answer; ok = CreateProcess ( "c:\\windows\\notepad.exe", //appl "c:\\work\\abc.txt", //command line NULL, NULL, FALSE, // bInheritHandles, 0, // dwCreationFlags NULL, // environment NULL, // current directory & info, & answer); if (! ok) { cout << "Chyba " << std::endl; ErrorMessage ("Opening Process"); } #if 0 ifstream in ("C:\\work\\abc.dat", ios::binary); ofstream out ("C:\\work\\def.dat", ios::binary); if (! in.good()) cout << "Nelze otevrit zdrojovy soubor " << std::endl; if (! out.good()) cout << "Nelze otevrit cilovy soubor " << std::endl; bool finished = false; while (in.good() && out.good() && ! finished) { const unsigned long size = 64*1024; char buf [size]; streamsize answer; in.read (buf,size); answer = in.gcount(); cout << "Zkopirovano " << answer << std::endl; if (answer != 0) { out.write (buf, answer); if (! out.good()) cout << "Chyba pri zapisu " << std::endl; } else finished = true; } in.close (); out.close (); cout << "konec " << std::endl; #endif #if 0 HANDLE src; src = CreateFile ( "C:\\work\\abc.dat", // pointer to name of the file GENERIC_READ, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes OPEN_EXISTING, // how to create FILE_ATTRIBUTE_NORMAL, // file attributes NULL // handle to file with attributes to copy ); HANDLE dst; dst = CreateFile ( "C:\\work\\def.dat", // pointer to name of the file GENERIC_WRITE, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes CREATE_ALWAYS, // how to create FILE_ATTRIBUTE_NORMAL, // file attributes NULL // handle to file with attributes to copy ); if (src != INVALID_HANDLE_VALUE && dst != INVALID_HANDLE_VALUE ) { std::cout << "Soubor otevren" << std::endl; const DWORD size = 64*1024; char buf [size]; DWORD answer; BOOL ok; do { ok = ReadFile (src, buf, size, &answer, NULL); if (ok) { DWORD result; ok = WriteFile (dst, buf, answer, &result, NULL); if (ok && answer != result) ok = FALSE; } } while (answer != 0 && ok); if (! ok) std::cout << "Chyba pri zapisu" << std::endl; else std::cout << "Zapsano" << std::endl; CloseHandle (src); CloseHandle (dst); } #endif char * str = new char [11]; std::cin.getline(str, 10); return 0; }