1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | import sys import mmap import time from ctypes import * from ctypes.wintypes import BOOL, DWORD, HANDLE, LPCWSTR, LPCVOID, LPVOID import header class Mmf: def __init__(self, nameOfMmf): if(type(nameOfMmf) != type("")): print("nameOfMmf 인자가 문자열이 아닙니다.") return 1 self.FILE_MAP_ALL_ACCESS = 0x000F001F # 플래그의 상수값 self.INVALID_HANDLE_VALUE = -1 # 시스템 페이지를 사용하기위한 파일디스크립터(INVALID_FILE_HANDLER) self.SHMEMSIZE = 0x100 # 공유메모리 크기 self.PAGE_READWRITE = 0x04 # 플래그의 상수값 self.FILE_MAP_READ = 0x0004 # 플래그의 상수값 self.kernel32_dll = windll.kernel32 self.msvcrt_dll = cdll.msvcrt self.create_file_mapping_func = self.kernel32_dll.CreateFileMappingW self.create_file_mapping_func.argtypes = (HANDLE, LPVOID, DWORD, DWORD, DWORD, LPCWSTR) self.create_file_mapping_func.restype = HANDLE self.open_file_mapping_func = self.kernel32_dll.OpenFileMappingW self.open_file_mapping_func.argtypes = (DWORD, BOOL, LPCWSTR) self.open_file_mapping_func.restype = HANDLE self.map_view_of_file_func = self.kernel32_dll.MapViewOfFile self.map_view_of_file_func.argtypes = (HANDLE, DWORD, DWORD, DWORD, c_ulonglong) self.map_view_of_file_func.restype = LPVOID self.memcpy_func = self.msvcrt_dll.memcpy self.memcpy_func.argtypes = (c_void_p, c_void_p, c_size_t) self.memcpy_func.restype = LPVOID self.rtl_copy_memory_func = self.kernel32_dll.RtlCopyMemory self.rtl_copy_memory_func.argtypes = (LPVOID, LPCVOID, c_ulonglong) self.unmap_view_of_file_func = self.kernel32_dll.UnmapViewOfFile self.unmap_view_of_file_func.argtypes = (LPCVOID, ) self.unmap_view_of_file_func.restype = BOOL self.memcpy_func = self.msvcrt_dll.memcpy self.memcpy_func.argtypes = (c_void_p, c_void_p, c_size_t) self.memcpy_func.restype = LPVOID self.rtl_copy_memory_func = self.kernel32_dll.RtlCopyMemory self.rtl_copy_memory_func.argtypes = (LPVOID, LPCVOID, c_ulonglong) self.unmap_view_of_file_func = self.kernel32_dll.UnmapViewOfFile self.unmap_view_of_file_func.argtypes = (LPCVOID, ) self.unmap_view_of_file_func.restype = BOOL self.close_handle_func = self.kernel32_dll.CloseHandle self.close_handle_func.argtypes = (HANDLE, ) self.close_handle_func.restype = BOOL self.get_last_error_func = self.kernel32_dll.GetLastError self.getch_func = self.msvcrt_dll._getch ''' C타입 함수 포인터들의 선언 ''' self.file_mapping_name_ptr = c_wchar_p(nameOfMmf) self.mapping_handle = self.create_file_mapping_func(self.INVALID_HANDLE_VALUE, 0, self.PAGE_READWRITE, 0, self.SHMEMSIZE, self.file_mapping_name_ptr) print("Mapping object handle: 0x{:016X}".format(self.mapping_handle)) if not self.mapping_handle: print("Could not open file mapping object: {:d}".format(self.get_last_error_func())) return 1 raise WinError() def update(self, data) : if(type(data) != type(b"")): print("data의 타입이 바이트 타입이 아닙니다.") return 1 self.data = bytes(data) self.msg_ptr = c_char_p(self.data) self.mapped_view_ptr = self.map_view_of_file_func(self.mapping_handle, self.FILE_MAP_ALL_ACCESS, 0, 0, self.SHMEMSIZE) print("Mapped view addr: 0x{:016X}".format(self.mapped_view_ptr)) if not self.mapped_view_ptr: print("Could not map view of file: {:d}".format(self.get_last_error_func())) self.close_handle_func(self.mapping_handle) return 1 raise WinError() print("Message length: {:d} chars ({:d} bytes)".format(len(self.data), len(self.data))) self.memcpy_func(self.mapped_view_ptr, self.msg_ptr, len(self.data)) def read(self) : self.read_handle = self.open_file_mapping_func(self.FILE_MAP_READ, 0, self.file_mapping_name_ptr) if not self.read_handle: print("Could not open file mapping object: {:d}".format(self.get_last_error_func())) return 1 raise WinError() self.readMapped_view_ptr = self.map_view_of_file_func(self.read_handle, self.FILE_MAP_READ, 0, 0, self.SHMEMSIZE) if not self.readMapped_view_ptr: print("Could not map view of file: {:d}".format(self.get_last_error_func())) self.close_handle_func(self.mapping_handle) return 1 raise WinError() DATA = c_char_p(self.readMapped_view_ptr) DATA = string_at(DATA, size=255) # 포인터에서 널문자를 무시하고 일정 크기를 바이트배열로 반환 stringToReturn = header.s_yHdr(DATA) return stringToReturn def close(self) : #if self.mapped_view_ptr in locals(): if hasattr(mmf, 'mapped_view_ptr'): self.unmap_view_of_file_func(self.mapped_view_ptr) print("mapped view ptr has been freed"); if hasattr(mmf, 'readMapped_view_ptr'): self.unmap_view_of_file_func(self.readMapped_view_ptr) print("readMapped_view_ptr has been freed"); self.close_handle_func(self.mapping_handle) if __name__ == "__main__": print("Python {:s} on {:s}".format(sys.version, sys.platform)) #rtl_copy_memory_func(mapped_view_ptr, msg_ptr, byte_len) print("Hit a key to read...") input() mmf = Mmf("123123") #mmf.update( header.yHdr_s("hello C++") ) print(mmf.read()); print("Hit a key to exit...") input() mmf.close() | cs |
ctypes 에서 c_char_p로 데이터를 수신해서 출력하면 null terminator 문자로 인식해서 바이트 배열로 만들때 null 전까지만 바이트 배열로 생성됩니다.
string_at 을 사용하면 null을 무시한 특정 크기의 바이트 배열을 미리 할당할 수 있습니다.
C++ MMF writer 부분
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include "pch.h" #include <iostream> #include <3vmw9.hpp> using namespace std; using namespace rv2; b::t writer() { hnd::t hMemoryMap = (hnd::t)0xFFFFFFFF; y::p pMemoryMap = NULL; s::t data = "hello python..!"; y::p yhdr = nil; z::t size = yHdr_s(yhdr, data); hMemoryMap = CreateFileMapping(hMemoryMap, NULL, PAGE_READWRITE, 0, size, L"123123"); if (!hMemoryMap) { MessageBox(NULL, L"failed createFileMaping", L"error", MB_OK); return FALSE; } pMemoryMap = (y::p)MapViewOfFile(hMemoryMap, FILE_MAP_ALL_ACCESS, 0, 0, 0x100); if (!pMemoryMap) { CloseHandle(hMemoryMap); MessageBox(NULL, L"failed mapViewofFile", L"error", MB_OK); return FALSE; } cout << size << endl; co_s(s_yHdr(yhdr)); for (int i = 0; i < 47; i++) { wcout << yhdr[i] << endl; } memcpy(pMemoryMap, yhdr, size); while (true) if (_kbhit()) break; if (pMemoryMap) UnmapViewOfFile(pMemoryMap); if (hMemoryMap) CloseHandle(hMemoryMap); return true; } int main() { writer(); return 0; } | cs |
'IPC(Pipe, Shared Mem)' 카테고리의 다른 글
python <-> c++ mmf 최종테스트 데모 예제 (0) | 2019.02.12 |
---|---|
c++ <-> python MMF 최종 테스트 코드 예제 (2) | 2019.02.07 |
파이썬 MMF read 예제 (0) | 2019.02.01 |
파이썬 MMF write 예제 (0) | 2019.02.01 |
ctypes mmf 예제 (0) | 2019.02.01 |