c++ - Winsock remote device discovery -
i new winsock , wish use bluetooth project.
i wrote simple code taking online resources find remote devices
it should print name of remote devices instead prints hex value think...i dont know is
the code is
#include "stdafx.h" #include<iostream> #include<winsock2.h> #include<ws2bth.h> #include<bluetoothapis.h> #include<stdlib.h> using namespace std; #define success 0 #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "irprops.lib") int main() { wsadata data; int result; result = wsastartup(makeword(2, 2), &data); if (result != success) { cout << "error occured while initialising winsock..."; exit(result); } cout << "winsock initialisation successful\n"; wsaqueryset queryset; memset(&queryset, 0, sizeof(wsaqueryset)); queryset.dwsize = sizeof(wsaqueryset); queryset.dwnamespace = ns_bth; handle hlookup; result = wsalookupservicebegin(&queryset, lup_containers, &hlookup); if (result != success) { cout << "error in initialising service\n"; exit(result); } cout << "initialising lookup service successful\n"; byte buffer[4096]; memset(buffer, 0, sizeof(buffer)); dword bufferlength = sizeof(buffer); wsaqueryset *presults = (wsaqueryset*)&buffer; while (result == success) { result = wsalookupservicenext(hlookup, lup_return_name | lup_containers | lup_return_addr | lup_flushcache | lup_return_type | lup_return_blob | lup_res_service, &bufferlength, presults); if (result == success) { //device found lptstr s = presults->lpszserviceinstancename; cout << s << endl; sleep(1000); } } wsalookupserviceend(hlookup); return 0; }
i require in solving issue
thanks in advance help
you have (potential) mismatch of character encodings. line
lptstr s = presults->lpszserviceinstancename;
expands to
lpwstr s = presults->lpszserviceinstancename;
if have project's character encoding set unicode (default setting). output unicode string, have use std::wcout instead of std::cout
:
lpcwstr s = presults->lpszserviceinstancename; wcout << s << endl;
to reduce odds of inadvertently using unexpected character encoding, code should explicitly specify character encoding uses. code in question should use wsaquerysetw
, , call wsalookupservicebeginw
, wsalookupservicenextw
instead.
explanation of observed behavior:
std::cout
interprets const char*
c-style string, , displays characters until finds nul
character (see operator<<(std::basic_ostream)).
a const wchar_t*
, on other hand, not interpreted mean special. std::cout
treats other pointer, , prints value using hexadecimal numeral system default (see std::basic_ostream::operator<<).
Comments
Post a Comment