Builder.cz - Informacni server o programovani

Odběr fotomagazínu

Fotografický magazín "iZIN IDIF" každý týden ve Vašem e-mailu.
Co nového ve světě fotografie!

 

Zadejte Vaši e-mailovou adresu:

Kamarád fotí rád?

Přihlas ho k odběru fotomagazínu!

 

Zadejte e-mailovou adresu kamaráda:



vlakna (win api)

Seznam témat     Nová odpověď

Přihlásit se     Registrace     Zapomenuté heslo

Re: vlakna (win api)

Autor: Sarge

23:33:28 27.10.2011

Skusil som to opravit, ale aj tak sa to nechova ako ma..ani do suboru to stale nezapisuje..


threads.c

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lastError = ERROR_SUCCESS;
DWORD result = ERROR_SUCCESS;
DWORD resultWait = WAIT_TIMEOUT;
BOOL bool1 = FALSE;
BOOL bool2 = FALSE;


threadNumbers = 0;
usageCount = 0;

ZeroMemory(processName, 20*sizeof(wchar_t));

logFileHandle = CreateFile(
LOG_FILE_NAME,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if((logFileHandle == INVALID_HANDLE_VALUE) || (logFileHandle == NULL))
goto error;

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_WARN, logFileHandle);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ERROR, logFileHandle);

_RPTFW0(_CRT_WARN, L"Starting debug logs.\n");

// Initialize global variables
result = Initialize();
if (result != ERROR_SUCCESS)
goto error;

// Spawn a new thread
hFirstThread = CreateThread(
NULL,
0,
&FirstThreadFunc,
NULL,
0, //vlákno je spustené hneď po vytvorení
NULL
);
if (hFirstThread == NULL)
goto error;

hSecondThread = CreateThread(
NULL,
0,
&SecondThreadFunc,
NULL,
0, //vlákno je spustené hneď po vytvorení
NULL
);
if (hSecondThread == NULL)
goto error;

while(resultWait == WAIT_TIMEOUT) {

resultWait = WaitForMultipleObjectsEx(
4,
objects,
FALSE,
5000,
TRUE
);

switch(resultWait){
case WAIT_TIMEOUT:
continue;
case WAIT_FAILED:
wprintf_s(L"fail 0\n");
break;
case WAIT_OBJECT_0 + 0:
wprintf_s(L"Thread processed poem in \\adresar\\out.txt\n\n");
FindNextChangeNotification(objects[0]);
bool1 = TRUE;
break;
case WAIT_OBJECT_0 + 1:
wprintf_s(L"Process name: %s\n",processName);
wprintf_s(L"Number of threads: %d\n",threadNumbers);
wprintf_s(L"Usage count: %d\n\n",usageCount);
ResetEvent(objects[1]);
bool2 = TRUE;
break;
case WAIT_OBJECT_0 + 2:
wprintf_s(L"First thread: An error occured\n");
break;
case WAIT_OBJECT_0 + 3:
wprintf_s(L"Second thread: An error occured\n");
break;
}

//if (WaitForSingleObject(hFirstThread,0) == WAIT_OBJECT_0 && WaitForSingleObject(hSecondThread,0) == WAIT_OBJECT_0)
if (bool1 && bool2)
break;
else
resultWait = WAIT_TIMEOUT;

}

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_WARN, L"An error occured, error code : %d.\n", lastError);
return lastError;
final:
Finalize(lastError);
return lastError;
}

