SoundGraphAccess.cpp
author sl
Thu, 07 Feb 2013 01:10:03 +0100
changeset 0 a77691c40066
child 2 ebd2fb40b033
permissions -rw-r--r--
First publish.
Write from C# named pipe server is working without using 100% CPU.
     1 // SoundGraphAccess.cpp : Defines the entry point for the application.
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "SoundGraphAccess.h"
     6 #include "iMONDisplayAPI.h"
     7 
     8 #define WM_DSP_PLUGIN_NOTIFY	WM_APP + 1121
     9 #define MAX_LOADSTRING 100
    10 
    11 
    12 // Global Variables:
    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;
    18 BOOL gQuit=FALSE;
    19 
    20 LPTSTR gPipeNameSender = TEXT("\\\\.\\pipe\\sga-sender"); 
    21 LPTSTR gPipeNameReceiver = TEXT("\\\\.\\pipe\\sga-receiver"); 
    22 HANDLE gPipeSender=0;
    23 HANDLE gPipeReceiver=0;
    24 
    25 char gBufferReceiver[256];
    26 char gBufferSender[256];
    27 
    28 
    29 //
    30 BOOL m_bVfdConnected;
    31 BOOL m_bLcdConnected;
    32 
    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);
    38 
    39 
    40 
    41 
    42 /**
    43 Read our pipe from this thread
    44 */
    45 DWORD WINAPI ThreadReceiver( LPVOID lpParam )
    46 	{
    47 	//Create our pipe and listen
    48 	gPipeReceiver=CreateFile(gPipeNameReceiver,	GENERIC_READ ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    49 
    50 	while(!gQuit)
    51 		{
    52 		DWORD cbRead;
    53 		BOOL success=ReadFile(gPipeReceiver,gBufferReceiver,sizeof(gBufferReceiver),&cbRead, NULL); 
    54 		if(success)
    55 			{			
    56 			gBufferReceiver[cbRead]='\0';
    57 			OutputDebugStringA(gBufferReceiver);
    58 			}
    59 		if (!success && GetLastError() != ERROR_MORE_DATA) 
    60 			{
    61 			OutputDebugStringA("Can't Read\n");
    62 			}
    63 		//
    64 		Sleep(500);
    65 		}
    66 
    67 	if (gPipeReceiver!=0)
    68 		{
    69 		CloseHandle(gPipeReceiver);	
    70 		gPipeReceiver=0;
    71 		}
    72 	
    73 	return 0;
    74 	}
    75 
    76 
    77 /**
    78 Write to our pipe from this thread
    79 */
    80 DWORD WINAPI ThreadSender( LPVOID lpParam )
    81 	{
    82 	gPipeSender=CreateFile(gPipeNameSender,	GENERIC_WRITE ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
    83 	
    84 
    85 	if (gPipeSender!=0)
    86 		{
    87 		CloseHandle(gPipeSender);
    88 		gPipeSender=0;
    89 		}
    90 
    91 	return 0;
    92 	}
    93 
    94 
    95 /**
    96 */
    97 int APIENTRY _tWinMain(HINSTANCE hInstance,
    98                      HINSTANCE hPrevInstance,
    99                      LPTSTR    lpCmdLine,
   100                      int       nCmdShow)
   101 {
   102 	UNREFERENCED_PARAMETER(hPrevInstance);
   103 	UNREFERENCED_PARAMETER(lpCmdLine);
   104 
   105 	// TODO: Place code here.
   106 	MSG msg;
   107 	HACCEL hAccelTable;
   108 
   109 	// Initialize global strings
   110 	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
   111 	LoadString(hInstance, IDC_SOUNDGRAPHACCESS, szWindowClass, MAX_LOADSTRING);
   112 	MyRegisterClass(hInstance);
   113 
   114 	// Perform application initialization:
   115 	if (!InitInstance (hInstance, /*SW_HIDE*/ nCmdShow))
   116 		{
   117 		return FALSE;
   118 		}
   119 
   120 
   121 
   122 	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SOUNDGRAPHACCESS));
   123 
   124 	// Main message loop:
   125 	while (GetMessage(&msg, NULL, 0, 0))
   126 		{
   127 		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
   128 			{
   129 			TranslateMessage(&msg);
   130 			DispatchMessage(&msg);
   131 			}
   132 		}
   133 
   134 	return (int) msg.wParam;
   135 
   136 	}
   137 
   138 
   139 void DisplayPluginMessage(UINT uErrCode, BOOL bError)
   140 	{
   141 	LPCTSTR strErrMsg = _T("");
   142 
   143 	if(bError)
   144 		{
   145 		switch(uErrCode)
   146 			{
   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;
   153 			}
   154 		}
   155 	else
   156 		{
   157 		switch(uErrCode)
   158 			{
   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;
   162 			}
   163 		}
   164 	//
   165 	OutputDebugString((LPCTSTR)strErrMsg);
   166 	}
   167 
   168 
   169 
   170 //
   171 //  FUNCTION: MyRegisterClass()
   172 //
   173 //  PURPOSE: Registers the window class.
   174 //
   175 //  COMMENTS:
   176 //
   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
   181 //    with it.
   182 //
   183 ATOM MyRegisterClass(HINSTANCE hInstance)
   184 {
   185 	WNDCLASSEX wcex;
   186 
   187 	wcex.cbSize = sizeof(WNDCLASSEX);
   188 
   189 	wcex.style			= CS_HREDRAW | CS_VREDRAW;
   190 	wcex.lpfnWndProc	= WndProc;
   191 	wcex.cbClsExtra		= 0;
   192 	wcex.cbWndExtra		= 0;
   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));
   200 
   201 	return RegisterClassEx(&wcex);
   202 }
   203 
   204 //
   205 //   FUNCTION: InitInstance(HINSTANCE, int)
   206 //
   207 //   PURPOSE: Saves instance handle and creates main window
   208 //
   209 //   COMMENTS:
   210 //
   211 //        In this function, we save the instance handle in a global variable and
   212 //        create and display the main program window.
   213 //
   214 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
   215 {
   216    HWND hWnd;
   217 
   218    hInst = hInstance; // Store instance handle in our global variable
   219 
   220    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
   221       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   222 
   223    if (!hWnd)
   224    {
   225       return FALSE;
   226    }
   227 
   228    ShowWindow(hWnd, nCmdShow);
   229    UpdateWindow(hWnd);
   230 
   231    return TRUE;
   232 }
   233 
   234 //
   235 //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
   236 //
   237 //  PURPOSE:  Processes messages for the main window.
   238 //
   239 //  WM_COMMAND	- process the application menu
   240 //  WM_PAINT	- Paint the main window
   241 //  WM_DESTROY	- post a quit message and return
   242 //
   243 //
   244 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
   245 {
   246 	int wmId, wmEvent;
   247 	PAINTSTRUCT ps;
   248 	HDC hdc;
   249 
   250 	switch (message)
   251 	{
   252 	case WM_CREATE:
   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);
   257 		break;
   258 	case WM_COMMAND:
   259 		wmId    = LOWORD(wParam);
   260 		wmEvent = HIWORD(wParam);
   261 		// Parse the menu selections:
   262 		switch (wmId)
   263 		{
   264 		case IDM_ABOUT:
   265 			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
   266 			break;
   267 		case IDM_EXIT:
   268 			DestroyWindow(hWnd);
   269 			break;
   270 		default:
   271 			return DefWindowProc(hWnd, message, wParam, lParam);
   272 		}
   273 		break;
   274 	case WM_PAINT:
   275 		hdc = BeginPaint(hWnd, &ps);
   276 		// TODO: Add any drawing code here...
   277 		EndPaint(hWnd, &ps);
   278 		break;
   279 	case WM_DESTROY:
   280 		gQuit=TRUE;
   281 		//To complete write op
   282 		if (gPipeSender!=0)
   283 			{
   284 			CloseHandle(gPipeSender);
   285 			gPipeSender=0;
   286 			}
   287 		//To complete read op
   288 		if (gPipeReceiver!=0)
   289 			{
   290 			CloseHandle(gPipeReceiver);
   291 			gPipeReceiver=0;
   292 			}
   293 		WaitForSingleObject(gThreadSender,INFINITE);
   294 		WaitForSingleObject(gThreadReceiver,INFINITE);
   295 		CloseHandle(gThreadSender);
   296 		CloseHandle(gThreadReceiver);
   297 		//IMON_Display_Uninit();
   298 		PostQuitMessage(0);
   299 		break;
   300 	case WM_DSP_PLUGIN_NOTIFY:
   301 		switch(wParam)
   302 			{
   303 			case DSPNM_PLUGIN_SUCCEED:
   304 			case DSPNM_IMON_RESTARTED:
   305 			case DSPNM_HW_CONNECTED:
   306 				{
   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;
   312 				//UpdateControlUI();
   313 
   314 				DisplayPluginMessage(wParam, FALSE);
   315 				}
   316 				break;
   317 
   318 			case DSPNM_PLUGIN_FAILED:
   319 			case DSPNM_HW_DISCONNECTED:
   320 			case DSPNM_IMON_CLOSED:
   321 				{
   322 				//GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
   323 				m_bVfdConnected = FALSE;
   324 				m_bLcdConnected = FALSE;
   325 				//UpdateControlUI();
   326 
   327 				DisplayPluginMessage(lParam, TRUE);
   328 				}
   329 				break;
   330 
   331 			case DSPNM_LCD_TEXT_SCROLL_DONE:
   332 				{
   333 				//TRACE(_T("LCD Text Scroll Finished.\n"));
   334 				}
   335 				break;
   336 			}
   337 		return 0;
   338 		break;
   339 	default:
   340 		return DefWindowProc(hWnd, message, wParam, lParam);
   341 	}
   342 	return 0;
   343 }
   344 
   345 // Message handler for about box.
   346 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
   347 {
   348 	UNREFERENCED_PARAMETER(lParam);
   349 	switch (message)
   350 	{
   351 	case WM_INITDIALOG:
   352 		return (INT_PTR)TRUE;
   353 
   354 	case WM_COMMAND:
   355 		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
   356 		{
   357 			EndDialog(hDlg, LOWORD(wParam));
   358 			return (INT_PTR)TRUE;
   359 		}
   360 		break;
   361 	}
   362 	return (INT_PTR)FALSE;
   363 }
   364