├── Clean.bat
├── snatshot.png
├── FastIpcTest
├── Test.cpp
├── FastIpcTest.vcxproj.filters
└── FastIpcTest.vcxproj
├── ServerTest
├── Test.cpp
├── ServerTest.vcxproj.filters
└── ServerTest.vcxproj
├── WinPipeWapper
├── snatshot.png
├── WinPipeWapperTest
│ ├── PipTest.cpp
│ ├── Pipe.h
│ ├── WinPipeWapperTest.filters
│ ├── WinPipeWapperTest.vcxproj.filters
│ ├── WinPipeWapperTest.vcxproj
│ └── Pipe.cpp
└── WinPipeWapperTest.sln
├── ReadMe.md
├── ClientOne
├── ClientOne.vcxproj.filters
├── Test.cpp
└── ClientOne.vcxproj
├── ClientTwo
├── ClientTwo.vcxproj.filters
├── Test.cpp
└── ClientTwo.vcxproj
├── FastIpcLib
├── memory
│ ├── heap.h
│ └── heap.cpp
├── security
│ ├── security_utils.h
│ └── security_utils.cpp
├── fast_ipc
│ ├── ipc_client.h
│ ├── ipc.h
│ ├── ipc_server.h
│ ├── ipc_types.h
│ ├── ipc_thread.h
│ ├── ipc_named_pipe.h
│ ├── ipc_client.cpp
│ ├── ipc.cpp
│ ├── ipc_thread.cpp
│ ├── ipc_server.cpp
│ └── ipc_named_pipe.cpp
├── defines.h
├── FastIpcLib.vcxproj.filters
└── FastIpcLib.vcxproj
└── FastIpcTest.sln
/Clean.bat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/Clean.bat
--------------------------------------------------------------------------------
/snatshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/snatshot.png
--------------------------------------------------------------------------------
/FastIpcTest/Test.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/FastIpcTest/Test.cpp
--------------------------------------------------------------------------------
/ServerTest/Test.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/ServerTest/Test.cpp
--------------------------------------------------------------------------------
/WinPipeWapper/snatshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/WinPipeWapper/snatshot.png
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/PipTest.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wanttobeno/WinFastIPC/HEAD/WinPipeWapper/WinPipeWapperTest/PipTest.cpp
--------------------------------------------------------------------------------
/ReadMe.md:
--------------------------------------------------------------------------------
1 | ### 进程间通信(IPC,InterProcess Communication)
2 |
3 | #### 例子一:WinPipeWapper
4 |
5 | 移除第三方头文件依赖,使用std::string作为消息容器。
6 |
7 | 封装为一个类,使用简单,适用于小项目。
8 |
9 | ```cpp
10 | void th_pipeWrite()
11 | {
12 | Pipe pipe;
13 | pipe.CreatePipe("test_pipe");
14 | int nCount = 1;
15 | char szNum[10] = { 0 };
16 | do
17 | {
18 | itoa(nCount, szNum, 10);
19 | nCount++;
20 | std::string strMsg = "WinPip中文123_测试";
21 | strMsg.append(szNum);
22 | pipe.PipeSendMessage(strMsg);
23 | // 写的时间间要大于读的时间间隔
24 | Sleep(500);
25 | } while (!g_bExit);
26 | }
27 | ```
28 |
29 |
30 | #### demo
31 |
32 |
33 |
34 |
35 |
36 | ---
37 |
38 | #### 例子二:FastIPC
39 |
40 | 代码从[DarkoreXOR](https://github.com/DarkoreXOR)的[hooklib](https://github.com/DarkoreXOR/hooklib)项目中提取的。
41 |
42 | #### 说明
43 | FastIpc使用管道来实现通信,支持多线程。
44 |
45 | 测试demo:
46 |
47 | 
48 |
49 |
50 |
--------------------------------------------------------------------------------
/ClientOne/ClientOne.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/ClientTwo/ClientTwo.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/ServerTest/ServerTest.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/FastIpcTest/FastIpcTest.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/Pipe.h:
--------------------------------------------------------------------------------
1 | /*
2 | Pipe.h
3 | Written by Matthew Fisher
4 |
5 | A pipe is a connection between two programs, possibly on different computers.
6 | */
7 | #include
8 |
9 | class Pipe
10 | {
11 | public:
12 | Pipe();
13 | ~Pipe();
14 | //
15 | // Connection
16 | //
17 | void ClosePipe();
18 | void CreatePipe(const std::string &PipeName);
19 | void ConnectToLocalPipe(const std::string &PipeName);
20 | void ConnectToPipe(const std::string &PipeName);
21 |
22 | //
23 | // Messaging
24 | //
25 | bool MessagePresent();
26 | bool ReadMessage(std::string &strRead);
27 | void PipeSendMessage(const BYTE *Message, UINT MessageLength);
28 | void PipeSendMessage(const std::string &Message);
29 |
30 | //
31 | // Query
32 | //
33 | UINT ActiveInstances();
34 | std::string UserName();
35 | __forceinline bool Valid()
36 | {
37 | return (_Handle != NULL);
38 | }
39 |
40 | private:
41 | HANDLE _Handle;
42 | };
43 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.21005.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WinPipeWapperTest", "WinPipeWapperTest\WinPipeWapperTest.vcxproj", "{4DE3F6D2-526B-4C87-A228-EDCC199720C4}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Win32 = Debug|Win32
11 | Release|Win32 = Release|Win32
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {4DE3F6D2-526B-4C87-A228-EDCC199720C4}.Debug|Win32.ActiveCfg = Debug|Win32
15 | {4DE3F6D2-526B-4C87-A228-EDCC199720C4}.Debug|Win32.Build.0 = Debug|Win32
16 | {4DE3F6D2-526B-4C87-A228-EDCC199720C4}.Release|Win32.ActiveCfg = Release|Win32
17 | {4DE3F6D2-526B-4C87-A228-EDCC199720C4}.Release|Win32.Build.0 = Release|Win32
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/WinPipeWapperTest.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 |
26 |
27 | Header Files
28 |
29 |
30 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/WinPipeWapperTest.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Source Files
6 |
7 |
8 | Source Files
9 |
10 |
11 |
12 |
13 | {f2b7a60a-b705-4143-b200-3fbcfd872315}
14 | h;hpp;hxx;hm;inl;inc;xsd
15 |
16 |
17 | {2ee7fe1b-43bf-4ebf-b107-f8de85a13265}
18 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
19 |
20 |
21 | {459c89f3-e92f-4f7c-b805-86294de9939c}
22 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav
23 |
24 |
25 |
26 |
27 | Header Files
28 |
29 |
30 |
--------------------------------------------------------------------------------
/ClientTwo/Test.cpp:
--------------------------------------------------------------------------------
1 | #include "../FastIpcLib/fast_ipc/ipc.h"
2 | #include
3 | #include
4 |
5 | #ifdef _DEBUG
6 | #pragma comment(lib,"../Debug/FastIpcLib.lib")
7 | #else
8 | #pragma comment(lib,"../Release/FastIpcLib.lib")
9 | #endif // _DEBUG
10 |
11 | #define IpcChannel _T("MyTestIpcChannel")
12 |
13 | DWORD WINAPI LoopSend(void* pParm)
14 | {
15 | LPCWSTR Message = L"Wow. There is Client Two Msg ...";
16 | DWORD MessageSize = (DWORD)((wcslen(Message) + 1) * sizeof(WCHAR));
17 | BOOL AnswerBool;
18 |
19 | while (TRUE)
20 | {
21 | if (!IpcSendIpcMessage(IpcChannel,
22 | (LPVOID)Message,
23 | MessageSize,
24 | &AnswerBool,
25 | sizeof(AnswerBool),
26 | INFINITE,
27 | FALSE))
28 | {
29 | printf("thread %d exit\n", GetCurrentThreadId());
30 | return 1;
31 | }
32 | }
33 | return 0;
34 | }
35 |
36 | int main(int agrc, char** agrv)
37 | {
38 | printf("This is Client Two \n");
39 |
40 | const DWORD NumOfThreads = 8;
41 | HANDLE ThreadHandles[NumOfThreads];
42 |
43 | for (int i = 0; i < NumOfThreads; ++i)
44 | {
45 | ThreadHandles[i] = CreateThread(NULL, 0, LoopSend, NULL, 0, NULL);
46 | }
47 |
48 | getchar();
49 |
50 | for (int i = 0; i < NumOfThreads; ++i)
51 | {
52 | CloseHandle(ThreadHandles[i]);
53 | }
54 |
55 | printf("all tests done\n");
56 | getchar();
57 | return 0;
58 | }
59 |
--------------------------------------------------------------------------------
/ClientOne/Test.cpp:
--------------------------------------------------------------------------------
1 | #include "../FastIpcLib/fast_ipc/ipc.h"
2 | #include
3 | #include
4 |
5 | #ifdef _DEBUG
6 | #pragma comment(lib,"../Debug/FastIpcLib.lib")
7 | #else
8 | #pragma comment(lib,"../Release/FastIpcLib.lib")
9 | #endif // _DEBUG
10 |
11 | #define IpcChannel _T("MyTestIpcChannel")
12 |
13 |
14 | DWORD WINAPI LoopSend(void* pParm)
15 | {
16 | LPCWSTR Message = L"Wow. There is Client One Msg ...";
17 | DWORD MessageSize = (DWORD)((wcslen(Message) + 1) * sizeof(WCHAR));
18 | BOOL AnswerBool;
19 |
20 | while (TRUE)
21 | {
22 | if (!IpcSendIpcMessage(IpcChannel,
23 | (LPVOID)Message,
24 | MessageSize,
25 | &AnswerBool,
26 | sizeof(AnswerBool),
27 | INFINITE,
28 | FALSE))
29 | {
30 | printf("thread %d exit\n", GetCurrentThreadId());
31 | return 1;
32 | }
33 | }
34 | return 0;
35 | }
36 |
37 | int main(int agrc, char** agrv)
38 | {
39 | printf("This is Client One \n");
40 |
41 | const DWORD NumOfThreads = 8;
42 | HANDLE ThreadHandles[NumOfThreads];
43 |
44 | for (int i = 0; i < NumOfThreads; ++i)
45 | {
46 | ThreadHandles[i] = CreateThread(NULL, 0, LoopSend, NULL, 0, NULL);
47 | }
48 |
49 | getchar();
50 |
51 | for (int i = 0; i < NumOfThreads; ++i)
52 | {
53 | CloseHandle(ThreadHandles[i]);
54 | }
55 |
56 | printf("all tests done\n");
57 | getchar();
58 | return 0;
59 | }
60 |
--------------------------------------------------------------------------------
/FastIpcLib/memory/heap.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __HEAP_H
31 | #define __HEAP_H
32 |
33 | #include
34 |
35 | LPVOID
36 | AllocMem(
37 | SIZE_T Size
38 | );
39 |
40 | BOOL
41 | DeallocMem(
42 | LPVOID MemoryPointer
43 | );
44 |
45 | VOID
46 | HeapInitialize();
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/FastIpcLib/security/security_utils.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __SECURITY_UTILS_H
31 | #define __SECURITY_UTILS_H
32 |
33 | #include
34 | #include "../memory/heap.h"
35 |
36 | BOOL
37 | ScCreateSecurityAttributes(
38 | PSECURITY_ATTRIBUTES SecurityAttributes,
39 | PSECURITY_DESCRIPTOR SecurityDescriptor,
40 | PSID *Sid
41 | );
42 |
43 | BOOL
44 | ScDestroySecurityAttributes(
45 | PSID Sid
46 | );
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_client.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_CLIENT_H
31 | #define __IPC_CLIENT_H
32 |
33 | #include
34 | #include "../defines.h"
35 | #include "ipc_types.h"
36 | #include "ipc_named_pipe.h"
37 |
38 | BOOL
39 | IpcClientCallIpcServer(
40 | LPCWSTR ChannelName,
41 | LPVOID MessageBuffer,
42 | DWORD MessageSize,
43 | LPVOID AnswerBuffer,
44 | DWORD AnswerSize,
45 | DWORD Timeout
46 | );
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/FastIpcLib/memory/heap.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "heap.h"
31 |
32 | HANDLE DefaultHeapHandle;
33 |
34 | LPVOID
35 | AllocMem(SIZE_T Size)
36 | {
37 | /*
38 | return HeapAlloc(DefaultHeapHandle,
39 | HEAP_ZERO_MEMORY,
40 | Size);
41 | */
42 |
43 | return malloc(Size);
44 | }
45 |
46 | BOOL
47 | DeallocMem(LPVOID MemoryPointer)
48 | {
49 | /*
50 | return HeapFree(DefaultHeapHandle,
51 | HEAP_ZERO_MEMORY,
52 | MemoryPointer);
53 | */
54 |
55 | if (!MemoryPointer)
56 | {
57 | return FALSE;
58 | }
59 |
60 | free(MemoryPointer);
61 |
62 | return TRUE;
63 | }
64 |
65 | VOID
66 | HeapInitialize()
67 | {
68 | DefaultHeapHandle = GetProcessHeap();
69 | }
70 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_H
31 | #define __IPC_H
32 |
33 | #include
34 | #include "../memory/heap.h"
35 | #include "../defines.h"
36 | #include "ipc_types.h"
37 | #include "ipc_named_pipe.h"
38 | #include "ipc_client.h"
39 | #include "ipc_server.h"
40 |
41 | BOOL
42 | IpcCreateIpcChannel(
43 | LPCWSTR ChannelName,
44 | IPC_ROUTINE Routine,
45 | BOOL MultiSession,
46 | PVOID *ChannelData
47 | );
48 |
49 | BOOL
50 | IpcDestroyIpcChannel(
51 | PVOID ChannelData
52 | );
53 |
54 | BOOL
55 | IpcSendIpcMessage(
56 | LPCWSTR ChannelName,
57 | PVOID MessageBuffer,
58 | DWORD MessageSize,
59 | PVOID AnswerBuffer,
60 | DWORD AnswerSize,
61 | DWORD Timeout,
62 | BOOL MultiSession
63 | );
64 |
65 | #endif
66 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_server.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_SERVER_H
31 | #define __IPC_SERVER_H
32 |
33 | #include
34 | #include "../defines.h"
35 | #include "ipc_types.h"
36 | #include "ipc_named_pipe.h"
37 | #include "ipc_thread.h"
38 |
39 | PIPC_CHANNEL_DATA
40 | IpcServerAllocateChannelData();
41 |
42 | BOOL
43 | IpcServerDeallocateChannelData(
44 | PIPC_CHANNEL_DATA ChannelData
45 | );
46 |
47 | BOOL
48 | IpcServerTryCreateChannel(
49 | LPCWSTR ChannelName,
50 | BOOL MultiSession
51 | );
52 |
53 | BOOL
54 | IpcServerCreateServerThread(
55 | LPCWSTR ChannelName,
56 | IPC_ROUTINE Routine,
57 | BOOL MultiSession,
58 | PTHREAD *Thread
59 | );
60 |
61 | BOOL
62 | IpcDestroyServerThread(
63 | PTHREAD *Thread
64 | );
65 |
66 |
67 |
68 | #endif
69 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_types.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_TYPES_H
31 | #define __IPC_TYPES_H
32 |
33 | #include "ipc_thread.h"
34 | #include "ipc_named_pipe.h"
35 |
36 | #define MAX(a, b) ((a) > (b) ? (a) : (b))
37 |
38 | typedef
39 | BOOL
40 | (WINAPI *IPC_ROUTINE)(
41 | LPCWSTR ChannelName,
42 | LPVOID MessageBuffer,
43 | DWORD MessageSize,
44 | LPVOID AnswerBuffer,
45 | DWORD AnswerSize
46 | );
47 |
48 | typedef struct _IPC_CHANNEL_DATA
49 | {
50 | PTHREAD ServerThread;
51 | NAMED_PIPE ServerNamedPipe;
52 | } IPC_CHANNEL_DATA, *PIPC_CHANNEL_DATA;
53 |
54 | typedef struct _IPC_SERVER_THREAD_DATA
55 | {
56 | WCHAR ChannelName[0x100];
57 | IPC_ROUTINE Routine;
58 | BOOL MultiSession;
59 | } IPC_SERVER_THREAD_DATA, *PIPC_SERVER_THREAD_DATA;
60 |
61 | typedef struct _IPC_CHANNEL_HEADER
62 | {
63 | BOOL HasMessage;
64 | BOOL HasAnswer;
65 | DWORD MessageSize;
66 | DWORD AnswerSize;
67 | } IPC_CHANNEL_HEADER, *PIPC_CHANNEL_HEADER;
68 |
69 | #endif
70 |
--------------------------------------------------------------------------------
/FastIpcLib/defines.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __DEFINES_H
31 | #define __DEFINES_H
32 |
33 | #include
34 |
35 | // POINTER
36 | #define PTR_ADD_OFFSET(base, offset) ((LPVOID)((ULONG_PTR)base + (offset)))
37 | #define PTR_SUB_OFFSET(base, offset) ((LPVOID)((ULONG_PTR)base - (offset)))
38 |
39 | // TRY FINALLY EXCEPT
40 | #define TRY(return_type, return_value) return_type __try_var_value = (return_value); __try
41 | #define FINALLY __finally
42 | #define EXCEPT __except
43 | #define LEAVE(return_value) __try_var_value = (return_value); __leave;
44 | #define TRY_VALUE (__try_var_value)
45 |
46 | // IF
47 | #define DO_IF(expr, do_expr) if (expr) { do_expr; }
48 | #define DO_ELSE(expr, do_expr) DO_IF(!(expr), do_expr)
49 |
50 | // LOOP
51 | #define CONTINUE continue;
52 | #define CONTINUE_IF(expr) DO_IF(expr, CONTINUE)
53 |
54 | #define BREAK break
55 | #define BREAK_IF(expr) DO_IF(expr, BREAK)
56 |
57 | #define RETURN return
58 | #define RETURN_IF(expr) DO_IF((expr), RETURN)
59 | #define RETURN_VALUE_IF(expr, value) DO_IF((expr), RETURN(value))
60 |
61 | #endif
62 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_thread.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_THREAD_H
31 | #define __IPC_THREAD_H
32 |
33 | #include
34 | #include "../defines.h"
35 | #include "../memory/heap.h"
36 |
37 | typedef struct _THREAD THREAD, *PTHREAD;
38 |
39 | typedef
40 | DWORD
41 | (*IPC_THREAD_ROUTINE)(
42 | PTHREAD Thread,
43 | LPVOID UserData
44 | );
45 |
46 | typedef struct _THREAD
47 | {
48 | DWORD ObjectId;
49 | HANDLE ObjectHandle;
50 | BOOL IsSuspended;
51 | LPVOID UserData;
52 | BOOL DoSafeStop;
53 | CRITICAL_SECTION CriticalSection;
54 | IPC_THREAD_ROUTINE ThreadRoutine;
55 | } THREAD, *PTHREAD;
56 |
57 | BOOL
58 | IpcCreateThread(
59 | IN IPC_THREAD_ROUTINE ThreadRoutine,
60 | IN BOOL CreateSuspended,
61 | IN LPVOID UserData,
62 | OUT PTHREAD *Thread
63 | );
64 |
65 | BOOL
66 | IpcDestroyThread(
67 | IN PTHREAD *Thread
68 | );
69 |
70 | VOID
71 | IpcSafeStopThread(
72 | IN PTHREAD Thread,
73 | IN BOOL WaitThread,
74 | IN DWORD Timeout
75 | );
76 |
77 | BOOL
78 | IpcIsSafeStopThread(
79 | IN PTHREAD Thread
80 | );
81 |
82 | BOOL
83 | IpcTerminateThread(
84 | IN PTHREAD Thread,
85 | IN DWORD ExitCode
86 | );
87 |
88 | BOOL
89 | IpcSuspendThread(
90 | IN PTHREAD Thread
91 | );
92 |
93 | BOOL
94 | IpcResumeThread(
95 | IN PTHREAD Thread
96 | );
97 |
98 | LPVOID
99 | IpcGetThreadUserData(
100 | IN PTHREAD Thread
101 | );
102 |
103 | #endif
104 |
--------------------------------------------------------------------------------
/FastIpcTest.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.40629.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FastIpcTest", "FastIpcTest\FastIpcTest.vcxproj", "{B4985F2D-CF8E-44AF-BB18-15A30616DD7B}"
7 | ProjectSection(ProjectDependencies) = postProject
8 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C} = {98DDCF3A-F102-456A-969C-38BF6D8BC73C}
9 | EndProjectSection
10 | EndProject
11 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ClientOne", "ClientOne\ClientOne.vcxproj", "{3CA58389-DAE3-4E8B-BBF8-3260BE36A249}"
12 | EndProject
13 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ClientTwo", "ClientTwo\ClientTwo.vcxproj", "{70900974-02AF-49F3-B039-6C67851DF1BA}"
14 | EndProject
15 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerTest", "ServerTest\ServerTest.vcxproj", "{5E7E3195-1082-46E1-B3D9-50B9F558E3B3}"
16 | EndProject
17 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FastIpcLib", "FastIpcLib\FastIpcLib.vcxproj", "{98DDCF3A-F102-456A-969C-38BF6D8BC73C}"
18 | EndProject
19 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{D8B4AB54-607E-448D-881B-EF6A1C8E8A0D}"
20 | EndProject
21 | Global
22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
23 | Debug|Win32 = Debug|Win32
24 | Release|Win32 = Release|Win32
25 | EndGlobalSection
26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
27 | {B4985F2D-CF8E-44AF-BB18-15A30616DD7B}.Debug|Win32.ActiveCfg = Debug|Win32
28 | {B4985F2D-CF8E-44AF-BB18-15A30616DD7B}.Debug|Win32.Build.0 = Debug|Win32
29 | {B4985F2D-CF8E-44AF-BB18-15A30616DD7B}.Release|Win32.ActiveCfg = Release|Win32
30 | {B4985F2D-CF8E-44AF-BB18-15A30616DD7B}.Release|Win32.Build.0 = Release|Win32
31 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249}.Debug|Win32.ActiveCfg = Debug|Win32
32 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249}.Debug|Win32.Build.0 = Debug|Win32
33 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249}.Release|Win32.ActiveCfg = Release|Win32
34 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249}.Release|Win32.Build.0 = Release|Win32
35 | {70900974-02AF-49F3-B039-6C67851DF1BA}.Debug|Win32.ActiveCfg = Debug|Win32
36 | {70900974-02AF-49F3-B039-6C67851DF1BA}.Debug|Win32.Build.0 = Debug|Win32
37 | {70900974-02AF-49F3-B039-6C67851DF1BA}.Release|Win32.ActiveCfg = Release|Win32
38 | {70900974-02AF-49F3-B039-6C67851DF1BA}.Release|Win32.Build.0 = Release|Win32
39 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3}.Debug|Win32.ActiveCfg = Debug|Win32
40 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3}.Debug|Win32.Build.0 = Debug|Win32
41 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3}.Release|Win32.ActiveCfg = Release|Win32
42 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3}.Release|Win32.Build.0 = Release|Win32
43 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C}.Debug|Win32.ActiveCfg = Debug|Win32
44 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C}.Debug|Win32.Build.0 = Debug|Win32
45 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C}.Release|Win32.ActiveCfg = Release|Win32
46 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C}.Release|Win32.Build.0 = Release|Win32
47 | EndGlobalSection
48 | GlobalSection(SolutionProperties) = preSolution
49 | HideSolutionNode = FALSE
50 | EndGlobalSection
51 | GlobalSection(NestedProjects) = preSolution
52 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249} = {D8B4AB54-607E-448D-881B-EF6A1C8E8A0D}
53 | {70900974-02AF-49F3-B039-6C67851DF1BA} = {D8B4AB54-607E-448D-881B-EF6A1C8E8A0D}
54 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3} = {D8B4AB54-607E-448D-881B-EF6A1C8E8A0D}
55 | EndGlobalSection
56 | EndGlobal
57 |
--------------------------------------------------------------------------------
/FastIpcLib/FastIpcLib.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 | {db5f4933-0b0a-4842-b2fe-63be8b35a11e}
18 |
19 |
20 | {86d24412-c8fa-4664-a071-84533953d4f8}
21 |
22 |
23 | {afee81ef-8679-4498-b532-8964340f65ee}
24 |
25 |
26 | {6cda0acf-d1a2-456d-8231-86638d277cea}
27 |
28 |
29 | {bd5ac2be-468f-4584-a901-18b31a9b5b66}
30 |
31 |
32 | {6e8c83a9-d953-4437-820c-8cfd69f38172}
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | 头文件\fast_ipc
41 |
42 |
43 | 头文件\fast_ipc
44 |
45 |
46 | 头文件\fast_ipc
47 |
48 |
49 | 头文件\fast_ipc
50 |
51 |
52 | 头文件\fast_ipc
53 |
54 |
55 | 头文件\fast_ipc
56 |
57 |
58 | 头文件\memory
59 |
60 |
61 | 头文件\security
62 |
63 |
64 | 头文件
65 |
66 |
67 |
68 |
69 | 源文件\fast_ipc
70 |
71 |
72 | 源文件\fast_ipc
73 |
74 |
75 | 源文件\fast_ipc
76 |
77 |
78 | 源文件\fast_ipc
79 |
80 |
81 | 源文件\fast_ipc
82 |
83 |
84 | 源文件\memory
85 |
86 |
87 | 源文件\security
88 |
89 |
90 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_named_pipe.h:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #ifndef __IPC_NAMED_PIPE_H
31 | #define __IPC_NAMED_PIPE_H
32 |
33 | #include
34 | #include "../defines.h"
35 | #include "../security/security_utils.h"
36 |
37 | #define IPC_NAMED_PIPE_NAME_LENGTH 0x100
38 |
39 | typedef struct _NAMED_PIPE
40 | {
41 | WCHAR ObjectName[IPC_NAMED_PIPE_NAME_LENGTH];
42 | HANDLE ObjectHandle;
43 | HANDLE EventHandle;
44 | } NAMED_PIPE, *PNAMED_PIPE;
45 |
46 | typedef
47 | BOOL
48 | (*NAMED_PIPE_LEAVE_PROC)(
49 | LPVOID Context
50 | );
51 |
52 | BOOL
53 | IpcCreateNamedPipe(
54 | IN LPCWSTR ObjectName,
55 | IN BOOL FirstInstance,
56 | IN DWORD OutBufferSize,
57 | IN DWORD InBufferSize,
58 | IN DWORD DefaultTimeout,
59 | OUT PNAMED_PIPE NamedPipe
60 | );
61 |
62 | BOOL
63 | IpcDestroyNamedPipe(
64 | IN PNAMED_PIPE NamedPiped
65 | );
66 |
67 | BOOL
68 | IpcOpenNamedPipe(
69 | IN LPCWSTR ObjectName,
70 | IN BOOL WaitIfNotAvailable,
71 | OUT PNAMED_PIPE NamedPipe
72 | );
73 |
74 | BOOL
75 | IpcCloseNamedPipe(
76 | IN PNAMED_PIPE NamedPipe
77 | );
78 |
79 | BOOL
80 | IpcWaitConnectClient(
81 | IN PNAMED_PIPE NamedPipe,
82 | IN DWORD Timeout,
83 | IN LPVOID Context,
84 | IN NAMED_PIPE_LEAVE_PROC LeaveProc
85 | );
86 |
87 | BOOL
88 | IpcDisconnectClient(
89 | IN PNAMED_PIPE NamedPipe
90 | );
91 |
92 | BOOL
93 | IpcReadFromNamedPipe(
94 | IN PNAMED_PIPE NamedPipe,
95 | IN LPVOID Buffer,
96 | IN DWORD BufferSize,
97 | IN BOOL IsServerSide,
98 | IN LPVOID Context,
99 | IN NAMED_PIPE_LEAVE_PROC LeaveProc
100 | );
101 |
102 | BOOL
103 | IpcWriteToNamedPipe(
104 | IN PNAMED_PIPE NamedPipe,
105 | IN LPVOID Buffer,
106 | IN DWORD BufferSize,
107 | IN BOOL IsServerSide,
108 | IN LPVOID Context,
109 | IN NAMED_PIPE_LEAVE_PROC LeaveProc
110 | );
111 |
112 | BOOL
113 | IpcFlushNamedPipe(
114 | IN PNAMED_PIPE NamedPipe
115 | );
116 |
117 | #endif
118 |
--------------------------------------------------------------------------------
/ClientOne/ClientOne.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {3CA58389-DAE3-4E8B-BBF8-3260BE36A249}
15 | Win32Proj
16 | ClientOne
17 |
18 |
19 |
20 | Application
21 | true
22 | v120
23 | Unicode
24 |
25 |
26 | Application
27 | false
28 | v120
29 | true
30 | Unicode
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | true
44 |
45 |
46 | false
47 |
48 |
49 |
50 |
51 |
52 | Level3
53 | Disabled
54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
55 |
56 |
57 | Console
58 | true
59 |
60 |
61 |
62 |
63 | Level3
64 |
65 |
66 | MaxSpeed
67 | true
68 | true
69 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
70 |
71 |
72 | Console
73 | true
74 | true
75 | true
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/ClientTwo/ClientTwo.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {70900974-02AF-49F3-B039-6C67851DF1BA}
15 | Win32Proj
16 | ClientTwo
17 |
18 |
19 |
20 | Application
21 | true
22 | v120
23 | Unicode
24 |
25 |
26 | Application
27 | false
28 | v120
29 | true
30 | Unicode
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | true
44 |
45 |
46 | false
47 |
48 |
49 |
50 |
51 |
52 | Level3
53 | Disabled
54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
55 |
56 |
57 | Console
58 | true
59 |
60 |
61 |
62 |
63 | Level3
64 |
65 |
66 | MaxSpeed
67 | true
68 | true
69 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
70 |
71 |
72 | Console
73 | true
74 | true
75 | true
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/FastIpcTest/FastIpcTest.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {B4985F2D-CF8E-44AF-BB18-15A30616DD7B}
15 | Win32Proj
16 | FastIpcTest
17 |
18 |
19 |
20 | Application
21 | true
22 | v120
23 | Unicode
24 |
25 |
26 | Application
27 | false
28 | v120
29 | true
30 | Unicode
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | true
44 |
45 |
46 | false
47 |
48 |
49 |
50 |
51 |
52 | Level3
53 | Disabled
54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
55 |
56 |
57 | Console
58 | true
59 |
60 |
61 |
62 |
63 | Level3
64 |
65 |
66 | MaxSpeed
67 | true
68 | true
69 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
70 |
71 |
72 | Console
73 | true
74 | true
75 | true
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/ServerTest/ServerTest.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {5E7E3195-1082-46E1-B3D9-50B9F558E3B3}
15 | Win32Proj
16 | ServerTest
17 |
18 |
19 |
20 | Application
21 | true
22 | v120
23 | Unicode
24 |
25 |
26 | Application
27 | false
28 | v120
29 | true
30 | Unicode
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | true
44 |
45 |
46 | false
47 |
48 |
49 |
50 |
51 |
52 | Level3
53 | Disabled
54 | WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
55 |
56 |
57 | Console
58 | true
59 |
60 |
61 |
62 |
63 | Level3
64 |
65 |
66 | MaxSpeed
67 | true
68 | true
69 | WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)
70 |
71 |
72 | Console
73 | true
74 | true
75 | true
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_client.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "ipc_client.h"
31 |
32 | BOOL
33 | IpcClientCallIpcServer(LPCWSTR ChannelName,
34 | LPVOID MessageBuffer,
35 | DWORD MessageSize,
36 | LPVOID AnswerBuffer,
37 | DWORD AnswerSize,
38 | DWORD Timeout)
39 | {
40 | NAMED_PIPE Pipe;
41 | IPC_CHANNEL_HEADER Header;
42 | BOOL Accepted;
43 |
44 | TRY(BOOL, TRUE)
45 | {
46 | if (!IpcOpenNamedPipe(ChannelName,
47 | TRUE,
48 | &Pipe))
49 | {
50 | LEAVE(FALSE);
51 | }
52 |
53 | Header.HasMessage = MessageSize && MessageBuffer;
54 | Header.HasAnswer = AnswerSize && AnswerBuffer;
55 | Header.MessageSize = MessageSize;
56 | Header.AnswerSize = AnswerSize;
57 |
58 | if (!IpcWriteToNamedPipe(&Pipe,
59 | &Header,
60 | sizeof(IPC_CHANNEL_HEADER),
61 | FALSE,
62 | NULL,
63 | NULL))
64 | {
65 | LEAVE(FALSE);
66 | }
67 |
68 | if (!IpcReadFromNamedPipe(&Pipe,
69 | &Accepted,
70 | sizeof(BOOL),
71 | FALSE,
72 | NULL,
73 | NULL))
74 | {
75 | LEAVE(FALSE);
76 | }
77 |
78 | if (!IpcWriteToNamedPipe(&Pipe,
79 | MessageBuffer,
80 | MessageSize,
81 | FALSE,
82 | NULL,
83 | NULL))
84 | {
85 | LEAVE(FALSE);
86 | }
87 |
88 | if (Header.HasAnswer && !IpcReadFromNamedPipe(&Pipe,
89 | AnswerBuffer,
90 | AnswerSize,
91 | FALSE,
92 | NULL,
93 | NULL))
94 | {
95 | LEAVE(FALSE);
96 | }
97 | }
98 | FINALLY
99 | {
100 | IpcCloseNamedPipe(&Pipe);
101 | }
102 |
103 | return TRY_VALUE;
104 | }
105 |
106 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "ipc.h"
31 |
32 | BOOL
33 | IpcCreateIpcChannel(LPCWSTR ChannelName,
34 | IPC_ROUTINE Routine,
35 | BOOL MultiSession,
36 | PVOID *ChannelData)
37 | {
38 | PIPC_CHANNEL_DATA ChannelDataBuffer = NULL;
39 | PTHREAD Thread;
40 |
41 | if (!ChannelData)
42 | {
43 | return FALSE;
44 | }
45 |
46 | if (!(ChannelDataBuffer = IpcServerAllocateChannelData()))
47 | {
48 | return FALSE;
49 | }
50 |
51 | if (!IpcServerTryCreateChannel(ChannelName,
52 | MultiSession))
53 | {
54 | IpcServerDeallocateChannelData(ChannelDataBuffer);
55 | return FALSE;
56 | }
57 |
58 | if (!IpcServerCreateServerThread(ChannelName,
59 | Routine,
60 | MultiSession,
61 | &Thread))
62 | {
63 | IpcServerDeallocateChannelData(ChannelDataBuffer);
64 | return FALSE;
65 | }
66 |
67 | ChannelDataBuffer->ServerThread = Thread;
68 |
69 | IpcResumeThread(Thread);
70 |
71 | *ChannelData = ChannelDataBuffer;
72 |
73 | return TRUE;
74 | }
75 |
76 | BOOL
77 | IpcDestroyIpcChannel(PVOID ChannelData)
78 | {
79 | BOOL Result;
80 | PIPC_CHANNEL_DATA ChannelDataBuffer;
81 |
82 | if (!ChannelData)
83 | {
84 | return FALSE;
85 | }
86 |
87 | ChannelDataBuffer = (PIPC_CHANNEL_DATA)ChannelData;
88 |
89 | IpcSafeStopThread(ChannelDataBuffer->ServerThread,
90 | TRUE,
91 | INFINITE);
92 |
93 | Result = IpcDestroyServerThread(&ChannelDataBuffer->ServerThread);
94 | Result = IpcServerDeallocateChannelData(ChannelDataBuffer) && Result;
95 |
96 | return Result;
97 | }
98 |
99 | BOOL
100 | IpcSendIpcMessage(LPCWSTR ChannelName,
101 | PVOID MessageBuffer,
102 | DWORD MessageSize,
103 | PVOID AnswerBuffer,
104 | DWORD AnswerSize,
105 | DWORD Timeout,
106 | BOOL MultiSession)
107 | {
108 | if (!IpcClientCallIpcServer(ChannelName,
109 | MessageBuffer,
110 | MessageSize,
111 | AnswerBuffer,
112 | AnswerSize,
113 | Timeout))
114 | {
115 | return FALSE;
116 | }
117 |
118 | return TRUE;
119 | }
120 |
--------------------------------------------------------------------------------
/FastIpcLib/security/security_utils.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "security_utils.h"
31 |
32 | BOOL
33 | ScCreateSecurityAttributes(PSECURITY_ATTRIBUTES SecurityAttributes,
34 | PSECURITY_DESCRIPTOR SecurityDescriptor,
35 | PSID *Sid)
36 | {
37 | BOOL result;
38 | DWORD sidSize = 0;
39 | WCHAR domainName[0x100];
40 | DWORD domainNameLength = 0x100;
41 | WCHAR userName[0x400];
42 | DWORD userNameSize = sizeof(WCHAR) * 0x100;
43 | SID_NAME_USE sidNameUse;
44 |
45 | if (!Sid)
46 | {
47 | return FALSE;
48 | }
49 |
50 | result = GetUserNameW(userName, &userNameSize);
51 |
52 | if (!result)
53 | {
54 | return FALSE;
55 | }
56 |
57 | result = LookupAccountNameW(NULL,
58 | userName,
59 | NULL,
60 | &sidSize,
61 | domainName,
62 | &domainNameLength,
63 | &sidNameUse);
64 |
65 | if (result || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
66 | {
67 | return FALSE;
68 | }
69 |
70 | *Sid = (PSID)AllocMem(sidSize);
71 |
72 | if (!(*Sid))
73 | {
74 | return FALSE;
75 | }
76 |
77 | result = LookupAccountNameW(NULL,
78 | userName,
79 | *Sid,
80 | &sidSize,
81 | domainName,
82 | &domainNameLength,
83 | &sidNameUse);
84 |
85 | if (!result)
86 | {
87 | DeallocMem(*Sid);
88 | return FALSE;
89 | }
90 |
91 | result = InitializeSecurityDescriptor(SecurityDescriptor,
92 | SECURITY_DESCRIPTOR_REVISION);
93 |
94 | if (!result)
95 | {
96 | DeallocMem(*Sid);
97 | return FALSE;
98 | }
99 |
100 | result = SetSecurityDescriptorOwner(SecurityDescriptor,
101 | *Sid,
102 | TRUE);
103 |
104 | if (!result)
105 | {
106 | DeallocMem(*Sid);
107 | return FALSE;
108 | }
109 |
110 | result = SetSecurityDescriptorDacl(SecurityDescriptor,
111 | TRUE,
112 | NULL,
113 | TRUE);
114 |
115 | if (!result)
116 | {
117 | DeallocMem(*Sid);
118 | return FALSE;
119 | }
120 |
121 | SecurityAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
122 | SecurityAttributes->lpSecurityDescriptor = SecurityDescriptor;
123 | SecurityAttributes->bInheritHandle = FALSE;
124 |
125 | return TRUE;
126 | }
127 |
128 | BOOL
129 | ScDestroySecurityAttributes(PSID Sid)
130 | {
131 | DeallocMem(Sid);
132 | return TRUE;
133 | }
134 |
135 |
--------------------------------------------------------------------------------
/FastIpcLib/FastIpcLib.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {98DDCF3A-F102-456A-969C-38BF6D8BC73C}
15 | Win32Proj
16 | FastIpcLib
17 |
18 |
19 |
20 | StaticLibrary
21 | true
22 | v120
23 | Unicode
24 |
25 |
26 | StaticLibrary
27 | false
28 | v120
29 | true
30 | Unicode
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | Level3
48 | Disabled
49 | WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)
50 |
51 |
52 | Windows
53 | true
54 |
55 |
56 |
57 |
58 | Level3
59 |
60 |
61 | MaxSpeed
62 | true
63 | true
64 | WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)
65 |
66 |
67 | Windows
68 | true
69 | true
70 | true
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/WinPipeWapperTest.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 |
14 | {4DE3F6D2-526B-4C87-A228-EDCC199720C4}
15 | PipTest
16 | Win32Proj
17 | WinPipWapperTest
18 |
19 |
20 |
21 | Application
22 | v120
23 | Unicode
24 | true
25 |
26 |
27 | Application
28 | v120
29 | Unicode
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | <_ProjectFileVersion>12.0.21005.1
43 |
44 |
45 | $(SolutionDir)$(Configuration)\
46 | $(Configuration)\
47 | true
48 |
49 |
50 | $(SolutionDir)$(Configuration)\
51 | $(Configuration)\
52 | false
53 |
54 |
55 |
56 | Disabled
57 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
58 | true
59 | EnableFastChecks
60 | MultiThreadedDebugDLL
61 |
62 | Level3
63 | EditAndContinue
64 |
65 |
66 | true
67 | Console
68 | MachineX86
69 |
70 |
71 |
72 |
73 | MaxSpeed
74 | true
75 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
76 | MultiThreadedDLL
77 | true
78 |
79 | Level3
80 | ProgramDatabase
81 |
82 |
83 | true
84 | Console
85 | true
86 | true
87 | MachineX86
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_thread.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "ipc_thread.h"
31 |
32 | DWORD
33 | WINAPI
34 | IpcSystemThreadRoutine(IN LPVOID Parameter)
35 | {
36 | PTHREAD CurrentThread;
37 |
38 | if (!Parameter)
39 | {
40 | return 1;
41 | }
42 |
43 | CurrentThread = (PTHREAD)Parameter;
44 |
45 | return CurrentThread->ThreadRoutine(CurrentThread,
46 | CurrentThread->UserData);
47 | }
48 |
49 | BOOL
50 | IpcCreateThread(IN IPC_THREAD_ROUTINE ThreadRoutine,
51 | IN BOOL CreateSuspended,
52 | IN LPVOID UserData,
53 | OUT PTHREAD *Thread)
54 | {
55 | PTHREAD NewThread = NULL;
56 |
57 | TRY(BOOL, TRUE)
58 | {
59 | if (!(NewThread = (PTHREAD)AllocMem(sizeof(THREAD))))
60 | {
61 | LEAVE(FALSE);
62 | }
63 |
64 | InitializeCriticalSection(&NewThread->CriticalSection);
65 |
66 | NewThread->DoSafeStop = FALSE;
67 | NewThread->IsSuspended = CreateSuspended;
68 | NewThread->ThreadRoutine = ThreadRoutine;
69 | NewThread->UserData = UserData;
70 | NewThread->ObjectHandle = CreateThread(NULL,
71 | 0,
72 | IpcSystemThreadRoutine,
73 | NewThread,
74 | CREATE_SUSPENDED,
75 | &NewThread->ObjectId);
76 |
77 | if (!NewThread->ObjectHandle)
78 | {
79 | LEAVE(FALSE);
80 | }
81 |
82 | if (!CreateSuspended)
83 | {
84 | ResumeThread(NewThread->ObjectHandle);
85 | }
86 | }
87 | FINALLY
88 | {
89 | if (!TRY_VALUE && NewThread)
90 | {
91 | DeleteCriticalSection(&NewThread->CriticalSection);
92 | TerminateThread(NewThread->ObjectHandle, 0);
93 | CloseHandle(NewThread->ObjectHandle);
94 | DeallocMem(NewThread);
95 | NewThread = NULL;
96 | }
97 | }
98 |
99 | *Thread = NewThread;
100 |
101 | return TRY_VALUE;
102 | }
103 |
104 | BOOL
105 | IpcDestroyThread(IN PTHREAD *Thread)
106 | {
107 | BOOL Result;
108 |
109 | DeleteCriticalSection(&(*Thread)->CriticalSection);
110 | Result = CloseHandle((*Thread)->ObjectHandle);
111 | Result = DeallocMem(*Thread) && Result;
112 |
113 | *Thread = NULL;
114 |
115 | return Result;
116 | }
117 |
118 | VOID
119 | IpcSafeStopThread(IN PTHREAD Thread,
120 | IN BOOL WaitThread,
121 | IN DWORD Timeout)
122 | {
123 | EnterCriticalSection(&Thread->CriticalSection);
124 | Thread->DoSafeStop = TRUE;
125 | LeaveCriticalSection(&Thread->CriticalSection);
126 |
127 | if (WaitThread)
128 | {
129 | WaitForSingleObject(Thread->ObjectHandle, Timeout);
130 | }
131 | }
132 |
133 | BOOL
134 | IpcIsSafeStopThread(IN PTHREAD Thread)
135 | {
136 | BOOL Result;
137 | EnterCriticalSection(&Thread->CriticalSection);
138 | Result = Thread->DoSafeStop;
139 | LeaveCriticalSection(&Thread->CriticalSection);
140 | return Result;
141 | }
142 |
143 | BOOL
144 | IpcTerminateThread(IN PTHREAD Thread,
145 | IN DWORD ExitCode)
146 | {
147 | return TerminateThread(Thread->ObjectHandle, ExitCode);
148 | }
149 |
150 | BOOL
151 | IpcSuspendThread(IN PTHREAD Thread)
152 | {
153 | if (Thread->IsSuspended)
154 | {
155 | return FALSE;
156 | }
157 |
158 | SuspendThread(Thread->ObjectHandle);
159 | Thread->IsSuspended = TRUE;
160 |
161 | return TRUE;
162 | }
163 |
164 | BOOL
165 | IpcResumeThread(IN PTHREAD Thread)
166 | {
167 | if (!Thread->IsSuspended)
168 | {
169 | return FALSE;
170 | }
171 |
172 | ResumeThread(Thread->ObjectHandle);
173 | Thread->IsSuspended = FALSE;
174 |
175 | return TRUE;
176 | }
177 |
178 | LPVOID
179 | IpcGetThreadUserData(IN PTHREAD Thread)
180 | {
181 | return Thread->UserData;
182 | }
183 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_server.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "ipc_server.h"
31 |
32 | PIPC_CHANNEL_DATA
33 | IpcServerAllocateChannelData()
34 | {
35 | return (PIPC_CHANNEL_DATA)AllocMem(sizeof(IPC_CHANNEL_DATA));
36 | }
37 |
38 | BOOL
39 | IpcServerDeallocateChannelData(PIPC_CHANNEL_DATA ChannelData)
40 | {
41 | return DeallocMem(ChannelData);
42 | }
43 |
44 | BOOL
45 | IpcServerTryCreateChannel(LPCWSTR ChannelName,
46 | BOOL MultiSession)
47 | {
48 | NAMED_PIPE Pipe;
49 |
50 | TRY(BOOL, TRUE)
51 | {
52 | if (!IpcCreateNamedPipe(ChannelName,
53 | TRUE,
54 | 0x1000,
55 | 0x1000,
56 | NMPWAIT_WAIT_FOREVER,
57 | &Pipe))
58 | {
59 | LEAVE(FALSE);
60 | }
61 | }
62 | FINALLY
63 | {
64 | IpcDestroyNamedPipe(&Pipe);
65 | }
66 |
67 | return TRY_VALUE;
68 | }
69 |
70 | DWORD
71 | IpcServerThread(PTHREAD Thread,
72 | LPVOID UserData)
73 | {
74 | PIPC_SERVER_THREAD_DATA ThreadData;
75 | NAMED_PIPE Pipe;
76 | IPC_CHANNEL_HEADER Header;
77 | LPVOID InternalBuffer = NULL;
78 | BOOL Accepted;
79 |
80 | ThreadData = (PIPC_SERVER_THREAD_DATA)UserData;
81 |
82 | TRY(DWORD, 0)
83 | {
84 | if (!IpcCreateNamedPipe(ThreadData->ChannelName,
85 | FALSE,
86 | 0x1000,
87 | 0x1000,
88 | NMPWAIT_WAIT_FOREVER,
89 | &Pipe))
90 | {
91 | LEAVE(1);
92 | }
93 |
94 | while (TRUE)
95 | {
96 | if (!IpcWaitConnectClient(&Pipe,
97 | INFINITE,
98 | Thread,
99 | (NAMED_PIPE_LEAVE_PROC)IpcIsSafeStopThread))
100 | {
101 | LEAVE(1);
102 | }
103 |
104 | if (!IpcReadFromNamedPipe(&Pipe,
105 | &Header,
106 | sizeof(IPC_CHANNEL_HEADER),
107 | TRUE,
108 | Thread,
109 | (NAMED_PIPE_LEAVE_PROC)IpcIsSafeStopThread))
110 | {
111 | LEAVE(1);
112 | }
113 |
114 | Accepted = TRUE;
115 |
116 | if (!IpcWriteToNamedPipe(&Pipe,
117 | &Accepted,
118 | sizeof(BOOL),
119 | TRUE,
120 | Thread,
121 | (NAMED_PIPE_LEAVE_PROC)IpcIsSafeStopThread))
122 | {
123 | LEAVE(1);
124 | }
125 |
126 | if (!(InternalBuffer = AllocMem(MAX(Header.MessageSize,
127 | Header.AnswerSize))))
128 | {
129 | LEAVE(1);
130 | }
131 |
132 | if (Header.HasMessage && !IpcReadFromNamedPipe(&Pipe,
133 | InternalBuffer,
134 | Header.MessageSize,
135 | TRUE,
136 | Thread,
137 | (NAMED_PIPE_LEAVE_PROC)IpcIsSafeStopThread))
138 | {
139 | IpcFlushNamedPipe(&Pipe);
140 | IpcDisconnectClient(&Pipe);
141 | DeallocMem(InternalBuffer);
142 | InternalBuffer = NULL;
143 | continue;
144 | }
145 |
146 | ThreadData->Routine(ThreadData->ChannelName,
147 | InternalBuffer,
148 | Header.MessageSize,
149 | InternalBuffer,
150 | Header.AnswerSize);
151 |
152 | if (Header.HasAnswer)
153 | {
154 | IpcWriteToNamedPipe(&Pipe,
155 | InternalBuffer,
156 | Header.AnswerSize,
157 | TRUE,
158 | Thread,
159 | (NAMED_PIPE_LEAVE_PROC)IpcIsSafeStopThread);
160 | }
161 |
162 | IpcFlushNamedPipe(&Pipe);
163 | IpcDisconnectClient(&Pipe);
164 | DeallocMem(InternalBuffer);
165 | InternalBuffer = NULL;
166 |
167 | }
168 | }
169 | FINALLY
170 | {
171 | IpcDisconnectClient(&Pipe);
172 | IpcDestroyNamedPipe(&Pipe);
173 | DeallocMem(InternalBuffer);
174 | }
175 |
176 | return TRY_VALUE;
177 | }
178 |
179 | BOOL
180 | IpcServerCreateServerThread(LPCWSTR ChannelName,
181 | IPC_ROUTINE Routine,
182 | BOOL MultiSession,
183 | PTHREAD *Thread)
184 | {
185 | PIPC_SERVER_THREAD_DATA UserData;
186 |
187 | UserData = (PIPC_SERVER_THREAD_DATA)
188 | AllocMem(sizeof(IPC_SERVER_THREAD_DATA));
189 |
190 | if (!UserData)
191 | {
192 | return FALSE;
193 | }
194 |
195 | wcscpy_s(UserData->ChannelName,
196 | ChannelName);
197 |
198 | UserData->Routine = Routine;
199 | UserData->MultiSession = MultiSession;
200 |
201 | if (!IpcCreateThread(IpcServerThread,
202 | TRUE,
203 | UserData,
204 | Thread))
205 | {
206 | DeallocMem(UserData);
207 | return FALSE;
208 | }
209 |
210 | return TRUE;
211 | }
212 |
213 | BOOL
214 | IpcDestroyServerThread(PTHREAD *Thread)
215 | {
216 | BOOL Result;
217 | LPVOID UserData;
218 |
219 | UserData = IpcGetThreadUserData(*Thread);
220 | Result = DeallocMem(UserData);
221 | Result = IpcTerminateThread(*Thread, 0) && Result;
222 | Result = IpcDestroyThread(Thread) && Result;
223 |
224 | return Result;
225 | }
226 |
227 |
--------------------------------------------------------------------------------
/WinPipeWapper/WinPipeWapperTest/Pipe.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Pipe.cpp
3 | Written by Matthew Fisher
4 |
5 | A pipe is a connection between two programs, possibly on different computers.
6 | */
7 |
8 | #include
9 | #include
10 | #include "Pipe.h"
11 |
12 | #define Assert(x,y) assert(x)
13 |
14 | Pipe::Pipe()
15 | {
16 | _Handle = NULL;
17 | }
18 |
19 | Pipe::~Pipe()
20 | {
21 | ClosePipe();
22 | }
23 |
24 | void Pipe::ClosePipe()
25 | {
26 | if(_Handle != NULL)
27 | {
28 | FlushFileBuffers(_Handle);
29 | DisconnectNamedPipe(_Handle);
30 | CloseHandle(_Handle);
31 | _Handle = NULL;
32 | }
33 | }
34 |
35 | void Pipe::CreatePipe(const std::string &PipeName)
36 | {
37 | ClosePipe();
38 | const UINT PipeBufferSize = 100000;
39 |
40 | DWORD dwRes;
41 | PSID pEveryoneSID = NULL, pAdminSID = NULL;
42 | PACL pACL = NULL;
43 | PSECURITY_DESCRIPTOR pSD = NULL;
44 | EXPLICIT_ACCESS ea[1];
45 | SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
46 | SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
47 | SECURITY_ATTRIBUTES Attributes;
48 | HKEY hkSub = NULL;
49 |
50 | // Create a well-known SID for the Everyone group.
51 | BOOL Success = AllocateAndInitializeSid(&SIDAuthWorld, 1,
52 | SECURITY_WORLD_RID,
53 | 0, 0, 0, 0, 0, 0, 0,
54 | &pEveryoneSID);
55 | Assert(Success != FALSE, "AllocateAndInitializeSid failed in Pipe::CreatePipe");
56 |
57 | // Initialize an EXPLICIT_ACCESS structure for an ACE.
58 | // The ACE will allow Everyone read access to the key.
59 | ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
60 | ea[0].grfAccessPermissions = FILE_ALL_ACCESS;
61 | ea[0].grfAccessMode = SET_ACCESS;
62 | ea[0].grfInheritance= NO_INHERITANCE;
63 | ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
64 | ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
65 | ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;
66 |
67 | // Create a new ACL that contains the new ACEs.
68 | dwRes = SetEntriesInAcl(1, ea, NULL, &pACL);
69 | Assert(dwRes == ERROR_SUCCESS, "SetEntriesInAcl failed in Pipe::CreatePipe");
70 |
71 | // Initialize a security descriptor.
72 | pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
73 | Assert(pSD != NULL, "LocalAlloc failed in Pipe::CreatePipe");
74 |
75 | Success = InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);
76 | Assert(Success != FALSE, "InitializeSecurityDescriptor failed in Pipe::CreatePipe");
77 |
78 | // Add the ACL to the security descriptor.
79 | Success = SetSecurityDescriptorDacl(pSD,
80 | TRUE, // bDaclPresent flag
81 | pACL,
82 | FALSE);
83 | Assert(Success != FALSE, "SetSecurityDescriptorDacl failed in Pipe::CreatePipe");
84 |
85 | // Initialize a security attributes structure.
86 | Attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
87 | Attributes.lpSecurityDescriptor = pSD;
88 | Attributes.bInheritHandle = FALSE;
89 |
90 | std::string FullPipeName = std::string("\\\\.\\pipe\\") + PipeName;
91 | _Handle = CreateNamedPipeA(
92 | FullPipeName.c_str(), // pipe name
93 | PIPE_ACCESS_DUPLEX, // read/write access
94 | PIPE_TYPE_MESSAGE | // message type pipe
95 | PIPE_READMODE_MESSAGE | // message-read mode
96 | PIPE_WAIT, // blocking mode
97 | PIPE_UNLIMITED_INSTANCES, // max. instances
98 | PipeBufferSize, // output buffer size
99 | PipeBufferSize, // input buffer size
100 | NMPWAIT_USE_DEFAULT_WAIT, // client time-out
101 | &Attributes); // default security attribute
102 | Assert(_Handle != INVALID_HANDLE_VALUE, "CreateNamedPipe failed in Pipe::CreatePipe");
103 |
104 | //
105 | // Block until a connection comes in
106 | //
107 | BOOL Connected = (ConnectNamedPipe(_Handle, NULL) != 0);
108 | Assert(Connected != FALSE, "ConnectNamedPipe failed in Pipe::CreatePipe");
109 | }
110 |
111 | void Pipe::ConnectToLocalPipe(const std::string &PipeName)
112 | {
113 | ConnectToPipe(std::string("\\\\.\\pipe\\") + PipeName);
114 | }
115 |
116 | void Pipe::ConnectToPipe(const std::string &PipeName)
117 | {
118 | ClosePipe();
119 | bool Done = false;
120 | while(!Done)
121 | {
122 | _Handle = CreateFileA(
123 | PipeName.c_str(), // pipe name
124 | GENERIC_READ | // read and write access
125 | GENERIC_WRITE,
126 | 0, // no sharing
127 | NULL, // default security attributes
128 | OPEN_EXISTING, // opens existing pipe
129 | 0, // default attributes
130 | NULL); // no template file
131 | if(_Handle != INVALID_HANDLE_VALUE)
132 | {
133 | Done = true;
134 | }
135 | Sleep(100);
136 | }
137 |
138 | DWORD Mode = PIPE_READMODE_MESSAGE;
139 | BOOL Success = SetNamedPipeHandleState(
140 | _Handle, // pipe handle
141 | &Mode, // new pipe mode
142 | NULL, // don't set maximum bytes
143 | NULL); // don't set maximum time
144 | Assert(Success != FALSE, "SetNamedPipeHandleState failed in Pipe::ConnectToPipe");
145 | }
146 |
147 | bool Pipe::MessagePresent()
148 | {
149 | Assert(_Handle != NULL, "Pipe invalid in Pipe::MessagePresent");
150 | DWORD BytesReady = 0;
151 | BOOL Success = PeekNamedPipe(
152 | _Handle,
153 | NULL,
154 | 0,
155 | NULL,
156 | &BytesReady,
157 | NULL);
158 | Assert(Success != FALSE, "PeekNamedPipe failed in Pipe::MessagePresent");
159 | return (BytesReady > 0);
160 | }
161 |
162 | bool Pipe::ReadMessage(std::string &strRead)
163 | {
164 | Assert(_Handle != NULL, "Pipe invalid in Pipe::ReadMessage");
165 | DWORD BytesReady = 0;
166 | BOOL Success = PeekNamedPipe(
167 | _Handle,
168 | NULL,
169 | 0,
170 | NULL,
171 | &BytesReady,
172 | NULL);
173 | Assert(Success != FALSE, "PeekNamedPipe failed in Pipe::ReadMessage");
174 |
175 | if(BytesReady == 0)
176 | {
177 | return false;
178 | }
179 |
180 | DWORD BytesRead;
181 | char buf[512] = {0};
182 | do
183 | {
184 | Success = ReadFile(
185 | _Handle, // handle to pipe
186 | buf, // buffer to receive data
187 | 512, // size of buffer
188 | &BytesRead, // number of bytes read
189 | NULL); // not overlapped I/O
190 |
191 | strRead.append(buf);
192 | if (BytesRead<512)
193 | break;
194 | } while (!Success);
195 |
196 | // Assert(Success != FALSE && BytesRead > 0, "ReadFile failed in Pipe::ReadMessage");
197 | return true;
198 | }
199 |
200 | void Pipe::PipeSendMessage(const std::string &Message)
201 | {
202 | const char* pData = Message.c_str();
203 | PipeSendMessage((const BYTE*)pData, Message.size());
204 | }
205 |
206 | void Pipe::PipeSendMessage(const BYTE *Message, UINT MessageLength)
207 | {
208 | Assert(_Handle != NULL, "Pipe invalid in Pipe::SendMessage");
209 |
210 | DWORD BytesWritten;
211 | BOOL Success = WriteFile(
212 | _Handle, // pipe handle
213 | Message, // message
214 | MessageLength, // message length
215 | &BytesWritten, // bytes written
216 | NULL); // not overlapped
217 | Assert(Success != FALSE, "WriteFile failed in Pipe::ReadMessage");
218 | Assert(BytesWritten == MessageLength, "WriteFile failed to send entire message in Pipe::ReadMessage");
219 | }
220 |
221 | UINT Pipe::ActiveInstances()
222 | {
223 | Assert(_Handle != NULL, "Pipe invalid in Pipe::ActiveInstances");
224 | DWORD Instances;
225 | BOOL Success = GetNamedPipeHandleState(
226 | _Handle,
227 | NULL,
228 | &Instances,
229 | NULL,
230 | NULL,
231 | NULL,
232 | 0);
233 | Assert(Success != FALSE, "GetNamedPipeHandleState failed in Pipe::ActiveInstances");
234 | return Instances;
235 | }
236 |
237 | std::string Pipe::UserName()
238 | {
239 | Assert(_Handle != NULL, "Pipe invalid in Pipe::UserName");
240 | char Buffer[512];
241 | BOOL Success = GetNamedPipeHandleStateA(
242 | _Handle,
243 | NULL,
244 | NULL,
245 | NULL,
246 | NULL,
247 | Buffer,
248 | 512);
249 | Assert(Success != FALSE, "GetNamedPipeHandleState failed in Pipe::UserName");
250 | return std::string(Buffer);
251 | }
252 |
--------------------------------------------------------------------------------
/FastIpcLib/fast_ipc/ipc_named_pipe.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * [ BSD License: http://opensource.org/licenses/bsd-license.php ]
3 | * ===========================================================================
4 | * Copyright (c) 2015, Lakutin Ivan
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | *
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 | */
29 |
30 | #include "ipc_named_pipe.h"
31 |
32 | VOID
33 | IpcMakePipeName(OUT LPWSTR DestinationName,
34 | IN SIZE_T DestinationNameSize,
35 | IN LPCWSTR SourceName)
36 | {
37 | wcscpy_s(DestinationName,
38 | DestinationNameSize,
39 | L"\\\\.\\pipe\\");
40 |
41 | wcscat_s(DestinationName,
42 | DestinationNameSize,
43 | SourceName);
44 | }
45 |
46 | BOOL
47 | IpcCreateNamedPipe(IN LPCWSTR ObjectName,
48 | IN BOOL FirstInstance,
49 | IN DWORD OutBufferSize,
50 | IN DWORD InBufferSize,
51 | IN DWORD DefaultTimeout,
52 | OUT PNAMED_PIPE NamedPipe)
53 | {
54 | DWORD OpenMode;
55 | DWORD PipeMode;
56 | PSID sid;
57 | SECURITY_DESCRIPTOR sd = { 0 };
58 | SECURITY_ATTRIBUTES sa = { 0 };
59 |
60 | IpcMakePipeName(NamedPipe->ObjectName,
61 | 0x100,
62 | ObjectName);
63 |
64 | NamedPipe->EventHandle = CreateEventW(NULL,
65 | TRUE,
66 | TRUE,
67 | NULL);
68 |
69 | if (!NamedPipe->EventHandle)
70 | {
71 | return FALSE;
72 | }
73 |
74 | OpenMode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED;
75 | OpenMode |= FirstInstance ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0;
76 |
77 | PipeMode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE;
78 | PipeMode |= PIPE_WAIT;
79 | PipeMode |= PIPE_REJECT_REMOTE_CLIENTS;
80 |
81 | if (!ScCreateSecurityAttributes(&sa, &sd, &sid))
82 | {
83 | NamedPipe->ObjectHandle = 0;
84 | CloseHandle(NamedPipe->EventHandle);
85 | NamedPipe->EventHandle = 0;
86 | return FALSE;
87 | }
88 |
89 | NamedPipe->ObjectHandle = CreateNamedPipeW(NamedPipe->ObjectName,
90 | OpenMode,
91 | PipeMode,
92 | PIPE_UNLIMITED_INSTANCES,
93 | OutBufferSize,
94 | InBufferSize,
95 | DefaultTimeout,
96 | &sa);
97 |
98 | ScDestroySecurityAttributes(sid);
99 |
100 | if (NamedPipe->ObjectHandle == INVALID_HANDLE_VALUE ||
101 | GetLastError() == ERROR_ACCESS_DENIED)
102 | {
103 | NamedPipe->ObjectHandle = 0;
104 | CloseHandle(NamedPipe->EventHandle);
105 | NamedPipe->EventHandle = 0;
106 | return FALSE;
107 | }
108 |
109 | return TRUE;
110 | }
111 |
112 | #include
113 |
114 | BOOL
115 | IpcDestroyNamedPipe(IN PNAMED_PIPE NamedPipe)
116 | {
117 | BOOL Result = FALSE;
118 |
119 | if (!NamedPipe->ObjectHandle || !NamedPipe->EventHandle)
120 | {
121 | return FALSE;
122 | }
123 |
124 | TRY(BOOL, TRUE)
125 | {
126 | Result = CloseHandle(NamedPipe->EventHandle);
127 | NamedPipe->EventHandle = 0;
128 | Result = CloseHandle(NamedPipe->ObjectHandle) && Result;
129 | NamedPipe->ObjectHandle = 0;
130 | LEAVE(Result);
131 | }
132 | EXCEPT(EXCEPTION_EXECUTE_HANDLER)
133 | {
134 | return TRY_VALUE;
135 | }
136 |
137 | return Result;
138 | }
139 |
140 | UINT32
141 | IpcGetCurrentTimeMilliseconds()
142 | {
143 | UINT32 Milliseconds = 0;
144 | SYSTEMTIME Time;
145 | GetSystemTime(&Time);
146 |
147 | Milliseconds += Time.wMinute * 60 * 1000;
148 | Milliseconds += Time.wSecond * 1000;
149 | Milliseconds += Time.wMilliseconds;
150 |
151 | return Milliseconds;
152 | }
153 |
154 | BOOL
155 | IpcOpenNamedPipe(IN LPCWSTR ObjectName,
156 | IN BOOL WaitIfBusy,
157 | OUT PNAMED_PIPE NamedPipe)
158 | {
159 | DWORD LastError;
160 |
161 | IpcMakePipeName(NamedPipe->ObjectName,
162 | 0x100,
163 | ObjectName);
164 |
165 | NamedPipe->EventHandle = CreateEventW(NULL,
166 | TRUE,
167 | TRUE,
168 | NULL);
169 |
170 | if (!NamedPipe->EventHandle)
171 | {
172 | return FALSE;
173 | }
174 |
175 | while (TRUE)
176 | {
177 | NamedPipe->ObjectHandle = CreateFileW(NamedPipe->ObjectName,
178 | GENERIC_READ | GENERIC_WRITE,
179 | 0,
180 | NULL,
181 | OPEN_EXISTING,
182 | 0,
183 | NULL);
184 |
185 | LastError = GetLastError();
186 |
187 | if (NamedPipe->ObjectHandle == INVALID_HANDLE_VALUE &&
188 | LastError == ERROR_FILE_NOT_FOUND)
189 | {
190 | NamedPipe->ObjectHandle = 0;
191 | CloseHandle(NamedPipe->EventHandle);
192 | NamedPipe->EventHandle = 0;
193 | return FALSE;
194 | }
195 |
196 | if (WaitIfBusy &&
197 | NamedPipe->ObjectHandle &&
198 | LastError == ERROR_PIPE_BUSY)
199 | {
200 | WaitNamedPipeW(NamedPipe->ObjectName,
201 | NMPWAIT_WAIT_FOREVER);
202 | continue;
203 | }
204 |
205 | break;
206 | }
207 |
208 | if (!SetNamedPipeHandleState(NamedPipe->ObjectHandle,
209 | PIPE_READMODE_BYTE | PIPE_WAIT,
210 | NULL,
211 | NULL))
212 | {
213 | CloseHandle(NamedPipe->EventHandle);
214 | NamedPipe->EventHandle = 0;
215 | CloseHandle(NamedPipe->ObjectHandle);
216 | NamedPipe->ObjectHandle = 0;
217 | return FALSE;
218 | }
219 |
220 | return TRUE;
221 | }
222 |
223 | BOOL
224 | IpcCloseNamedPipe(IN PNAMED_PIPE NamedPipe)
225 | {
226 | BOOL Result = FALSE;
227 |
228 | if (!NamedPipe->ObjectHandle || !NamedPipe->EventHandle)
229 | {
230 | return FALSE;
231 | }
232 |
233 | TRY(BOOL, TRUE)
234 | {
235 | Result = CloseHandle(NamedPipe->EventHandle);
236 | NamedPipe->EventHandle = 0;
237 | Result = CloseHandle(NamedPipe->ObjectHandle) && Result;
238 | NamedPipe->ObjectHandle = 0;
239 | LEAVE(Result);
240 | }
241 | EXCEPT(EXCEPTION_EXECUTE_HANDLER)
242 | {
243 | return TRY_VALUE;
244 | }
245 |
246 | return Result;
247 | }
248 |
249 | BOOL
250 | IpcWaitConnectClient(IN PNAMED_PIPE NamedPipe,
251 | IN DWORD Timeout,
252 | IN LPVOID Context,
253 | IN NAMED_PIPE_LEAVE_PROC LeaveProc)
254 | {
255 | BOOL Result;
256 | volatile OVERLAPPED Overlapped = { 0 };
257 | DWORD UnknownValue;
258 | DWORD LastError;
259 |
260 | Overlapped.hEvent = NamedPipe->EventHandle;
261 |
262 | Result = ConnectNamedPipe(NamedPipe->ObjectHandle,
263 | (LPOVERLAPPED)&Overlapped);
264 |
265 | if (Result)
266 | {
267 | return FALSE;
268 | }
269 |
270 | LastError = GetLastError();
271 |
272 | if (LastError == ERROR_PIPE_CONNECTED)
273 | {
274 | return TRUE;
275 | }
276 |
277 | if (LastError == ERROR_IO_PENDING)
278 | {
279 | while (TRUE)
280 | {
281 | Result = GetOverlappedResult(NamedPipe->ObjectHandle,
282 | (LPOVERLAPPED)&Overlapped,
283 | &UnknownValue,
284 | FALSE);
285 |
286 | if (!Result && GetLastError() != ERROR_IO_INCOMPLETE)
287 | {
288 | break;
289 | }
290 |
291 | if (Result && GetLastError() == ERROR_BROKEN_PIPE)
292 | {
293 | break;
294 | }
295 |
296 | if (HasOverlappedIoCompleted(&Overlapped))
297 | {
298 | break;
299 | }
300 |
301 | if (LeaveProc && LeaveProc(Context))
302 | {
303 | break;
304 | }
305 | }
306 | }
307 |
308 | Result = GetOverlappedResult(NamedPipe->ObjectHandle,
309 | (LPOVERLAPPED)&Overlapped,
310 | &UnknownValue,
311 | FALSE);
312 |
313 | return Result;
314 | }
315 |
316 | BOOL
317 | IpcDisconnectClient(IN PNAMED_PIPE NamedPipe)
318 | {
319 | return DisconnectNamedPipe(NamedPipe->ObjectHandle);
320 | }
321 |
322 | BOOL
323 | IpcReadFromNamedPipe(IN PNAMED_PIPE NamedPipe,
324 | IN LPVOID Buffer,
325 | IN DWORD BufferSize,
326 | IN BOOL IsServerSide,
327 | IN LPVOID Context,
328 | IN NAMED_PIPE_LEAVE_PROC LeaveProc)
329 | {
330 | BOOL Result;
331 | DWORD Read;
332 | volatile OVERLAPPED Overlapped = { 0 };
333 | DWORD LastError;
334 |
335 | Overlapped.hEvent = NamedPipe->EventHandle;
336 |
337 | Result = ReadFile(NamedPipe->ObjectHandle,
338 | Buffer,
339 | BufferSize,
340 | &Read,
341 | IsServerSide ? (LPOVERLAPPED)&Overlapped : NULL);
342 |
343 | if (Result)
344 | {
345 | return Read == BufferSize;
346 | }
347 | else if (!IsServerSide)
348 | {
349 | return FALSE;
350 | }
351 | else
352 | {
353 | LastError = GetLastError();
354 |
355 | if (LastError == ERROR_BROKEN_PIPE)
356 | {
357 | return FALSE;
358 | }
359 |
360 | if (LastError == ERROR_HANDLE_EOF)
361 | {
362 | return FALSE;
363 | }
364 |
365 | if (LastError == ERROR_IO_PENDING)
366 | {
367 | while (TRUE)
368 | {
369 | if (HasOverlappedIoCompleted(&Overlapped))
370 | {
371 | break;
372 | }
373 | }
374 | }
375 |
376 | Result = GetOverlappedResult(NamedPipe->ObjectHandle,
377 | (LPOVERLAPPED)&Overlapped,
378 | &Read,
379 | FALSE);
380 | }
381 |
382 | return Result && Read == BufferSize;
383 | }
384 |
385 | BOOL
386 | IpcWriteToNamedPipe(IN PNAMED_PIPE NamedPipe,
387 | IN LPVOID Buffer,
388 | IN DWORD BufferSize,
389 | IN BOOL IsServerSide,
390 | IN LPVOID Context,
391 | IN NAMED_PIPE_LEAVE_PROC LeaveProc)
392 | {
393 | BOOL Result;
394 | DWORD Written;
395 | volatile OVERLAPPED Overlapped = { 0 };
396 | DWORD LastError;
397 |
398 | Overlapped.hEvent = NamedPipe->EventHandle;
399 |
400 | Result = WriteFile(NamedPipe->ObjectHandle,
401 | Buffer,
402 | BufferSize,
403 | &Written,
404 | IsServerSide ? (LPOVERLAPPED)&Overlapped : NULL);
405 |
406 | if (Result)
407 | {
408 | return Written == BufferSize;
409 | }
410 | else if (!IsServerSide)
411 | {
412 | return FALSE;
413 | }
414 | else
415 | {
416 | LastError = GetLastError();
417 |
418 | if (LastError == ERROR_BROKEN_PIPE)
419 | {
420 | return FALSE;
421 | }
422 |
423 | if (LastError == ERROR_HANDLE_EOF)
424 | {
425 | return FALSE;
426 | }
427 |
428 | if (LastError == ERROR_IO_PENDING)
429 | {
430 | while (TRUE)
431 | {
432 | if (HasOverlappedIoCompleted(&Overlapped))
433 | {
434 | break;
435 | }
436 | }
437 | }
438 |
439 | Result = GetOverlappedResult(NamedPipe->ObjectHandle,
440 | (LPOVERLAPPED)&Overlapped,
441 | &Written,
442 | FALSE);
443 | }
444 |
445 | return Result && Written == BufferSize;
446 | }
447 |
448 | BOOL
449 | IpcFlushNamedPipe(IN PNAMED_PIPE NamedPipe)
450 | {
451 | return FlushFileBuffers(NamedPipe->ObjectHandle);
452 | }
453 |
--------------------------------------------------------------------------------