DWORD WINAPI FirstThreadFunc(PVOID unusedParam) {

LPWSTR poem = NULL;
LPWSTR copyPoem = NULL;
LPWSTR poemPosition = NULL;
DWORD lastError = ERROR_SUCCESS;
wchar_t* dotPosition = NULL;
LPWSTR bufferString = NULL;
DWORD resultEvent = ERROR_SUCCESS;

bufferString = (LPWSTR) malloc(100*sizeof(wchar_t));
ZeroMemory(bufferString, 100*sizeof(wchar_t));

poem = GetPoem();

if(!poem)
goto error;

copyPoem = (LPWSTR) malloc(wcslen(poem)*sizeof(wchar_t));
ZeroMemory(copyPoem, wcslen(poem)*sizeof(wchar_t));

dotPosition = wcschr( poem, '.' );
poemPosition = poem+1;

while(dotPosition) {


if(*dotPosition == '.') //ak sa v texte nachadza pozicia kde je bodka zacne sa spracovavat text
{

wcsncpy(bufferString,poemPosition,dotPosition - poemPosition); //skopirujem vers
wcscat(copyPoem,bufferString); //pripojim vers do retazca, ktory neskor zapisem do suboru
wcscat(copyPoem,L"\r\n"); //novy riadok za versom v subore

if (*(dotPosition + 1) == '|') //zistujem ci za versom nieje koniec slohy
{
wcscat(copyPoem,L"\r\n"); //oddelim verse v subore
poemPosition = dotPosition+2;
}

else
poemPosition = dotPosition+1;

dotPosition = wcschr( poemPosition, '.' ); //hladam dalsi vers v subore
ZeroMemory(bufferString, 100*sizeof(wchar_t));

}

}

SavePoem(copyPoem);

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_ERROR, L"First thread: An error occured: %d.\n", lastError);
resultEvent = SetEvent(errorOneEventObject);
if (!resultEvent)
_RPTFW1(_CRT_ERROR, L"First thread: An error occured in set event errorOneEventObject: %d.\n", lastError);

final:
_RPTFW0(_CRT_WARN, L"First thread: end.\n");
free(poem);
free(bufferString);
free(dotPosition);
if(lastError == ERROR_SUCCESS)
if ( FindNextChangeNotification(hNotificationObject) == FALSE)
_RPTFW1(_CRT_ERROR, L"FindNextChangeNotification function failed: %d.\n", lastError);
return lastError;

}

DWORD WINAPI SecondThreadFunc(PVOID unusedParam){

DWORD resultEvent = ERROR_SUCCESS;
DWORD lastError = ERROR_SUCCESS;
BOOL result = TRUE;

result = CurrentProcessInfo();
if (result != ERROR_SUCCESS)
goto error;

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_ERROR, L"Second thread: An error occured: %d.\n", lastError);
resultEvent = SetEvent(errorTwoEventObject);
if (!resultEvent)
_RPTFW1(_CRT_ERROR, L"Second thread: An error occured in set event errorTwoEventObject: %d.\n", lastError);
return lastError;

final:
_RPTFW0(_CRT_WARN, L"Second thread: end.\n");
resultEvent = SetEvent(doneEventObject);
if (!resultEvent)
_RPTFW1(_CRT_ERROR, L"Second thread: An error occured in set event doneEventObject: %d.\n", lastError);
return lastError;

}

DWORD Initialize()
{
DWORD lastError = ERROR_SUCCESS;
// set global variables to NULL
hNotificationObject = NULL;
hFirstThread = NULL;
fileHandle = NULL;

doneEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
DONE_EVENT
);
if (doneEventObject == NULL)
goto error;
_RPTFW1(_CRT_WARN, L"Event handle : %p\n", doneEventObject);

errorOneEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
ERROR_EVENT_1
);
if (errorOneEventObject == NULL)
goto error;
_RPTFW1(_CRT_WARN, L"Event handle : %p\n", errorOneEventObject);

errorTwoEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
ERROR_EVENT_2
);
if (errorTwoEventObject == NULL)
goto error;
_RPTFW1(_CRT_WARN, L"Event handle : %p\n", errorTwoEventObject);

hNotificationObject = FindFirstChangeNotification(
L"\\adresar",
FALSE,
FILE_NOTIFY_CHANGE_LAST_WRITE
);

if (hNotificationObject == INVALID_HANDLE_VALUE)
goto error;

fileHandle = CreateFile(
L"\\adresar\\out.txt",
GENERIC_ALL,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if((fileHandle == INVALID_HANDLE_VALUE) || (fileHandle == NULL))
goto error;

objects[0] = hNotificationObject;
objects[1] = doneEventObject;
objects[2] = errorOneEventObject;
objects[3] = errorTwoEventObject;

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_ERROR, L"An error occured while creating event: %d\n", lastError);
return lastError;
final:
return lastError;
}

