c++
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 | #include <3vmw9.hpp> #include "pch.h" #include <locale.h> using namespace std; using namespace rv2; head::U headEnc_ws(wstring _ws) {/// head::u head_; head_.v0 = (y::T)1; head_.v1 = (y::T)0; head_.c0 = (y::T)3; head_.c1 = (y::T)0; head_.t = (n::T)time(0); head_.r = (i::T)0; head_.y0 = byt::pchr; head_.y1 = (y::T)0; head_.n = (y::T)0; head_.d = (y::T)w::Z; head_.h = (h::T)1; head_.w = (h::T)_ws.length(); head_.l = head_.h * head_.w * head_.d; head_.z = (i::T)(head_.l + head::Z);//yHd::Z; return head_; }/// z::t y_wP(y::p& y_, w::P& _wP, z::R _z) { ; y_ = new y::t[_z * 2]; memcpy(y_, _wP, _z * 2); return _z * 2; } vo::t y_ws(y::p& y_, wstring _s) { ; y_wP(y_, _s.c_str(), _s.length()); } vo::t wp_y(w::p& wp_, y::P& _y, z::R _z) { ; wp_ = new w::t[_z/2]; wp_[_z/2] = L'\0'; memcpy(wp_, _y, _z); } wstring ws_y(y::P& _y, z::R _z) { ; w::p wp_; wp_y(wp_, _y, _z); return wstring(wp_); } wstring ws_headRaw(y::P& _yRaw, head::R _head) { ; UNUSED(_head); return ws_y(_yRaw, _head.l); } head::U headRaw_ws(y::p& yRaw_, wstring _ws) { ; head::U head_(headEnc_ws(_ws)); y_ws(yRaw_, _ws); return head_; } i::T yHdr_ws(y::p& yHdr_, wstring _ws) { ; y::p yRaw(nil); head::U head(headRaw_ws(yRaw, _ws)); yHdr_headRaw(yHdr_, yRaw, head); return head.z; } wstring ws_yHdr(y::P& _yHdr) { ; y::p yRaw(nullptr); head::U head(headRaw_yHdr(yRaw, _yHdr)); return ws_headRaw(yRaw, head); } wstring ws_mmf(hnd::R _hMmf) { ; wstring ws_; if (_hMmf != hnd::T0) { y::p yRaw(nil); head::U head(headRaw_mmf(yRaw, _hMmf)); ws_ = ws_headRaw(yRaw, head); delete yRaw; } return ws_; } //read b::T mmf_ws(hnd::r hMmf_, wstring _ws) { ; y::p yHdr(nil); z::T z(yHdr_ws(yHdr, _ws)); b::T b_(mmf_y(hMmf_, yHdr, z)); delete yHdr; return b_; } //writ void read_s() { mmf::reader::l mmfReader("mmftest_pchr"); co_s(mmfReader.read_s()); } void read_ws() { wcout << ws_mmf(open_mmf("mmftest_pchr")) << endl; } void write_s() { mmf::writer::l mmfWriter("mmftest_pchr", "unused.."); mmfWriter.writ_s("hello from c++...!"); } void write_ws() { hnd::t wsMmf = create_mmf("mmftest_pchr", 1024); mmf_ws(wsMmf, L"안녕하세요 from c++..!"); } int main() { _wsetlocale(LC_ALL, L"korean"); //지역화 설정을 전역적으로 적용 //wcout.imbue(locale("korean")); //출력시 부분적 적용 //wcin.imbue(locale("korean")); //입력시 부분적 적용 s::v vsMenu = { "mmf 바이트 스트링 쓰기","mmf 바이트 스트링 읽기","mmf 유니코드 스트링 쓰기","mmf 유니코드 스트링 읽기" }; vpv::v vpvMenu = { &write_s, &read_s, &write_ws, &read_ws }; rv2::menu::l lMenu(vsMenu, vpvMenu); while (1) if (_kbhit()) break; } | cs |
python 구현 파일
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | 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 update_ws(self, data) : 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_s(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 read_ws(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.ws_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) def print_menu(): print("1. mmf 바이트 스트링 쓰기 ") print("2. mmf 바이트 스트링 읽기 ") print("3. mmf 유니코드 스트링 쓰기 ") print("4. mmf 유니코드 스트링 읽기 ") print("5. 종 료") menu = input("메뉴선택: ") return int(menu) if __name__ == "__main__": print("Python {:s} on {:s}".format(sys.version, sys.platform)) mmf = Mmf("mmftest_pchr") while(1): menu = print_menu() if menu == 5: mmf.close() break; elif menu == 4: print("mmf로 부터의 메세지 :", end='') print(mmf.read_ws()); elif menu == 3: mmf.update( header.yHdr_ws("안녕하세요 파이썬으로 부터의 메세지 입니다.") ) elif menu == 2: print("mmf로 부터의 메세지 :", end='') print(mmf.read_s()); elif menu == 1: mmf.update( header.yHdr_s("hello C++ from python..!")) | cs |
파이썬 헤더 파일
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 180 181 182 183 184 185 186 187 | # -*- coding: utf-8 -*- import ctypes class Head(ctypes.Structure): _fields_ = [("z", ctypes.c_int), ("v0", ctypes.c_ubyte),("v1", ctypes.c_ubyte),("c0", ctypes.c_ubyte),("c1", ctypes.c_ubyte), ("t", ctypes.c_ulonglong), ("r",ctypes.c_int), ("y0",ctypes.c_ubyte), ("y1",ctypes.c_ubyte),("n",ctypes.c_ubyte),("d",ctypes.c_ubyte), ("h",ctypes.c_ushort),("w",ctypes.c_ushort), ("l",ctypes.c_int)] class Y2(ctypes.Union): _fields_= [("h1", ctypes.c_ushort), ("t2", ctypes.c_ubyte * 2)] class Y4(ctypes.Union): _fields_= [("i1", ctypes.c_int), ("f1", ctypes.c_float), ("t4", ctypes.c_ubyte * 4)] class Y8(ctypes.Union): _fields_= [("n1", ctypes.c_ulonglong), ("d1", ctypes.c_double), ("t8", ctypes.c_ubyte * 8)] class Yhd(ctypes.Structure): _fields_ = [("yHd",ctypes.c_ubyte * 32)] def int_to_bytes1(x): return x.to_bytes(1, 'big') def int_to_bytes2(x): return x.to_bytes(2, 'big') def int_to_bytes4(x): return x.to_bytes(4, 'big') def int_to_bytes8(x): return x.to_bytes(8, 'big') def int_from_bytes(xbytes): return int.from_bytes(xbytes, 'big') def yHd_head(head): y2 = Y2() ; y4 = Y4() ; y8 = Y8() ; yHd = Yhd() ; y42 = Y4() ; y4.i1 = head.z yHd.yHd[0] = y4.t4[0] ; yHd.yHd[1] = y4.t4[1] ; yHd.yHd[2] = y4.t4[2] ; yHd.yHd[3] = y4.t4[3] yHd.yHd[4] = head.v0 ; yHd.yHd[5] = head.v1 ; yHd.yHd[6] = head.c0 ; yHd.yHd[7] = head.c1 y8.n1 = head.t yHd.yHd[8] = y8.t8[0] ; yHd.yHd[9] = y8.t8[1] ; yHd.yHd[10] = y8.t8[2] ; yHd.yHd[11] = y8.t8[3] ; yHd.yHd[12] = y8.t8[4] ; yHd.yHd[13] = y8.t8[5] ; yHd.yHd[14] = y8.t8[6] ; yHd.yHd[15] = y8.t8[7] ; y4.i1 = head.r yHd.yHd[16] = y4.t4[0] ; yHd.yHd[17] = y4.t4[1] ; yHd.yHd[18] = y4.t4[2] ; yHd.yHd[19] = y4.t4[3] ; yHd.yHd[20] = head.y0 ; yHd.yHd[21] = head.y1 ; yHd.yHd[22] = head.n ; yHd.yHd[23] = head.d ; y2.h1 = head.h yHd.yHd[24] = y2.t2[0] ; yHd.yHd[25] = y2.t2[1] y2.h1 = 0 y2.h1 = head.w yHd.yHd[26] = y2.t2[0] ; yHd.yHd[27] = y2.t2[1] y42.i1 = head.l yHd.yHd[28] = y42.t4[0] ; yHd.yHd[29] = y42.t4[1] ; yHd.yHd[30] = y42.t4[2] ; yHd.yHd[31] = y42.t4[3] return yHd def head_yHd(yHd): head = Head() ; y2 = Y2() ; y4 = Y4() ; y8 = Y8() ; y42 = Y4() ; y4.t4[0] = yHd.yHd[0] ; y4.t4[1] = yHd.yHd[1] ; y4.t4[2] = yHd.yHd[2] ; y4.t4[3] = yHd.yHd[3] ; head.z = y4.i1 head.v0 = yHd.yHd[4] ; head.v1 = yHd.yHd[5] ; head.c0 = yHd.yHd[6] ; head.c1 = yHd.yHd[7] ; y8.t8[0] = yHd.yHd[8] ; y8.t8[1] = yHd.yHd[9] ; y8.t8[2] = yHd.yHd[10] ; y8.t8[3] = yHd.yHd[11] ; y8.t8[4] = yHd.yHd[12] ; y8.t8[5] = yHd.yHd[13] ; y8.t8[6] = yHd.yHd[14] ; y8.t8[7] = yHd.yHd[15] ; head.t = y8.n1 y4.t4[0] = yHd.yHd[16] ; y4.t4[1] = yHd.yHd[17] ; y4.t4[2] = yHd.yHd[18] ; y4.t4[3] = yHd.yHd[19] ; head.r = y4.i1 head.y0 = yHd.yHd[20] ; head.y1 = yHd.yHd[21] ; head.n = yHd.yHd[22] ; head.d = yHd.yHd[23] ; y2.t2[0] = yHd.yHd[24] ; y2.t2[1] = yHd.yHd[25] ; head.h = y2.h1 y2.t2[0] = yHd.yHd[26] ; y2.t2[1] = yHd.yHd[27] ; head.w = y2.h1 y42.t4[0] = yHd.yHd[28] ; y42.t4[1] = yHd.yHd[29] ; y42.t4[2] = yHd.yHd[30] ; y42.t4[3] = yHd.yHd[31] ; head.l = y42.i1 return head def headEnc_s(string): head = Head() head.v0 = ctypes.c_ubyte(1) ; head.v1 = ctypes.c_ubyte(0) ; head.c0 = ctypes.c_ubyte(3) ; head.c1 = ctypes.c_ubyte(0) head.t = ctypes.c_ulonglong(0) ; head.r = ctypes.c_int(0) ; head.y0 = ctypes.c_ubyte(1) ; head.y1 = ctypes.c_ubyte(0) ; head.n = ctypes.c_ubyte(0) ; head.d = ctypes.c_ubyte(1) ; head.h = ctypes.c_ushort(1) ; head.w = ctypes.c_ushort(len(string)) ; head.l = ctypes.c_int(head.h * head.w * head.d) head.z = ctypes.c_int(head.l + 32) return head def headEnc_ws(string): head = Head() head.v0 = ctypes.c_ubyte(1) ; head.v1 = ctypes.c_ubyte(0) ; head.c0 = ctypes.c_ubyte(3) ; head.c1 = ctypes.c_ubyte(0) head.t = ctypes.c_ulonglong(0) ; head.r = ctypes.c_int(0) ; head.y0 = ctypes.c_ubyte(1) ; head.y1 = ctypes.c_ubyte(0) ; head.n = ctypes.c_ubyte(0) ; head.d = ctypes.c_ubyte(2) ; head.h = ctypes.c_ushort(1) ; head.w = ctypes.c_ushort(len(string)) ; head.l = ctypes.c_int(head.h * head.w * head.d) head.z = ctypes.c_int(head.l + 32) return head def yHdr_s(string): head = headEnc_s(string) yHdr_To_send = bytes(head) + string.encode() return yHdr_To_send def yHdr_ws(string): head = headEnc_ws(string) yHdr_To_send = bytes(head) + string.encode("utf-16le") ''' #디버그 코드 print(bytes(head)) print(string.encode("utf-16le")) for i in range(32 , 34): print(yHdr_To_send[i]) ''' #print(bytes(head)) return yHdr_To_send def s_yHdr(yhdr): y4 = Y4() y4.t4[0] = ctypes.c_ubyte(yhdr[0]) y4.t4[1] = ctypes.c_ubyte(yhdr[1]) y4.t4[2] = ctypes.c_ubyte(yhdr[2]) y4.t4[3] = ctypes.c_ubyte(yhdr[3]) sizeOfData = y4.i1 print(f"sizeOfData = {sizeOfData}") rawData = yhdr[32:sizeOfData] return rawData def ws_yHdr(yhdr): y4 = Y4() y4.t4[0] = ctypes.c_ubyte(yhdr[0]) y4.t4[1] = ctypes.c_ubyte(yhdr[1]) y4.t4[2] = ctypes.c_ubyte(yhdr[2]) y4.t4[3] = ctypes.c_ubyte(yhdr[3]) sizeOfData = y4.i1 print(f"sizeOfData = {sizeOfData}") rawData = yhdr[32:sizeOfData] return rawData.decode("utf-16le") | cs |
'IPC(Pipe, Shared Mem)' 카테고리의 다른 글
윈도우즈 메세지로 COPY_DATA_STRUCT 송신부 수신부 (0) | 2019.02.18 |
---|---|
Python <-> c++ PIPE, MMF 테스트 예제 최종(코드업로드) (0) | 2019.02.12 |
c++ <-> python MMF 최종 테스트 코드 예제 (2) | 2019.02.07 |
ctypes mmf 널문자 무시 바이트데이터 수신 예제 (0) | 2019.02.07 |
파이썬 MMF read 예제 (0) | 2019.02.01 |