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

📄 asui.cxx

📁 这些文件包括蓝牙虚拟串口与打印机程序实例
💻 CXX
📖 第 1 页 / 共 3 页
字号:
		return 0;
	}
	
	if (g_pState->fState != DO_NOTHING) {
		g_pState->fState = DO_STOP;
		g_pState->Unlock ();

		SetWindowText (hWnd, L"Stopping...");

		return 0;
	}

	CleanInquiryData ();

	g_pState->fState = DO_INQUIRY;

	g_pState->Unlock ();

	HWND hWndButton  = GetDlgItem (hWnd, IDC_INQUIRY);
	HWND hWndDevList = GetDlgItem (hWnd, IDC_DEVICELIST);

	SetWindowText (hWndButton, L"STOP");
	SetWindowText (hWnd, L"Inquiry Running...");
	SendMessage (hWndDevList, LB_RESETCONTENT, 0, 0);

	HANDLE hLookup;
	int iErr = PerformInquiry(hWndDevList, FALSE, &hLookup);

	if (iErr == ERROR_SUCCESS)
		iErr = PerformInquiry(hWndDevList, TRUE, &hLookup);

	BthNsLookupServiceEnd(hLookup);

	if (iErr != ERROR_SUCCESS) {
		SetWindowText (hWndButton, L"Inquiry");
		SetWindowText (hWnd, L"Bluetooth ActiveSync");

		g_pState->fState = DO_NOTHING;

		WCHAR szString[64];
		wsprintf (szString, L"Bluetooth hardware error %d\n", iErr);
		MessageBox(hWnd, szString, L"Error", MB_OK | MB_TOPMOST);
		return 0;
	}

	SetWindowText (hWnd, L"SDP Queries");

	g_pState->Lock ();
	CHECK_STOP;

	g_pState->fState = DO_SDP;

	while (g_pState->hWnd && (g_pState->fState != DO_STOP)) {
		InquiryResult *pRes = g_pState->pDev;
		while (pRes && pRes->fHaveSDP)
			pRes = pRes->pNext;

		if (! pRes)
			break;

		pRes->fHaveSDP = TRUE;
		BT_ADDR b = pRes->b;

		g_pState->Unlock ();

		WCHAR szTitle[64];
		wsprintf (szTitle, L"SDP %04x%08x", GET_NAP(b), GET_SAP(b));
		SetWindowText (hWnd, szTitle);

		int channel = DoSDP (&b);

		if (channel) {
			for (int i = 0 ; ; ++i) {
				int iData = SendMessage (hWndDevList, LB_GETITEMDATA, (WPARAM)i, (LPARAM)0);
				if (iData == LB_ERR)
					break;

				if (iData == (int)pRes) {
					WCHAR szOldName[MAX_NAME + 64];
					WCHAR szNewName[MAX_NAME + 128];
					if (SendMessage(hWndDevList, LB_GETTEXTLEN, (WPARAM)i, (LPARAM)0) <= (MAX_NAME + 64)) {
						SendMessage (hWndDevList, LB_GETTEXT, (WPARAM)i, (LPARAM)szOldName);
						wsprintf (szNewName, L"%s (c = %d)", szOldName, channel);

						SendMessage (hWndDevList, LB_DELETESTRING, (WPARAM)i, (LPARAM)0);
						SendMessage (hWndDevList, LB_INSERTSTRING, (WPARAM)i, (LPARAM)szNewName);
						SendMessage (hWndDevList, LB_SETITEMDATA,  (WPARAM)i, (LPARAM)pRes);
					}

					break;
				}
			}

		}

		g_pState->Lock ();
		if (channel) {
			InquiryResult *pX = g_pState->pDev;
			while (pX && (pX != pRes))
				pX = pX->pNext;

			if (pX)
				pX->channel = channel;
		}
	}

	g_pState->fState = DO_NOTHING;
	g_pState->Unlock ();

	SetWindowText (hWndButton, L"Inquiry");
	SetWindowText (hWnd, L"Bluetooth ActiveSync");
	return 0;
}