VOID Finalize(DWORD lastError)
{


if (doneEventObject != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free event handle.\n");
CloseHandle(doneEventObject);
}

if (fileHandle != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free event handle.\n");
CloseHandle(fileHandle);
}

if (errorOneEventObject != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free event handle.\n");
CloseHandle(errorOneEventObject);
}

if (errorTwoEventObject != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free event handle.\n");
CloseHandle(errorTwoEventObject);
}

if (FindCloseChangeNotification(hNotificationObject))
_RPTFW0(_CRT_WARN, L"Attempt to free event handle.\n");
else
_RPTFW0(_CRT_WARN, L"CloseChangeNotification: An error occured.\n");

if (hFirstThread != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free thread handle.\n");
CloseHandle(hFirstThread);
}

if (hSecondThread != NULL)
{
_RPTFW0(_CRT_WARN, L"Attempt to free thread handle.\n");
CloseHandle(hSecondThread);
}

if((logFileHandle != INVALID_HANDLE_VALUE) && (logFileHandle != NULL))
{
_RPTFW1(_CRT_WARN, L"Leaving _tmain, lastError: %d\n", lastError);
CloseHandle(logFileHandle);
}

return;
}

DWORD CurrentProcessInfo()
{
BOOL bResult = FALSE;
DWORD lastError = ERROR_SUCCESS;
DWORD pid = 0; // current process id

HANDLE snapshotHandle = NULL;
PROCESSENTRY32 processEntry;

pid = GetCurrentProcessId();
snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ((snapshotHandle == INVALID_HANDLE_VALUE) || (snapshotHandle == NULL))
goto error;

processEntry.dwSize = sizeof(PROCESSENTRY32);

bResult = Process32First(snapshotHandle, &processEntry);
if (bResult == FALSE)
{
wprintf_s(L"No processes found.\n");
_RPTFW0(_CRT_WARN, L"No processes found.\n");
goto final;
}
while (Process32Next(snapshotHandle, &processEntry))
{
if (processEntry.th32ProcessID == pid)
break;
}


wcscpy(processName,processEntry.szExeFile);
threadNumbers = processEntry.cntThreads;
usageCount = processEntry.cntUsage;

_RPTFW1(_CRT_WARN, L"Process name: %s\n", processEntry.szExeFile);
_RPTFW1(_CRT_WARN, L"Number of threads: %d\n", processEntry.cntThreads);
_RPTFW1(_CRT_WARN, L"Usage count: %d\n", processEntry.cntUsage);

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_ERROR,L"An error occured: %d",lastError);
final:
if ((snapshotHandle != NULL) && (snapshotHandle != INVALID_HANDLE_VALUE))
CloseHandle(snapshotHandle);
return lastError;
}

