I'm trying to my wifi connection to send data to a reciver for experimentations. I managed to connect the card to the reciver using the windows API Native for wifi.
I store the GUID of my card in pIfInfo pointer (PWLAN_INTERFACE_INFO) and extract it to create the handle (WifiAcess) that should allow me to write on the connection.
Then if i run this I get the error ERROR_INVALID_PARAMETER on the WriteFile function with and without the (void*) casting before envois.
Well, My problem is that, I don't know which parameter is invalid
DWORD bytesSend;
char *buffer ;
unsigned int nbChar ;
data to send
int headerData[4] = {85,0,11,0};
int ServosData[6] = {100,100,0,100,0,0};
BYTE * envois = new BYTE[11];
envois[0] = headerData[0] ;
envois[1] = headerData[1] ;
envois[2] = headerData[2] ;
envois[3] = headerData[3] ;
envois[4] = ServosData[0] ;
envois[5] = ServosData[1] ;
envois[6] = ServosData[2] ;
envois[7] = ServosData[3] ;
envois[8] = ServosData[4] ;
envois[9] = ServosData[5] ;
check summ
for(int i = 0 ; i<10;i++)
{
envois[10] = envois[10] + envois[i] ;
}
envois[10] = envois[10]%256 ;
nbChar = 11 ;
HANDLE WifiAcess = INVALID_HANDLE_VALUE;
LPCSTR AcessGUID ;
char GuidString2Char[46] ;
char DefChar = ' ';
bool sendtOk = false ;
here we get the GUID of our Wlan card from the wifi connection
sprintf(GuidString2Char, "\\\\.\\{%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
pIfInfo->InterfaceGuid.Data1,
pIfInfo->InterfaceGuid.Data2,
pIfInfo->InterfaceGuid.Data3,
pIfInfo->InterfaceGuid.Data4[0],
pIfInfo->InterfaceGuid.Data4[1],
pIfInfo->InterfaceGuid.Data4[2],
pIfInfo->InterfaceGuid.Data4[3],
pIfInfo->InterfaceGuid.Data4[4],
pIfInfo->InterfaceGuid.Data4[5],
pIfInfo->InterfaceGuid.Data4[6],
pIfInfo->InterfaceGuid.Data4[7]);
Put the translated GUID in a LPCSTR
AcessGUID = GuidString2Char ;
then create the Handle
WifiAcess = CreateFile(
AcessGUID ,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
(HANDLE)INVALID_HANDLE_VALUE); // NULL
Check for errors
if(WifiAcess == INVALID_HANDLE_VALUE)
{
dwResult = GetLastError();
switch(dwResult)
{
case ERROR_FILE_NOT_FOUND :
wprintf(L"\t\t\CreateFile ERROR_FILE_NOT_FOUND\n");
break ;
default :
wprintf(L"\t\t\CreateFile default\n");
break;
}
}
else { wprintf(L"\t\t\CreateFile Success\n"); }
Try to write the buffer on the WifiAcess Handle
sendtOk = WriteFile(WifiAcess, envois, nbChar, &bytesSend, NULL) ;
if(!sendtOk)
{
wprintf(L"\n\t\t\tEcheque de l envois : %d bytes envoyes\n\n", bytesSend);
dwResult = GetLastError();
switch(dwResult)
{
////////////////////////////////////////////////////////////////////////////////////////////////////
case ERROR_INVALID_PARAMETER :
wprintf(L"\t\t\WriteFile ERROR_INVALID_PARAMETER\n");
break ;
////////////////////////////////////////////////////////////////////////////////////////////////////
case S_FALSE :
wprintf(L"\t\t\WriteFile S_FALSE\n");
break ;
case E_UNEXPECTED :
wprintf(L"\t\t\WriteFile E_UNEXPECTED\n");
break ;
default :
wprintf(L"\t\t\WriteFile default with dword : 0x%x \n", dwResult);
break;
}
}else
{
wprintf(L"Data Sendt\n\n");
}
Ok, i'found what was wrong, the analogy with serial com cannot be used, instead i used the winsock class :
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738545(v=vs.85).aspx
===================================================================================================== initialisation :
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// ======================================================================================================
// Création du Socket :
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_INET ;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
#define DEFAULT_PORT "27015"
// Resolve the server address and port
iResult = getaddrinfo("10.10.100.254","8899" , &hints, &result); //"10.10.100.254" DEFAULT_PORT "8899"
if (iResult == 0) {
printf("\n\t\tgetaddrinfo, adresse trouvee\n");
struct addrinfo *res = NULL;
int error;
for (res = result; res != NULL; res = res->ai_next)
{
char hostname[NI_MAXHOST] = "";
error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
if (error != 0)
{
fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
continue;
}
if (*hostname != '\0')
{
printf("hostname: %s\n", hostname);
ptr=res;
}
}
}else
{
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}
SOCKET ConnectSocket = INVALID_SOCKET;
// Attempt to connect to the first address returned by
// the call to getaddrinfo
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// ======================================================================================================
// Connect to a socket :
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
switch(WSAGetLastError())
{
case WSAEHOSTUNREACH:
wprintf(L"\n\t\tNo route to host. A socket operation was attempted to an unreachable host. See WSAENETUNREACH.\n");
break;
default:
break;
}
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple example we just free the resources
// returned by getaddrinfo and print an error message
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// ======================================================================================================
// Send and recive data :
#define DEFAULT_BUFLEN 512
int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "this is a test";
//char recvbuf[DEFAULT_BUFLEN];
// Send an initial buffer
for(int i = 0; i<100;i++)
{
if(sendCommand(ConnectSocket,i, i, 0, i)==-1) break;
Sleep(10);
}
// Disconnecting the Client : http://msdn.microsoft.com/en-us/library/windows/desktop/bb530743(v=vs.85).aspx
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
this worked fin for me
Your switch to Windows sockets looks good. The only suggestions I'd have is, if you need cross-version compatibility, adding some fallback logic incase WSAStartup fails finding 2.2 that you're requesting.
Other suggestions would be stylistic, mainly to clean up your usage of WSACleanup by consolidating cleanup code for all the cases - such as using an SEH/leave or a goto fail case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.