static BOOL CALLBACK DlgProc2 (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {
	case WM_INITDIALOG:
		SetWindowText (GetDlgItem (hWnd, IDC_CHANNEL), L"");
		SetFocus (GetDlgItem (hWnd, IDC_CHANNEL));

		return 0;

	case WM_COMMAND:
		{
			int wID = LOWORD(wParam);
			switch (wID)
			{
			case IDOK:
				{
					WCHAR szChannel[64];
					GetWindowText (GetDlgItem (hWnd, IDC_CHANNEL), szChannel, 64);
					int c = _wtoi (szChannel);
					EndDialog (hWnd, c);
				}
				return 0;

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

	return 0;
}

static int GetBA (WCHAR *pp, BT_ADDR *pba) {
	while (*pp == ' ')
		++pp;

	for (int i = 0 ; i < 4 ; ++i, ++pp) {
		if (! iswxdigit (*pp))
			return FALSE;

		int c = *pp;
		if (c >= 'a')
			c = c - 'a' + 0xa;
		else if (c >= 'A')
			c = c - 'A' + 0xa;
		else c = c - '0';

		if ((c < 0) || (c > 16))
			return FALSE;

		*pba = *pba * 16 + c;
	}

	for (i = 0 ; i < 8 ; ++i, ++pp) {
		if (! iswxdigit (*pp))
			return FALSE;

		int c = *pp;
		if (c >= 'a')
			c = c - 'a' + 0xa;
		else if (c >= 'A')
			c = c - 'A' + 0xa;
		else c = c - '0';

		if ((c < 0) || (c > 16))
			return FALSE;

		*pba = *pba * 16 + c;
	}

	if ((*pp != ' ') && (*pp != '\0'))
		return FALSE;

	return TRUE;
}

static void StopDevice (void) {
	if (g_pState->hDevice) {
		DeregisterDevice (g_pState->hDevice);
		g_pState->hDevice = NULL;
	}

	RasDeleteEntry (NULL, RAS_NAME_BLUETOOTH);
}

static int IsRegistered (void) {
	HKEY hk;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\asui\\device", 0, KEY_ALL_ACCESS, &hk))
		return FALSE;

	RegCloseKey (hk);

	return TRUE;
}

static void Deregister (void) {
	StopDevice ();

	RegDeleteKey (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\asui\\device");
}

static int GetRegistration (WCHAR *szOut, int cSize) {
	if (cSize < 64)
		return FALSE;

	wcscpy (szOut, L"No bonding");

	HKEY hk;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"ExtModems\\bluetooth_syn", 0, KEY_READ, &hk)) {
		wcscpy (szOut, L"No sync key!");

		return TRUE;
	}

	WCHAR szString[256];
	DWORD dwSize = sizeof(szString);
	DWORD dwType;

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"port", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		wcscpy (szOut, L"No sync port!");
		RegCloseKey (hk);

		return TRUE;
	}

	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')) {
		wcscpy (szOut, L"Bad AS port!");

		return TRUE;
	}

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

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\asui\\device", 0, KEY_ALL_ACCESS, &hk))
		return TRUE;

	BT_ADDR b;
	unsigned char c;

	DWORD dwC;
	dwSize = sizeof(dwC);
	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"channel", NULL, &dwType, (BYTE *)&dwC, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dwC))) {
		RegCloseKey (hk);

		return TRUE;
	}

	c = (unsigned char)dwC;

	dwSize = sizeof(szString);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"device", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		RegCloseKey (hk);

		return TRUE;
	}

	RegCloseKey (hk);

	if ((c <= 0) || (c > 31)) {
		wcscpy (szOut, L"Bad channel!");
		return TRUE;
	}

	if (! GetBA (szString, &b)) {
		wcscpy (szOut, L"Bad address!");
		return TRUE;
	}

	wsprintf (szOut, L"%04x%08x %d %s on COM%d:", GET_NAP(b), GET_SAP(b), c, g_pState->hDevice ? L"Running" : L"Stopped", ndx);

	return TRUE;
}

