Hello Danial.
07 Apr 97 16:53, Danial Gibson wrote to All:
DG> a program to chat between the computers. So I read the win32 sdk
DG> and thought, gee, pipes look good. So I did that for two hours
DG> wondering why it wouldn't work. Found out it's cause pipes are only in
DG> nt, not 95.
I've been down that alley to :-(
DG> 1. Why does it get it twice?
dont know!
DG> 2. Will it _always_ get it twice?
I think so.
DG> 3. If the answer to 2 is no, then how do I fix it?
After reading your message i made a small program to see if it was true -
and it was. Maybe it is a bug in the windows SDK, or maybe there is a reason
for it - not really obvious to me though.
I dont know which compiler you're using. I'm using MS Visual C++ 4.0. If
you're using the same compiler, it could be a problem with the compiler.
The short of the long is, that I found a way to get by it. Not in a very
elegant way, but it works: The basic idea is to only take every 2 messages
from the mailslot, when you are reading it, using the MODULUS (%) function.
The next few lines of code shows the functions I'm using (do not get confused
with the MFC syntax - the mailslot functions are pure API code using the
Windows SDK).
Most of the code comes directly from the Visual C++ online help.
////////////////////// Start
BOOL CMailSlotDlg::Makeslot()
{
LPSTR lpszSlotName = "\\\\.\\mailslot\\sample_mailslot";
/* The mailslot handle "hSlot1" is declared globally. */
m_hSlot1 = CreateMailslot(lpszSlotName,
0, /* no maximum message size */
MAILSLOT_WAIT_FOREVER, /* no time-out for read operations */
(LPSECURITY_ATTRIBUTES) NULL); /* no security attributes */
if (m_hSlot1 == INVALID_HANDLE_VALUE) {
ErrorHandler("CreateMailslot"); /* local error handler */
return FALSE;
}
SetDlgItemText(IDC_STATUS, "CreateMailslot successful.");
return TRUE;
}
void CMailSlotDlg::ErrorHandler(LPSTR msg)
{
MessageBox(msg);
}
BOOL CMailSlotDlg::Readslot()
{
DWORD cbMessage, cMessage, cbRead;
BOOL fResult;
LPSTR lpszBuffer;
CHAR achID[80];
DWORD cAllMessages;
cbMessage = cMessage = cbRead = 0;
/* Mailslot handle "hSlot1" is declared globally. */
fResult = GetMailslotInfo(m_hSlot1, /* mailslot handle */
(LPDWORD) NULL, /* no maximum message size */
&cbMessage, /* size of next message */
&cMessage, /* number of messages */
(LPDWORD) NULL); /* no read time-out */
if (!fResult) {
ErrorHandler("GetMailslotInfo");
return FALSE;
}
if (cbMessage == MAILSLOT_NO_MESSAGE) {
SetDlgItemText(IDC_STATUS, "No waiting messages.");
return TRUE;
}
cAllMessages = cMessage;
int counter = 0;
while (cMessage != 0) { /* retrieves each message */
counter ++;
/* Create a message-number string. */
wsprintf((LPSTR) achID,
"\nMessage #%d of %d\n", (cAllMessages - (cMessage))/2 + 1,
cAllMessages/2);
/* Allocate memory for the message. */
lpszBuffer = (LPSTR) GlobalAlloc(GPTR,
lstrlen((LPSTR) achID) + cbMessage);
lpszBuffer[0] = '\0';
fResult = ReadFile(m_hSlot1,
lpszBuffer,
cbMessage,
&cbRead,
(LPOVERLAPPED) NULL);
if (!fResult) {
ErrorHandler("ReadFile");
GlobalFree((HGLOBAL) lpszBuffer);
return FALSE;
}
/* Concatenate the message and the message-number string. */
lstrcat(lpszBuffer, (LPSTR) achID);
/* Display the message. */
if (counter %2 == 0)
MessageBox(lpszBuffer, "Contents of Mailslot", MB_OK);
GlobalFree((HGLOBAL) lpszBuffer);
fResult = GetMailslotInfo(m_hSlot1, /* mailslot handle */
(LPDWORD) NULL, /* no maximum message size */
&cbMessage, /* size of next message */
&cMessage, /* number of messages */
(LPDWORD) NULL); /* no read time-out */
if (!fResult) {
ErrorHandler("GetMailslotInfo");
return FALSE;
}
}
return TRUE;
}
void CMailSlotDlg::OnCheckmail()
{
// TODO: Add your control notification handler code here
Readslot();
}
BOOL CMailSlotDlg::WriteMessage(LPSTR lpszMessage)
{
//LPSTR lpszMessage = "12_Bogstaver";
BOOL fResult;
HANDLE hFile;
DWORD cbWritten;
hFile = CreateFile("\\\\*\\mailslot\\sample_mailslot",
GENERIC_WRITE,
FILE_SHARE_READ, /* required to write to a mailslot */
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (hFile == INVALID_HANDLE_VALUE) {
ErrorHandler("Primary domain"); /* local error handler */
return FALSE;
}
fResult = WriteFile(hFile,
lpszMessage,
(DWORD) lstrlen(lpszMessage) + 1, /* include terminat. null char. */
&cbWritten,
(LPOVERLAPPED) NULL);
if (!fResult) {
ErrorHandler("WriteFile");
return FALSE;
}
SetDlgItemText(IDC_STATUS, "WriteFile successful.");
fResult = CloseHandle(hFile);
if (!fResult) {
ErrorHandler("CloseHandle");
return FALSE;
}
SetDlgItemText(IDC_STATUS, "CloseHandle successful.");
return TRUE;
}
void CMailSlotDlg::OnSendmail()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_EDIT, m_NewMessage);
WriteMessage(PCHAR(m_NewMessage));
}
////////////////////// End
Good luck!
Please tell me, if you find a more elegant way to solve the problem
Christian
--- GoldED 2.50 UNREG
---------------
* Origin: Kokain i Kinder‘g - det jo hele 4 ting (2:235/335.22)
|