mailslot用法

code number 1:

 

/*************************************/

#include <windows.h>

#include <tchar.h>

#include <stdio.h>

 

HANDLE hSlot;

LPTSTR SlotName = TEXT(".//mailslot//sample_mailslot");

 

BOOL WriteSlot(LPTSTR lpszMessage)

{

   BOOL fResult; 

   DWORD cbWritten; 

   HANDLE hFile;

   hFile = CreateFile(SlotName,GENERIC_WRITE,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

   fResult = WriteFile(hFile, 

     lpszMessage, 

     (DWORD) (lstrlen(lpszMessage)+1)*sizeof(TCHAR),  

     &cbWritten, 

     (LPOVERLAPPED) NULL); 

 

   if (!fResult) 

   { 

      printf("WriteFile failed with %d./n", GetLastError()); 

 CloseHandle(hFile);

      return FALSE; 

   } 

 

   printf("Slot written to successfully./n"); 

   CloseHandle(hFile);

   return TRUE;

}

 

 

BOOL ReadSlot() 

    DWORD cbMessage, cMessage, cbRead; 

    BOOL fResult; 

    TCHAR lpszBuffer[512] = {0}; 

    DWORD cAllMessages; 

    HANDLE hEvent;

    OVERLAPPED ov;

 

    cbMessage = cMessage = cbRead = 0; 

 

    hEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("ExampleSlot"));

    if( NULL == hEvent )

        return FALSE;

    ov.Offset = 0;

    ov.OffsetHigh = 0;

    ov.hEvent = hEvent;

 

    fResult = GetMailslotInfo( hSlot, // 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) 

    { 

        printf("GetMailslotInfo failed with %d./n", GetLastError()); 

        return FALSE; 

    } 

 

    if (cbMessage == MAILSLOT_NO_MESSAGE) 

    { 

        printf("Waiting for a message.../n"); 

        return TRUE; 

    } 

 

    cAllMessages = cMessage; 

 

    while (cMessage != 0)  // retrieve all messages

    { 

 

        lpszBuffer[0] = '/0'; 

 

        fResult = ReadFile(hSlot, 

            lpszBuffer, 

            cbMessage, 

            &cbRead, 

            &ov); 

 

        if (!fResult) 

        { 

            printf("ReadFile failed with %d./n", GetLastError()); 

            return FALSE; 

        }  

 

        // Display the message. 

 

        _tprintf(TEXT("Contents of the mailslot: %s/n"), lpszBuffer); 

 

 

        fResult = GetMailslotInfo(hSlot,  // 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) 

        { 

            printf("GetMailslotInfo failed (%d)/n", GetLastError());

            return FALSE; 

        } 

    } 

    CloseHandle(hEvent);

    return TRUE; 

}

 

BOOL WINAPI MakeSlot(LPTSTR lpszSlotName) 

SECURITY_ATTRIBUTES stSecurityDesc = {0};

stSecurityDesc.nLength = sizeof(SECURITY_ATTRIBUTES);

stSecurityDesc.lpSecurityDescriptor = NULL;

stSecurityDesc.bInheritHandle = TRUE;

    hSlot = CreateMailslot(lpszSlotName, 

        0,                             // no maximum message size 

        MAILSLOT_WAIT_FOREVER,         // no time-out for operations 

        (LPSECURITY_ATTRIBUTES) NULL); // default security

 

    if (hSlot == INVALID_HANDLE_VALUE) 

    { 

        printf("CreateMailslot failed with %d/n", GetLastError());

        return FALSE; 

    } 

    return TRUE; 

}

 

void main()

{

   MakeSlot(SlotName);

 

   while(TRUE)

   {

      ReadSlot();

      Sleep(1000);

 WriteSlot("hello");

 Sleep(1000);

   }

   CloseHandle(hSlot);

}

 

 

 

 

/*************************************/

 

 

code number 2:

 

/**************************/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值