First publish.
Write from C# named pipe server is working without using 100% CPU.
1 // SoundGraphAccess.cpp : Defines the entry point for the application.
5 #include "SoundGraphAccess.h"
6 #include "iMONDisplayAPI.h"
8 #define WM_DSP_PLUGIN_NOTIFY WM_APP + 1121
9 #define MAX_LOADSTRING 100
13 HINSTANCE hInst = 0; // current instance
14 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
15 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
16 HANDLE gThreadReceiver = 0;
17 HANDLE gThreadSender = 0;
20 LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender");
21 LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver");
23 HANDLE gPipeReceiver=0;
25 char gBufferReceiver[256];
26 char gBufferSender[256];
33 // Forward declarations of functions included in this code module:
34 ATOM MyRegisterClass(HINSTANCE hInstance);
35 BOOL InitInstance(HINSTANCE, int);
36 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
37 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
43 Read our pipe from this thread
45 DWORD WINAPI ThreadReceiver( LPVOID lpParam )
47 //Create our pipe and listen
48 gPipeReceiver=CreateFile(gPipeNameReceiver, GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
53 BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL);
56 gBufferReceiver[cbRead]='\0';
57 OutputDebugStringA(gBufferReceiver);
59 if (!success && GetLastError() != ERROR_MORE_DATA)
61 OutputDebugStringA("Can't Read\n");
69 CloseHandle(gPipeReceiver);
78 Write to our pipe from this thread
80 DWORD WINAPI ThreadSender( LPVOID lpParam )
82 gPipeSender=CreateFile(gPipeNameSender, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
87 CloseHandle(gPipeSender);
97 int APIENTRY _tWinMain(HINSTANCE hInstance,
98 HINSTANCE hPrevInstance,
102 UNREFERENCED_PARAMETER(hPrevInstance);
103 UNREFERENCED_PARAMETER(lpCmdLine);
105 // TODO: Place code here.
109 // Initialize global strings
110 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
111 LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
112 MyRegisterClass(hInstance);
114 // Perform application initialization:
115 if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
122 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
124 // Main message loop:
125 while (GetMessage(&msg, NULL, 0, 0))
127 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
129 TranslateMessage(&msg);
130 DispatchMessage(&msg);
134 return (int) msg.wParam;
139 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
141 LPCTSTR strErrMsg = _T("");
147 case DSPN_ERR_IN_USED: strErrMsg = _T("Display Plug-in is Already Used by Other Application."); break;
148 case DSPN_ERR_HW_DISCONNECTED: strErrMsg = _T("iMON HW is Not Connected."); break;
149 case DSPN_ERR_NOT_SUPPORTED_HW: strErrMsg = _T("The Connected iMON HW doesn't Support Display Plug-in."); break;
150 case DSPN_ERR_PLUGIN_DISABLED: strErrMsg = _T("Display Plug-in Mode Option is Disabled."); break;
151 case DSPN_ERR_IMON_NO_REPLY: strErrMsg = _T("The Latest iMON is Not Installed or iMON Not Running."); break;
152 case DSPN_ERR_UNKNOWN: strErrMsg = _T("Unknown Failure."); break;
159 case DSPNM_PLUGIN_SUCCEED: strErrMsg = _T("Plug-in Mode Inited Successfully."); break;
160 case DSPNM_IMON_RESTARTED: strErrMsg = _T("iMON Started and Plug-in Mode Inited."); break;
161 case DSPNM_HW_CONNECTED: strErrMsg = _T("iMON HW Connected and Plug-in Mode Inited."); break;
165 OutputDebugString((LPCTSTR)strErrMsg);
171 // FUNCTION: MyRegisterClass()
173 // PURPOSE: Registers the window class.
177 // This function and its usage are only necessary if you want this code
178 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
179 // function that was added to Windows 95. It is important to call this function
180 // so that the application will get 'well formed' small icons associated
183 ATOM MyRegisterClass(HINSTANCE hInstance)
187 wcex.cbSize = sizeof(WNDCLASSEX);
189 wcex.style = CS_HREDRAW | CS_VREDRAW;
190 wcex.lpfnWndProc = WndProc;
193 wcex.hInstance = hInstance;
194 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SOUNDGRAPHACCESS));
195 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
196 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
197 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS);
198 wcex.lpszClassName = szWindowClass;
199 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
201 return RegisterClassEx(&wcex);
205 // FUNCTION: InitInstance(HINSTANCE, int)
207 // PURPOSE: Saves instance handle and creates main window
211 // In this function, we save the instance handle in a global variable and
212 // create and display the main program window.
214 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
218 hInst = hInstance; // Store instance handle in our global variable
220 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
221 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
228 ShowWindow(hWnd, nCmdShow);
235 // FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
237 // PURPOSE: Processes messages for the main window.
239 // WM_COMMAND - process the application menu
240 // WM_PAINT - Paint the main window
241 // WM_DESTROY - post a quit message and return
244 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
253 //IMON_Display_Uninit();
254 //IMON_Display_Init(hWnd, WM_DSP_PLUGIN_NOTIFY);
255 gThreadReceiver = CreateThread( NULL, 0, ThreadReceiver, NULL/*data pointer*/, 0, NULL);
256 gThreadSender = CreateThread( NULL, 0, ThreadSender, NULL/*data pointer*/, 0, NULL);
259 wmId = LOWORD(wParam);
260 wmEvent = HIWORD(wParam);
261 // Parse the menu selections:
265 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
271 return DefWindowProc(hWnd, message, wParam, lParam);
275 hdc = BeginPaint(hWnd, &ps);
276 // TODO: Add any drawing code here...
281 //To complete write op
284 CloseHandle(gPipeSender);
287 //To complete read op
288 if (gPipeReceiver!=0)
290 CloseHandle(gPipeReceiver);
293 WaitForSingleObject(gThreadSender,INFINITE);
294 WaitForSingleObject(gThreadReceiver,INFINITE);
295 CloseHandle(gThreadSender);
296 CloseHandle(gThreadReceiver);
297 //IMON_Display_Uninit();
300 case WM_DSP_PLUGIN_NOTIFY:
303 case DSPNM_PLUGIN_SUCCEED:
304 case DSPNM_IMON_RESTARTED:
305 case DSPNM_HW_CONNECTED:
307 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
308 m_bVfdConnected = FALSE;
309 m_bLcdConnected = FALSE;
310 if((lParam & DSPN_DSP_VFD) == DSPN_DSP_VFD) m_bVfdConnected = TRUE;
311 if((lParam & DSPN_DSP_LCD) == DSPN_DSP_LCD) m_bLcdConnected = TRUE;
314 DisplayPluginMessage(wParam, FALSE);
318 case DSPNM_PLUGIN_FAILED:
319 case DSPNM_HW_DISCONNECTED:
320 case DSPNM_IMON_CLOSED:
322 //GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
323 m_bVfdConnected = FALSE;
324 m_bLcdConnected = FALSE;
327 DisplayPluginMessage(lParam, TRUE);
331 case DSPNM_LCD_TEXT_SCROLL_DONE:
333 //TRACE(_T("LCD Text Scroll Finished.\n"));
340 return DefWindowProc(hWnd, message, wParam, lParam);
345 // Message handler for about box.
346 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
348 UNREFERENCED_PARAMETER(lParam);
352 return (INT_PTR)TRUE;
355 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
357 EndDialog(hDlg, LOWORD(wParam));
358 return (INT_PTR)TRUE;
362 return (INT_PTR)FALSE;