LPWSTR GetPoem()
{
DWORD lastError = ERROR_SUCCESS;
HANDLE fileHandle = NULL;
BOOL result = FALSE;
DWORD bytesRead = 0;
LPWSTR bufferForFile = NULL;
DWORD fileSize = 0;

// otvori pozadovany subor
fileHandle = CreateFile(
L"poem.txt",
GENERIC_ALL,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if((fileHandle == INVALID_HANDLE_VALUE) || (fileHandle == NULL))
goto error;

// naalokuje pamat podla velkosti suboru
fileSize = GetFileSize(fileHandle, NULL);

bufferForFile = (LPWSTR) malloc(fileSize + 2);

ZeroMemory(bufferForFile, fileSize + 2);

// nacita obsah suboru
result = ReadFile(
fileHandle,
bufferForFile,
fileSize,
&bytesRead,
NULL
);

if(result = FALSE)
goto error;

goto final;

error:

lastError = GetLastError();
_RPTFW1(_CRT_WARN, L"An error occured, error code : %d.\n", lastError);
return NULL;

final:

CloseHandle(fileHandle);
return bufferForFile;

}

void SavePoem(LPWSTR poem){
HANDLE fileHandleWrite = NULL;
DWORD poemSize = 0;
DWORD bytesRead = 0;
DWORD lastError = ERROR_SUCCESS;
BOOL result = FALSE;

fileHandleWrite = CreateFile(
"\\adresar\\out.txt",
GENERIC_ALL,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if((fileHandleWrite == INVALID_HANDLE_VALUE) || (fileHandleWrite == NULL))
goto error;

poemSize = wcslen(poem)*sizeof(wchar_t);

result = WriteFile(
fileHandleWrite,
poem,
poemSize,
&bytesRead,
NULL
);

if(!result)
goto error;

goto final;

error:
lastError = GetLastError();
_RPTFW1(_CRT_WARN, _TEXT("An error occured, error code : %d.\n"), lastError);

final:
CloseHandle(fileHandleWrite);
}

threads.h

#ifndef _threads_H_
#define _threads_H_

#define LOG_FILE_NAME L"debug.log"
#define FINISHED_EVENT_NAME L"FinishedEvent"
#define DONE_EVENT L"DoneEvent"
#define ERROR_EVENT_1 L"ErrorEvent1"
#define ERROR_EVENT_2 L"ErrorEvent2"

DWORD WINAPI SecondThreadFunc(PVOID);
DWORD WINAPI FirstThreadFunc(PVOID);
DWORD Initialize();
VOID Finalize(DWORD);
DWORD CurrentProcessInfo();
LPWSTR GetPoem();
void SavePoem(LPWSTR);

HANDLE objects[4];
HANDLE logFileHandle;
HANDLE hNotificationObject;
HANDLE hFirstThread;
HANDLE hSecondThread;
HANDLE doneEventObject;
HANDLE errorOneEventObject;
HANDLE errorTwoEventObject;
HANDLE fileHandle;

wchar_t processName[20];
DWORD threadNumbers;
DWORD usageCount;

#endif

https://docs.google.com/open?id=0B5J3O_QNqtpFOTk4ZDNkN2EtNTg2My00ZTQ0LTk4ODEtZjdiZjc1MGNlZWY3

Citovat příspěvek

 

vlakna (win api)

Autor: Sarge

22:07:23 27.10.2011

Ahojte, robim na ulohe kde v hlavnom procese vytvorim 2 vlakna. V prvom vlakne mam spracovat text a zapisat ho do noveho suboru a 2 vlakno ma za ulohu zistit info o procese. Ked vlakna skoncia tak mam na konzolu vypisat info, podla toho ktory event je signalizovany. Hlavny proces skonci, ak skoncia obe vlakna.

Program sa prerusi bud hlaskou Program threads.exe prestal pracovat., alebo normalne skonci ale spracovany text nezapise do suboru (ked som to skusal ako samostatny proces tak to funguje).

Vedeli by ste niekto poradit? Okrem kodu som dal na koniec aj odkazy na .c, .h subor ak by to bolo pre niekoho pohodlnejsie.



#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
DWORD lastError = ERROR_SUCCESS;
DWORD result = ERROR_SUCCESS;
DWORD resultWait = WAIT_TIMEOUT;


threadNumbers = 0;
usageCount = 0;

ZeroMemory(processName, 20*sizeof(wchar_t));

result = Initialize();
if (result != ERROR_SUCCESS)
goto error;

// new thread
hFirstThread = CreateThread(
NULL,
0,
&FirstThreadFunc,
NULL,
0, //vlákno je spustené hneď po vytvorení
NULL
);
if (hFirstThread == NULL)
goto error;

hSecondThread = CreateThread(
NULL,
0,
&SecondThreadFunc,
NULL,
0, //vlákno je spustené hneď po vytvorení
NULL
);
if (hSecondThread == NULL)
goto error;

while(resultWait == WAIT_TIMEOUT) {

resultWait = WaitForMultipleObjectsEx(
4,
objects,
FALSE,
5000,
TRUE
);

switch(resultWait){
case WAIT_TIMEOUT:
continue;
case WAIT_FAILED:
wprintf_s(L"fail 0\n");
break;
case WAIT_OBJECT_0 + 0:
wprintf_s(L"Thread processed poem in \\adresar\\text.txt\n");
FindNextChangeNotification(changeNotificationObject);
break;
case WAIT_OBJECT_0 + 1:
wprintf_s(L"Process name: %s\n",processName);
wprintf_s(L"Number of threads: %d\n",threadNumbers);
wprintf_s(L"Usage count: %d\n",usageCount);
ResetEvent(doneEventObject);
break;
case WAIT_OBJECT_0 + 2:
wprintf_s(L"First thread error \n");
break;
case WAIT_OBJECT_0 + 3:
wprintf_s(L"Second thread error \n");
break;
}

if (WaitForSingleObject(hFirstThread,0) == WAIT_OBJECT_0 && WaitForSingleObject(hSecondThread,0) == WAIT_OBJECT_0)
break;
else
resultWait = WAIT_TIMEOUT;

}

goto final;

error:
lastError = GetLastError();
return lastError;
final:
Finalize(lastError);
return lastError;
}

DWORD WINAPI FirstThreadFunc(PVOID unusedParam) {

LPWSTR poem = NULL;
LPWSTR copyPoem = NULL;
LPWSTR poemPosition = NULL;
DWORD lastError = ERROR_SUCCESS;
wchar_t* dotPosition = NULL;
LPWSTR bufferString = NULL;
DWORD resultEvent = ERROR_SUCCESS;

bufferString = (LPWSTR) malloc(100*sizeof(wchar_t));
ZeroMemory(bufferString, 100*sizeof(wchar_t));

poem = GetPoem();

if(!poem)
goto error;

copyPoem = (LPWSTR) malloc(wcslen(poem)*sizeof(wchar_t));
ZeroMemory(copyPoem, wcslen(poem)*sizeof(wchar_t));

dotPosition = wcschr( poem, '.' );
poemPosition = poem+1;

while(dotPosition) {


if(*dotPosition == '.') //ak sa v texte nachadza pozicia kde je bodka zacne sa spracovavat text
{

wcsncpy(bufferString,poemPosition,dotPosition - poemPosition); //skopirujem vers
wcscat(copyPoem,bufferString); //pripojim vers do retazca, ktory neskor zapisem do suboru
wcscat(copyPoem,L"\r\n"); //novy riadok za versom v subore

if (*(dotPosition + 1) == '|') //zistujem ci za versom nieje koniec slohy
{
wcscat(copyPoem,L"\r\n"); //oddelim verse v subore
poemPosition = dotPosition+2;
}

else
poemPosition = dotPosition+1;

dotPosition = wcschr( poemPosition, '.' ); //hladam dalsi vers v subore
ZeroMemory(bufferString, 100*sizeof(wchar_t));

}

}

SavePoem(copyPoem);

goto final;

error:
lastError = GetLastError();
resultEvent = SetEvent(errorOneEventObject);
return lastError;

final:
free(poem);
free(bufferString);
free(dotPosition);
if ( FindNextChangeNotification(changeNotificationObject) != FALSE)
resultEvent = SetEvent(hNotificationObject);
return lastError;

}

DWORD WINAPI SecondThreadFunc(PVOID unusedParam){

DWORD resultEvent = ERROR_SUCCESS;
DWORD lastError = ERROR_SUCCESS;
BOOL result = TRUE;

result = CurrentProcessInfo();
if (result != ERROR_SUCCESS)
goto error;

goto final;

error:
lastError = GetLastError();
resultEvent = SetEvent(errorTwoEventObject);

return lastError;

final:
_RPTFW0(_CRT_WARN, L"Second thread: end.\n");
resultEvent = SetEvent(doneEventObject);
return lastError;

}

DWORD Initialize()
{
DWORD lastError = ERROR_SUCCESS;
// set global variables to NULL
hNotificationObject = NULL;
hFirstThread = NULL;
changeNotificationObject = NULL;
fileHandle = NULL;

hNotificationObject = CreateEvent(
NULL,
TRUE, //function creates a manual-reset event object
FALSE, //initial state of the event object is nonsignaled
FINISHED_EVENT_NAME //name of the event object
);
if (hNotificationObject == NULL)
goto error;


doneEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
DONE_EVENT
);
if (doneEventObject == NULL)
goto error;


errorOneEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
ERROR_EVENT_1
);
if (errorOneEventObject == NULL)
goto error;


errorTwoEventObject = CreateEvent(
NULL,
TRUE,
FALSE,
ERROR_EVENT_2
);
if (errorTwoEventObject == NULL)
goto error;


changeNotificationObject = FindFirstChangeNotification(
L"\\adresar",
FALSE,
FILE_NOTIFY_CHANGE_LAST_WRITE
);

if (changeNotificationObject == INVALID_HANDLE_VALUE)
goto error;

fileHandle = CreateFile(
L"\\adresar\\text.txt",
GENERIC_ALL,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);

if((fileHandle == INVALID_HANDLE_VALUE) || (fileHandle == NULL))
goto error;

objects[0] = hNotificationObject;
objects[1] = doneEventObject;
objects[2] = errorOneEventObject;
objects[3] = errorTwoEventObject;

goto final;

error:
lastError = GetLastError();
final:
return lastError;
}

VOID Finalize(DWORD lastError)
{
if (hNotificationObject != NULL)
CloseHandle(hNotificationObject);


if (doneEventObject != NULL)
CloseHandle(doneEventObject);


if (fileHandle != NULL)
CloseHandle(fileHandle);


if (errorOneEventObject != NULL)
CloseHandle(errorOneEventObject);


if (errorTwoEventObject != NULL)
CloseHandle(errorTwoEventObject);


if (changeNotificationObject != NULL)
CloseHandle(changeNotificationObject);


if (hFirstThread != NULL)
CloseHandle(hFirstThread);


if (hSecondThread != NULL)
CloseHandle(hSecondThread);


if((logFileHandle != INVALID_HANDLE_VALUE) && (logFileHandle != NULL))
CloseHandle(logFileHandle);

return;
}

