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

📄 asui.cxx

📁 这些文件包括蓝牙虚拟串口与打印机程序实例
💻 CXX
📖 第 1 页 / 共 3 页
字号:

	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"Bluetooth ActiveSync");
	}
}

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);

	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) {
		c = DoSDP (&b);
	}

	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\\asui\\device", 0, NULL, 0, KEY_ALL_ACCESS, 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));
		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");
	WCHAR szText[128];
	SetWindowText (GetDlgItem (hWnd, IDC_CURRENT), GetRegistration (szText, 128) ? szText : L"ERROR");
}

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;
}

static BOOL CALLBACK DlgProc3 (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		{
		g_pState->hWnd = hWnd;
		EnableWindow (GetDlgItem (hWnd, IDOK), g_pState->hDevice ? TRUE : FALSE);

		WCHAR szText[128];
		SetWindowText (GetDlgItem (hWnd, IDC_CURRENT), GetRegistration (szText, 128) ? szText : L"ERROR");

		SetForegroundWindow (hWnd);
		SetFocus (GetDlgItem (hWnd, g_pState->hDevice ? IDOK : IDC_CURRENT));

		return 1;
		}

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDCANCEL:
				EndDialog (hWnd, DO_STOP);
				return 0;

			case IDOK:
				EndDialog (hWnd, DO_SYNC);
				return 0;

			case IDC_OPTIONS:
				EndDialog (hWnd, DO_OPTIONS);
				return 0;
			}
		}
		break;
	}

	return 0;
}

static int CopyValue(HKEY hkDest, HKEY hkSrc, WCHAR *pszValueName) { // Returns ERROR_SUCCESS
	WCHAR szValue[256];
	DWORD dwType = 0;
	DWORD dwSize = sizeof(szValue);
	
	if ((! hkDest) || (! hkSrc) || (! pszValueName))
		return ERROR_FILE_NOT_FOUND;

	int fRes = RegQueryValueEx(hkSrc, pszValueName, NULL, &dwType, (LPBYTE )szValue, &dwSize);
	if (fRes != ERROR_SUCCESS)
		return fRes;

	fRes = RegSetValueEx(hkDest, pszValueName, 0, dwType, (LPBYTE )szValue, dwSize);

	return fRes;
}

static int CopyValues(HKEY hkDest, HKEY hkSrc) { // Returns ERROR_SUCCESS
	DWORD dwIndex = 0;
	WCHAR szValueName[256];
	DWORD cbValueName = sizeof(szValueName) / sizeof(WCHAR);
	DWORD dwType = 0;

	while(ERROR_SUCCESS==RegEnumValue(hkSrc, dwIndex++, szValueName, &cbValueName, NULL, &dwType, NULL, NULL)) {
		int fRet = CopyValue (hkDest, hkSrc, szValueName);

		if (fRet != ERROR_SUCCESS)
			return fRet;

		cbValueName = sizeof(szValueName) / sizeof(WCHAR);
	}

	return ERROR_SUCCESS;
}

static int MoveRegistryKey (LPWSTR szTo, LPWSTR szFrom) { // Returns ERROR_SUCCESS
	HKEY hKeyDest=NULL;
	DWORD dwDisp = 0;

	int fRet = RegCreateKeyEx(HKEY_CURRENT_USER, szTo, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKeyDest, &dwDisp);
	if (fRet != ERROR_SUCCESS)
		return fRet;

	HKEY hKeySrc=NULL;
	fRet = RegOpenKeyEx(HKEY_CURRENT_USER, szFrom, 0, KEY_READ, &hKeySrc);

	if (fRet != ERROR_SUCCESS) {
		RegCloseKey (hKeyDest);
		RegDeleteKey (HKEY_CURRENT_USER, szTo);
		return fRet;
	}

	fRet = CopyValues (hKeyDest, hKeySrc);

	RegCloseKey(hKeyDest);
	RegCloseKey(hKeySrc);

	if (fRet != ERROR_SUCCESS)
		RegDeleteKey (HKEY_CURRENT_USER, szTo);
	else
		RegDeleteKey (HKEY_CURRENT_USER, szFrom);

	return fRet;
}

static void LaunchActiveSync (HWND hTimer) {
	HKEY hk;

	if (g_fHaveSav)
		return;

	MoveRegistryKey (L"ControlPanel\\CommSav", L"ControlPanel\\Comm");

	DWORD dwDisp;
	if (ERROR_SUCCESS == RegCreateKeyEx (HKEY_CURRENT_USER, L"ControlPanel\\Comm", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hk, &dwDisp)) {
		g_fHaveSav = TRUE;
		DWORD one = 1;

		RegSetValueEx (hk, L"Cnct", 0, REG_SZ, (LPBYTE)RAS_NAME_BLUETOOTH, sizeof(RAS_NAME_BLUETOOTH));
		RegSetValueEx (hk, L"AutoCnct", 0, REG_DWORD, (LPBYTE)&one, sizeof(one));

		RegCloseKey (hk);

		SetTimer (hTimer, 1, 5000, NULL);

		PROCESS_INFORMATION pi;

		WCHAR szProcName[128];
		wcscpy (szProcName, L"repllog.exe");

		WCHAR szParameters[128];
		wcscpy (szParameters, L"/" APP_RUN_AT_RS232_DETECT);

		int fRes = CreateProcess (szProcName, szParameters, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &pi);

		if (fRes) {
			CloseHandle (pi.hProcess);
			CloseHandle (pi.hThread);
		}
	}
}

static void RestoreActiveSync (HWND hTimer) {
	if (! g_fHaveSav)
		return;

	KillTimer (hTimer, 1);
	MoveRegistryKey (L"ControlPanel\\Comm", L"ControlPanel\\CommSav");

	g_fHaveSav = FALSE;
}

static 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_TIMER:
		RestoreActiveSync (hWnd);
		break;

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

			int iRes = DialogBox (g_pState->hInst, MAKEINTRESOURCE (IDD_STARTBOX), NULL, DlgProc3);
			g_pState->hWnd = NULL;

			if (iRes == DO_STOP)
				break;

			if (iRes == DO_SYNC) {
				LaunchActiveSync (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 + -