By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,514 Members | 1,728 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,514 IT Pros & Developers. It's quick & easy.

exception handling

P: n/a
The following code is for reading passwords in the "new" wndows os's,
ie xp etc. What is the purpose of exception handling if you are going
to throw awy the results?

//***********************************************
// IPC.h
//***********************************************
#ifndef _IPC_H_
#define _IPC_H_

#define IPC_SHARED_MMF _T("{34F673E0-878F-11D5-B98A-00B0D07B8C7C}")
#define IPC_MUTEX _T("{34F673E1-878F-11D5-B98A-00B0D07B8C7C}")

// Class for Inter Process Communication using Memory Mapped Files
class CIPC
{
public:
CIPC();
virtual ~CIPC();

bool CreateIPCMMF(void);
bool OpenIPCMMF(void);
void CloseIPCMMF(void);

bool IsOpen(void) const {return (m_hFileMap != NULL);}

bool ReadIPCMMF(LPBYTE pBuf, DWORD &dwBufSize);
bool WriteIPCMMF(const LPBYTE pBuf, const DWORD dwBufSize);

bool Lock(void);
void Unlock(void);

protected:
HANDLE m_hFileMap;
HANDLE m_hMutex;
};

#endif
//***********************************************
// IPC.cpp
//***********************************************
#include "IPC.h"

//***********************************************
CIPC::CIPC() : m_hFileMap(NULL), m_hMutex(NULL)
{
}

//***********************************************
CIPC::~CIPC()
{
CloseIPCMMF();
Unlock();
}

//***********************************************
bool CIPC::CreateIPCMMF(void)
{
bool bCreated = false;

try
{
if(m_hFileMap != NULL)
return false; // Already created

// Create an in-memory 4KB memory mapped
// file to share data
m_hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
4096,
IPC_SHARED_MMF);
if(m_hFileMap != NULL)
bCreated = true;
}
catch(...) {}

return bCreated;
}

//***********************************************
bool CIPC::OpenIPCMMF(void)
{
bool bOpened = false;

try
{
if(m_hFileMap != NULL)
return true; // Already opened

m_hFileMap =
OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE,
FALSE,
IPC_SHARED_MMF);
if(m_hFileMap != NULL)
bOpened = true;
}
catch(...) {}

return bOpened;
}

//***********************************************
void CIPC::CloseIPCMMF(void)
{
try
{
if(m_hFileMap != NULL)
CloseHandle(m_hFileMap), m_hFileMap = NULL;
}
catch(...) {}
}

//***********************************************
bool CIPC::ReadIPCMMF(LPBYTE pBuf, DWORD &dwBufSize)

{
_ASSERTE(pBuf);

bool bSuccess = true;

try
{
if(m_hFileMap == NULL)
return false;

DWORD dwBaseMMF = (DWORD)MapViewOfFile(m_hFileMap,
FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
_ASSERTE(dwBaseMMF);

// The first DWORD in the MMF contains the size of the data
DWORD dwSizeofInBuf = dwBufSize;
CopyMemory(&dwBufSize, (LPVOID)dwBaseMMF, sizeof(DWORD));

if(dwSizeofInBuf != 0)
{
if(dwBufSize > dwSizeofInBuf)
bSuccess = false;
else
CopyMemory(pBuf,
(LPVOID)(dwBaseMMF + sizeof(DWORD)),
dwBufSize);
}

UnmapViewOfFile((LPVOID)dwBaseMMF);
}
catch(...) {}

return bSuccess;
}

//***********************************************
bool CIPC::WriteIPCMMF(const LPBYTE pBuf, const DWORD dwBufSize)
{
_ASSERTE(pBuf);

bool bSuccess = true;

try
{
if(m_hFileMap == NULL)
return false;

DWORD dwBaseMMF = (DWORD)MapViewOfFile(m_hFileMap,
FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
_ASSERTE(dwBaseMMF);

// The first DWORD in the MMF contains the size of the data
CopyMemory((LPVOID)dwBaseMMF, &dwBufSize, sizeof(DWORD));
CopyMemory((LPVOID)(dwBaseMMF + sizeof(DWORD)),
pBuf, dwBufSize);

UnmapViewOfFile((LPVOID)dwBaseMMF);
}
catch(...) {}

return bSuccess;
}

//***********************************************
bool CIPC::Lock(void)
{
bool bLocked = false;

try
{
// First get the handle to the mutex
m_hMutex = CreateMutex(NULL, FALSE, IPC_MUTEX);
if(m_hMutex != NULL)
{
// Wait to get the lock on the mutex
if(WaitForSingleObject(m_hMutex, INFINITE) ==
WAIT_OBJECT_0)
bLocked = true;
}
}
catch(...) {}

return bLocked;
}

//***********************************************
void CIPC::Unlock(void)
{
try
{
if(m_hMutex != NULL)
{
ReleaseMutex(m_hMutex);
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
}
catch(...) {}
}
Oct 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John White wrote:
The following code is for reading passwords in the "new" wndows os's,
ie xp etc. What is the purpose of exception handling if you are going
to throw awy the results? [...]
bool CIPC::CreateIPCMMF(void)
{
bool bCreated = false;

try
{
if(m_hFileMap != NULL)
return false; // Already created

// Create an in-memory 4KB memory mapped
// file to share data
m_hFileMap = CreateFileMapping((HANDLE)0xFFFFFFFF,
NULL,
PAGE_READWRITE,
0,
4096,
IPC_SHARED_MMF);
if(m_hFileMap != NULL)
bCreated = true;
}
catch(...) {}

return bCreated;
}
[...]


There is no other way to wrap error reporting implemented by exception
throwing in a function that returns a simple success/failure value.

Apparently 'CreateFleMapping' can throw some unknown exception and the
author of 'CIPC::CreateIPCMMF' doesn't want to bother to act differently
based on what exception is thrown. IOW, it's irrelevant to the caller
of 'CreateIPCMMF' what exception is thrown or even whether it is thrown.
The caller wants to see if it succeeded or not. That's what the purpose
is, IMO.

V
Oct 31 '05 #2

P: n/a
John White wrote:
The following code is for reading passwords in the "new" wndows os's,
ie xp etc. What is the purpose of exception handling if you are going
to throw awy the results?


[snip uncompilable windows(?) code - please read the FAQ before
posting]

I can't read the original author's mind, but I suspect a construct like
this...

try
{
// some code that might throw
}
catch (...) {}

....is intended to prevent an exception from stopping execution. That's
fine, but don't catch every possible exception with '...'. Instead,
catch only specific exceptions that you know are not fatal to the
program. That way you allow things like bad_alloc (i.e., an exception
that most likely won't get better if execution continues) to propagate
up to someone more qualified to handle it, e.g., terminate().

FWIW, Win32 API calls can't throw since they're all actually C
functions. If your post was intended as a question about Windows
programming, then you should try one of the Microsoft newsgroups.

Kristo

Oct 31 '05 #3

P: n/a
Kristo wrote:
catch (...) {}

...is intended to prevent an exception from stopping execution. That's
fine, but don't catch every possible exception with '...'. Instead,
catch only specific exceptions that you know are not fatal to the
program. That way you allow things like bad_alloc (i.e., an exception
that most likely won't get better if execution continues) to propagate
up to someone more qualified to handle it, e.g., terminate().

FWIW, Win32 API calls can't throw since they're all actually C
functions. If your post was intended as a question about Windows
programming, then you should try one of the Microsoft newsgroups.

[BEGIN OFF TOPIC]

Win32 runtime exception through SEH are propagated as a C++ exception
(God knows, I've been bitten by that zillions of times). The problem is
that they have no known type that you can catch, so you *have* to catch
them with catch (...), unless you want to use MS SEH.
[END OFF TOPIC]

Yeah, catching ...
Oct 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.