static void CreateDevice (void) {
	HKEY hk;
	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"ExtModems\\bluetooth_syn", 0, KEY_READ, &hk))
		return;

	WCHAR szName[50];
	DWORD dwSize = sizeof(szName);
	DWORD dwType;

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

		return;
	}

	RegCloseKey (hk);

	if ((toupper (szName[0]) != 'C') || (toupper (szName[1]) != 'O') ||
		(toupper (szName[2]) != 'M') || (szName[3] < '0') || (szName[3] > '9') ||
		(szName[4] != ':') || (szName[5] != '\0')) {
		MessageBox (NULL, L"ActiveSync partner is not defined (bad port)!", L"Bluetooth AS Error", MB_OK | MB_TOPMOST);
		RegCloseKey (hk);

		return;
	}

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

	int imtu = 0;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\asui", 0, KEY_READ, &hk))
		return;

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

	RegCloseKey (hk);

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\asui\\device", 0, KEY_READ, &hk))
		return;

	BT_ADDR b;
	unsigned char c;

	DWORD dwC;
	dwSize = sizeof(dwC);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"channel", NULL, &dwType, (BYTE *)&dwC, &dwSize)) ||
		(dwType != REG_DWORD) || (dwSize != sizeof(dwC))) {
		RegCloseKey (hk);

		return;
	}

	c = (unsigned char)dwC;

	WCHAR szString[256];
	dwSize = sizeof(szString);

	if ((ERROR_SUCCESS != RegQueryValueEx (hk, L"device", NULL, &dwType, (BYTE *)szString, &dwSize)) ||
		(dwType != REG_SZ) || (dwSize > sizeof(szString))) {
		RegCloseKey (hk);

		return;
	}

	RegCloseKey (hk);

	if ((c <= 0) || (c > 31)) {
		MessageBox (NULL, L"Bad channel in AS registry!", L"Bluetooth AS Error", MB_OK | MB_TOPMOST);
		return;
	}

	if (! GetBA (szString, &b)) {
		MessageBox (NULL, L"Bad address in AS registry!", L"Bluetooth AS Error", MB_OK | MB_TOPMOST);
		return;
	}

	StopDevice ();

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

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

	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"Bluetooth AS Error", MB_OK | MB_TOPMOST);
	}

	if (! g_pState->hDevice)
		return;

//	Name=`Bluetooth
//  +UseCountryAndAreaCodes=N
//  +SpecificIpAddr=N
//  +SpecificNameServers=N
//  +DeviceType=direct
//  +DeviceName=BluetoothSYN
//  +IpHeaderCompression=N
//  +SwCompression=N
//  UserName=guest
//  Password=guest

	RASENTRY RasEntry;

	RasEntry.dwSize = sizeof(RASENTRY);
	DWORD cb = sizeof(RASENTRY);
	RasGetEntryProperties (NULL, L"", &RasEntry, &cb, NULL, NULL);

	RasEntry.dwfOptions &= ~(RASEO_SpecificNameServers|RASEO_SpecificIpAddr|
									RASEO_IpHeaderCompression|RASEO_SwCompression|RASEO_UseCountryAndAreaCodes);

	wcscpy (RasEntry.szDeviceType, L"direct");
	wcscpy (RasEntry.szDeviceName, L"BluetoothSYN");

	RasSetEntryProperties(NULL, RAS_NAME_BLUETOOTH, &RasEntry, sizeof(RasEntry), NULL, 0);

	RASDIALPARAMS	RasDialParams;
	memset((char *)&RasDialParams, 0, sizeof(RasDialParams));

	RasDialParams.dwSize = sizeof(RASDIALPARAMS);
	wcscpy (RasDialParams.szEntryName, RAS_NAME_BLUETOOTH);

	wcscpy (RasDialParams.szUserName, L"guest");
	wcscpy (RasDialParams.szPassword, L"guest");

	RasSetEntryDialParams(NULL, &RasDialParams, FALSE);
}

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

⌨️ 快捷键说明

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