DWORD CurrentProcessInfo()
{
BOOL bResult = FALSE;
DWORD lastError = ERROR_SUCCESS;
DWORD pid = 0; // current process id

HANDLE snapshotHandle = NULL;
PROCESSENTRY32 processEntry;

pid = GetCurrentProcessId();
snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ((snapshotHandle == INVALID_HANDLE_VALUE) || (snapshotHandle == NULL))
goto error;

processEntry.dwSize = sizeof(PROCESSENTRY32);

bResult = Process32First(snapshotHandle, &processEntry);
if (bResult == FALSE)
{
wprintf_s(L"No processes found.\n");
goto final;
}
while (Process32Next(snapshotHandle, &processEntry))
{
if (processEntry.th32ProcessID == pid)
break;
}

wcscpy(processName,processEntry.szExeFile);
threadNumbers = processEntry.cntThreads;
usageCount = processEntry.cntUsage;

goto final;

error:
lastError = GetLastError();
final:
if ((snapshotHandle != NULL) && (snapshotHandle != INVALID_HANDLE_VALUE))
CloseHandle(snapshotHandle);
return lastError;
}

LPWSTR GetPoem()
{
DWORD lastError = ERROR_SUCCESS;
HANDLE fileHandle = NULL;
BOOL result = FALSE;
DWORD bytesRead = 0;
LPWSTR bufferForFile = NULL;
DWORD fileSize = 0;

// otvori pozadovany subor
fileHandle = CreateFile(
L"poem.txt",
GENERIC_ALL,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if((fileHandle == INVALID_HANDLE_VALUE) || (fileHandle == NULL))
goto error;

// naalokuje pamat podla velkosti suboru
fileSize = GetFileSize(fileHandle, NULL);

bufferForFile = (LPWSTR) malloc(fileSize + 2);

ZeroMemory(bufferForFile, fileSize + 2);

// nacita obsah suboru
result = ReadFile(
fileHandle,
bufferForFile,
fileSize,
&bytesRead,
NULL
);

if(result = FALSE)
goto error;

goto final;

error:

lastError = GetLastError();
return NULL;

final:

CloseHandle(fileHandle);
return bufferForFile;

}

void SavePoem(LPWSTR poem){
HANDLE fileHandleWrite = NULL;
DWORD poemSize = 0;
DWORD bytesRead = 0;
DWORD lastError = ERROR_SUCCESS;
BOOL result = FALSE;

fileHandleWrite = CreateFile(
L"\\adresar\\text.txt",
GENERIC_ALL,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if((fileHandleWrite == INVALID_HANDLE_VALUE) || (fileHandleWrite == NULL))
goto error;

poemSize = wcslen(poem)*sizeof(wchar_t);

result = WriteFile(
fileHandleWrite,
poem,
poemSize,
&bytesRead,
NULL
);

if(!result)
goto error;

goto final;

error:
lastError = GetLastError();

final:
CloseHandle(fileHandleWrite);
}

https://docs.google.com/open?id=0B5J3O_QNqtpFMzI1MzBhMGEtNDViOS00MTNmLTlmMzMtZTgxZmM1YjhkNThh

Citovat příspěvek

 

 

 

Přihlášení k mému účtu

Uživatelské jméno:

Heslo: