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(...) {}
} 3 1819
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
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
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 ... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: adi |
last post by:
Dear all,
This is more like a theoretical or conceptual question:
which is better, using exception or return code for
a .NET component?
I had created a COM object (using VB6), which uses...
|
by: Daniel Wilson |
last post by:
I am having exception-handling and stability problems with .NET. I will
have a block of managed code inside try...catch and will still get a generic
..NET exception box that will tell me which...
|
by: Noor |
last post by:
please tell the technique of centralize exception handling without try catch
blocks in c#.
|
by: Master of C++ |
last post by:
Hi,
I am an absolute newbie to Exception Handling, and I am trying to
retrofit exception handling to a LOT of C++ code that I've written
earlier. I am just looking for a bare-bones, low-tech...
|
by: tom |
last post by:
Hi,
I am developing a WinForm application and I am looking for a guide on where
to place Exception Handling. My application is designed into three tiers
UI, Business Objects, and Data Access...
|
by: C# Learner |
last post by:
Some time ago, I remember reading a discussion about the strengths and
weaknesses of exception handling. One of the weaknesses that was put
forward was that exception handling is inefficient (in...
|
by: craig |
last post by:
I am wondering if there are some best practices for determining a strategy
for using try/catch blocks within an application.
My current thoughts are:
1. The code the initiates any high-level...
|
by: Ele |
last post by:
When Exception handling disabled compiler still spits out "C++ exception
handler used." Why is that?
Why does it ask for "Specify /EHsc"?
Thanks!
c:\Program Files\Microsoft Visual Studio...
|
by: Zytan |
last post by:
Ok something simple like int.Parse(string) can throw these exceptions:
ArgumentNullException, FormatException, OverflowException
I don't want my program to just crash on an exception, so I must...
|
by: George2 |
last post by:
Hello everyone,
Such code segment is used to check whether function call or exception-
handling mechanism runs out of memory first (written by Bjarne),
void perverted()
{
try{
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |