Instantly share code, notes, and snippets.
Created
October 28, 2009 02:10
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save meklarian/220175 to your computer and use it in GitHub Desktop.
Src for a pair of win32 console programs to demonstrate cross-process file access.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SharedFileClient.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <windows.h> | |
#include <time.h> | |
void EmitError(DWORD dwErr) | |
{ | |
if(dwErr != 0) | |
{ | |
LPVOID tszBuf = 0; | |
DWORD dwFmt = FormatMessage( | |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
0, dwErr, 0, (LPTSTR)(&tszBuf), 0, 0); | |
_tprintf(_T("Message:\r\n--\r\n%s\t\n"), (LPTSTR)tszBuf); | |
if(tszBuf){::LocalFree(&tszBuf);} | |
} | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
_tprintf(_T("** Begin Main Thread\r\n")); | |
_tprintf(_T("Creating File\r\n")); | |
HANDLE hFile = ::CreateFile(_T("C:\\data\\tmp\\sharetest.txt"), | |
(GENERIC_READ | GENERIC_WRITE), | |
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), | |
0, OPEN_EXISTING, | |
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, | |
0); | |
DWORD dwErr = GetLastError(); | |
if(dwErr != 0) | |
{ | |
EmitError(dwErr); | |
_tprintf(_T("** Exit Main Thread\r\n")); | |
Sleep(60000); | |
return 1; | |
} | |
time_t ttStart = time(0); | |
time_t ttCurrent = 0; | |
time_t ttLast = 0; | |
do | |
{ | |
ttCurrent = time(0); | |
if(ttCurrent != ttLast) | |
{ | |
::SetFilePointer(hFile, 0, 0, FILE_BEGIN); | |
DWORD dwData = 0; | |
DWORD dwIn = 0; | |
if(::ReadFile(hFile, &dwData, sizeof(DWORD), &dwIn, 0) == TRUE) | |
{ | |
_tprintf(_T("Read %d bytes. [0x%8.8x]\r\n"), dwIn, dwData); | |
} | |
else | |
{ | |
EmitError(GetLastError()); | |
} | |
ttLast = ttCurrent; | |
} | |
Sleep(500); | |
} | |
while(ttCurrent < (ttStart + 30)); | |
::CloseHandle(hFile); | |
_tprintf(_T("** End Main Thread\r\n")); | |
Sleep(60000); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SharedFileHost.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <windows.h> | |
#include <time.h> | |
void EmitError(DWORD dwErr) | |
{ | |
if(dwErr != 0) | |
{ | |
LPVOID tszBuf = 0; | |
DWORD dwFmt = FormatMessage( | |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
0, dwErr, 0, (LPTSTR)(&tszBuf), 0, 0); | |
_tprintf(_T("Message:\r\n--\r\n%s\t\n"), (LPTSTR)tszBuf); | |
if(tszBuf){::LocalFree(&tszBuf);} | |
} | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
_tprintf(_T("** Begin Main Thread\r\n")); | |
_tprintf(_T("Creating File\r\n")); | |
HANDLE hFile = ::CreateFile(_T("C:\\data\\tmp\\sharetest.txt"), | |
(GENERIC_READ | GENERIC_WRITE), | |
(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE), | |
0, CREATE_ALWAYS, | |
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, | |
0); | |
DWORD dwErr = GetLastError(); | |
if(dwErr != 0) | |
{ | |
EmitError(dwErr); | |
_tprintf(_T("** Exit Main Thread\r\n")); | |
Sleep(60000); | |
return 1; | |
} | |
time_t ttStart = time(0); | |
time_t ttCurrent = 0; | |
time_t ttLast = 0; | |
do | |
{ | |
ttCurrent = time(0); | |
if(ttCurrent != ttLast) | |
{ | |
::SetFilePointer(hFile, 0, 0, FILE_BEGIN); | |
DWORD dwData = (DWORD)(ttCurrent & 0xFFFFFFFF); | |
DWORD dwOut = 0; | |
if(::WriteFile(hFile, &dwData, sizeof(DWORD), &dwOut, 0) == TRUE) | |
{ | |
_tprintf(_T("Wrote %d bytes. [0x%8.8x]\r\n"), dwOut, dwData); | |
} | |
else | |
{ | |
EmitError(GetLastError()); | |
} | |
ttLast = ttCurrent; | |
} | |
Sleep(500); | |
} | |
while(ttCurrent < (ttStart + 60)); | |
::CloseHandle(hFile); | |
_tprintf(_T("** End Main Thread\r\n")); | |
Sleep(60000); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment