⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phoneui.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
📖 第 1 页 / 共 3 页
字号:
	}

	DWORD dw = FALSE;
	dwSize = sizeof(dw);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"authenticate", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		uiportflags |= 	RFCOMM_PORT_FLAGS_AUTHENTICATE;

	dw = FALSE;
	dwSize = sizeof(dw);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"encrypt", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		uiportflags |= 	RFCOMM_PORT_FLAGS_ENCRYPT;

	dw = FALSE;
	dwSize = sizeof(dw);

	int fLan = FALSE;

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"lan", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
		dw = FALSE;

	if (dw)
		fLan = TRUE;

	RegCloseKey (hk);

	if ((c <= 0) || (c > 31)) {
		MessageBox (NULL, L"Bad channel in phone UI registry!", L"PhoneUI Error", MB_OK | MB_TOPMOST);
		return;
	}
	
	if (! GetBA (szString, &b)) {
		MessageBox (NULL, L"Bad address in phone UI registry!", L"PhoneUI Error", MB_OK | MB_TOPMOST);
		return;
	}

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, fLan ? L"ExtModems\\bluetooth_dcn" : L"ExtModems\\bluetooth_dun", 0, KEY_READ, &hk)) {
		MessageBox (NULL, L"Bluetooth DUN modem is not defined (no registry entry)!", L"PhoneUI Error", MB_OK | MB_TOPMOST);

		return;
	}

	dwSize = sizeof(szString);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"port", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		MessageBox (NULL, L"Bluetooth DUN modem is not defined (wrong or nonexistent port)!", L"PhoneUI Error", MB_OK | MB_TOPMOST);
		RegCloseKey (hk);

		return;
	}

	RegCloseKey (hk);

	if ((toupper (szString[0]) != 'C') || (toupper (szString[1]) != 'O') ||
		(toupper (szString[2]) != 'M') || (szString[3] < '0') || (szString[3] > '9') ||
		(szString[4] != ':') || (szString[5] != '\0')) {
		MessageBox (NULL, L"Bluetooth DUN modem is not defined (bad port)!", L"PhoneUI Error", MB_OK | MB_TOPMOST);
		return;
	}

	DWORD ndx = szString[3] - '0';

	int imtu = 0;

	if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui", 0, KEY_READ, &hk)) {
		dw = 0;
		dwSize = sizeof(dw);

		if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"mtu", NULL, &dwType, (BYTE *)&dw, &dwSize)) ||
			(dwType != REG_DWORD) || (dwSize != sizeof(dw)) || (! dw))
			dw = 0;

		imtu = (int)dw;

		RegCloseKey (hk);
	}

	StopDevice ();

	PORTEMUPortParams pp;
	memset (&pp, 0, sizeof(pp));

	pp.device  = b;
	pp.channel = c;
	pp.imtu    = imtu;

	pp.uiportflags = uiportflags;

	g_pState->hDevice = RegisterDevice (L"COM", ndx, L"btd.dll", (DWORD)&pp);

	if (! g_pState->hDevice) {
		wsprintf (szString, L"Failed to register COM port, Error = %d", GetLastError ());

		MessageBox (NULL, szString, L"PhoneUI Error", MB_OK | MB_TOPMOST);
	}
}

static void StopSearch (void) {
	int fWaitForStop = FALSE;

	BthNsLookupServiceEnd ((HANDLE)BTHNS_ABORT_CURRENT_INQUIRY);

	g_pState->Lock ();

	HWND hWnd = g_pState->hWnd;

	if (g_pState->fState != DO_NOTHING) {
		g_pState->fState = DO_STOP;
		fWaitForStop = TRUE;
	}

	g_pState->Unlock ();

	if (fWaitForStop) {
		SetWindowText (hWnd, L"Stopping search...");

		for (int i = 0 ; i < 20 ; ++i) {
			if (g_pState->fState == DO_NOTHING)
				break;

			Sleep (1000);
		}

		SetWindowText (hWnd, L"Phone UI");
	}
}

static int OK (void) {
	g_pState->Lock ();

	HWND hWnd = g_pState->hWnd;
	HINSTANCE hInst = g_pState->hInst;

	g_pState->Unlock ();

	StopSearch ();

	HWND hWndDevList   = GetDlgItem (hWnd, IDC_DEVICELIST);

	int iNdx = SendMessage (hWndDevList, LB_GETCURSEL, 0, 0);
	if (iNdx < 0) {
		MessageBox (hWnd, L"Nothing selected", L"Error", MB_OK | MB_TOPMOST);
		return FALSE;
	}

	int iItemData = SendMessage (hWndDevList, LB_GETITEMDATA, (WPARAM)iNdx, (LPARAM)0);

	int fEncrypt = (SendMessage (GetDlgItem (hWnd, IDC_CRYPT), BM_GETCHECK, 0, 0) == BST_CHECKED);
	int fAuth = (SendMessage (GetDlgItem (hWnd, IDC_AUTH), BM_GETCHECK, 0, 0) == BST_CHECKED);
	int fLan = (SendMessage (GetDlgItem (hWnd, IDC_LAN), BM_GETCHECK, 0, 0) == BST_CHECKED);

	g_pState->Lock ();
	InquiryResult *pRes = g_pState->pDev;
	while (pRes && (pRes != (InquiryResult *)iItemData))
		pRes = pRes->pNext;

	if (! pRes) {
		g_pState->Unlock ();
		MessageBox (hWnd, L"Please requery. This record is obsolete.", L"Error", MB_OK | MB_TOPMOST);
		return FALSE;
	}


	BT_ADDR b = pRes->b;
	unsigned char c = pRes->channel;
	int fHaveSDP = pRes->fHaveSDP;

	g_pState->Unlock ();

	if (! fHaveSDP) {
		SVSUTIL_ASSERT (! c);
		c = DoSDP (&b, fLan);
	}

	if (! c)
		c = DialogBox (hInst, MAKEINTRESOURCE (IDD_ENTERCHANNEL), NULL, DlgProc2);

	if ((c <= 0) || (c >= 32))
		return FALSE;

	HKEY hk;
	DWORD dwDisp;
	if (ERROR_SUCCESS == RegCreateKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\phoneui\\device", 0, NULL, 0, KEY_WRITE, NULL, &hk, &dwDisp)) {
		WCHAR szAddress[13];
		DWORD dw = c;
		wsprintf (szAddress, L"%04x%08x", GET_NAP(b), GET_SAP(b));
		RegSetValueEx (hk, L"device", 0, REG_SZ, (BYTE *)szAddress, sizeof(szAddress));
		RegSetValueEx (hk, L"channel", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw));
		RegSetValueEx (hk, L"encrypt", 0, REG_DWORD, (BYTE *)&fEncrypt, sizeof(fEncrypt));
		RegSetValueEx (hk, L"authenticate", 0, REG_DWORD, (BYTE *)&fAuth, sizeof(fAuth));
		RegSetValueEx (hk, L"lan", 0, REG_DWORD, (BYTE *)&fLan, sizeof(fLan));
		RegCloseKey (hk);
	}

	CreateDevice ();

	return TRUE;
}

static void SetButtonStates (HWND hWnd) {
	EnableWindow (GetDlgItem (hWnd, IDC_DEREGISTER), IsRegistered ());
	SetWindowText (GetDlgItem (hWnd, IDC_STARTSTOP), g_pState->hDevice ? L"STOP" : L"START");
	int fAuth, fEncrypt, fLan;
	WCHAR szText[128];
	SetWindowText (GetDlgItem (hWnd, IDC_CURRENT), GetRegistration (szText, 128, &fAuth, &fEncrypt, &fLan) ? szText : L"ERROR");
	SendMessage (GetDlgItem (hWnd, IDC_AUTH), BM_SETCHECK, (WPARAM) (fAuth ? BST_CHECKED : BST_UNCHECKED), 0);
	SendMessage (GetDlgItem (hWnd, IDC_CRYPT), BM_SETCHECK, (WPARAM) (fEncrypt ? BST_CHECKED : BST_UNCHECKED), 0);
	SendMessage (GetDlgItem (hWnd, IDC_LAN), BM_SETCHECK, (WPARAM) (fLan ? BST_CHECKED : BST_UNCHECKED), 0);
}

static BOOL CALLBACK DlgProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		g_pState->hWnd = hWnd;
		SetButtonStates (hWnd);
		SetForegroundWindow (hWnd);
		SetFocus (GetDlgItem (hWnd, IDCANCEL));
		SendMessage(GetDlgItem(hWnd, IDC_DEVICELIST), LB_SETHORIZONTALEXTENT, 250, 0); 

		return 0;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				if (OK ())
					EndDialog (hWnd, TRUE);

				return 0;

			case IDCANCEL:
				StopSearch ();

				EndDialog (hWnd, TRUE);
				return 0;

			case IDC_EXIT:
				StopSearch ();

				EndDialog (hWnd, FALSE);
				return 0;

			case IDC_STARTSTOP:
				if (g_pState->hDevice)
					StopDevice ();
				else
					CreateDevice ();
				SetButtonStates (hWnd);
				break;

			case IDC_DEREGISTER:
				Deregister ();
				SetButtonStates (hWnd);
				break;

			case IDC_INQUIRY:	// Inquiry
				CreateThread (NULL, 0, DoInquiry, NULL, 0, NULL);
				break;
			}
		}
		break;
	}

	return 0;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
	case WM_CREATE:
		return 0;

	case WM_DESTROY:
		PostQuitMessage (0);
		break;

	case WM_USER + 1:
		if (lParam == WM_LBUTTONDOWN) {
			if (g_pState->hWnd) {
				SetForegroundWindow (g_pState->hWnd);
				break;
			}

			if (! DialogBox (g_pState->hInst, MAKEINTRESOURCE (IDD_MAINBOX), NULL, DlgProc)) {
				NOTIFYICONDATA nid;
				memset (&nid, 0, sizeof(nid));

			   	nid.cbSize = sizeof(NOTIFYICONDATA);
			   	nid.hWnd   = hWnd;
			    nid.uID    = 1;

 				Shell_NotifyIcon (NIM_DELETE, &nid);

				DestroyWindow (hWnd);
			}

			g_pState->Lock ();
			g_pState->hWnd = NULL;
			g_pState->fState = DO_NOTHING;
			CleanInquiryData ();
			g_pState->Unlock ();
		}

		break;

	case WM_USER + 5:
		{
			if (g_pState->hWnd) {
				SetForegroundWindow (g_pState->hWnd);
				break;
			}

			NOTIFYICONDATA nid;
			memset (&nid, 0, sizeof(nid));

		   	nid.cbSize = sizeof(NOTIFYICONDATA);
		   	nid.hWnd   = hWnd;
		    nid.uID    = 1;

			Shell_NotifyIcon (NIM_DELETE, &nid);
			DestroyWindow (hWnd);
		}

		break;

	default:
        return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return FALSE;
}

int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpszCmdLine, int nCmdShow) {
	for (int i = 0 ; (i < 10) && (! IsAPIReady(SH_WMGR)); ++i)	//Wait for shell
		Sleep(1000);

	g_pState = new Global;

	if (! g_pState)
		return 0;

	g_pState->hInst = hInst;

	WNDCLASS	wc;
	memset (&wc, 0, sizeof(wc));

	wc.lpfnWndProc 		= WndProc;
	wc.hInstance   		= hInst;
	wc.lpszClassName 	= APPNAME;

	if (! RegisterClass (&wc))
		return 0;

	g_pState->hWndHidden = CreateWindow (APPNAME, APPNAME, WS_DISABLED,
						CW_USEDEFAULT, CW_USEDEFAULT,
						CW_USEDEFAULT, CW_USEDEFAULT,
						NULL, NULL, hInst, NULL);

	NOTIFYICONDATA nid;
	memset (&nid, 0, sizeof(nid));

   	nid.cbSize 			 = sizeof(NOTIFYICONDATA);
   	nid.hWnd 			 = g_pState->hWndHidden;
   	nid.uID 			 = 1;
   	nid.uFlags 		     = NIF_ICON | NIF_MESSAGE;
   	nid.uCallbackMessage = WM_USER + 1;
	nid.hIcon            = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_BTHICON), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

	if (wcsstr (lpszCmdLine, L"/noicon") == 0)
		Shell_NotifyIcon (NIM_ADD, &nid);

	CreateDevice ();

	MSG msg;

    while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage (&msg) ;
		DispatchMessage(&msg);
	}

	StopDevice ();

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -