Wake On Lan

Hello, this C++ console application will simply just not work.
The MacAdress is correct.
It prints "Result: 102" ( wich is correct ).
But it doesn't wake the computer up. ( It works with other WOL programs, WOL is enabled. )

I can't understand what's wrong. Could anyone please spot my error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <afxsock.h>

void ErrorWrapper() {

		DWORD dwRC = NULL;
		DWORD dwError = GetLastError();
		LPVOID lpMsgBuf;
		lpMsgBuf = NULL;

		dwRC = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
			|FORMAT_MESSAGE_FROM_SYSTEM
			|FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dwError,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR) &lpMsgBuf,
			0,
			NULL);

		if ( dwRC && lpMsgBuf )
			printf("Error %d: %s\n", dwError, lpMsgBuf);
		else
			printf("Error %d\n", dwError);
}

bool IsValidHexChar(int c) {
	if ( ( c >= '0' && c <= '9' ) || ( c >= 'A' && c <= 'F' ) || ( c >= 'a' && c <= 'f' ) )
		return true;
	return false;
}

bool IsMacAddr(char* addr) {
	if ( strlen(addr) != 17)
		return false;
	for ( int i = 0 ; i < 17 ; i++ ) {
		if ( (i + 1) % 3 ) {
			if ( !IsValidHexChar(addr[i]) )
				return false;
		}
		else {
			if ( ! ( addr[i] == '-' || addr[i] == '-' ) )
				return false;
		}
	}
	return true;
}

int main(int argc, char *argv[]) {

	if ( argc != 2 )
		return 0;

	if ( ! AfxSocketInit() )
		return 0;

	char Addr[64];
	strcpy_s(Addr, 63, argv[1]);

	if ( ! IsMacAddr(Addr) )
		return 0;

	int magicP[102];

	for ( int i = 0 ; i < 6 ; i++ )
		magicP[i] = 0xFF;

	unsigned int tempint;
	char tempstr[4];

	for ( int i = 0 ; i < 6 ; i++ ) {
		sprintf_s(tempstr, 3, "%c%c", toupper(Addr[i * 2 + i * 1]), toupper(Addr[i * 2 + i * 1 + 1]));
		sscanf_s(tempstr, "%x", &tempint);
		magicP[6 + i] = tempint;
	}

	for ( int i = 2 ; i < 17 ; i++ ) {
		for ( int j = 0 ; j < 6 ; j++ ) {
			magicP[i * 6 + j] = magicP[6 + j];
		}
	}

	UINT Port = 9; // Tried 7, 9, 40000

	CAsyncSocket Socket;

	Socket.Create(Port, SOCK_DGRAM);

	BOOL bOptVal = TRUE;
	if (Socket.SetSockOpt(SO_BROADCAST,(char*)&bOptVal,sizeof(BOOL))==SOCKET_ERROR) 
		return 0;

	int result = Socket.SendTo(magicP, 102, Port);

	Socket.Close();

	if ( result != SOCKET_ERROR ) {
		printf("\nResult: %d\n", result);
		Sleep(2000);
	}
	else {
		ErrorWrapper();

		printf("\n%d = WSANOTINITIALISED \
		\n%d = WSAENETDOWN \
		\n%d = WSAEACCES \
		\n%d = WSAEINPROGRESS \
		\n%d = WSAEFAULT  \
		\n%d = WSAEINVAL \
		\n%d = WSAENETRESET \
		\n%d = WSAENOBUFS \
		\n%d = WSAENOTCONN \
		\n%d = WSAENOTSOCK \
		\n%d = WSAEOPNOTSUPP \
		\n%d = WSAESHUTDOWN \
		\n%d = WSAEWOULDBLOCK \
		\n%d = WSAEMSGSIZE \
		\n%d = WSAECONNABORTED \
		\n%d = WSAECONNRESET \
		\n%d = WSAEADDRNOTAVAIL \
		\n%d = WSAEAFNOSUPPORT \
		\n%d = WSAEDESTADDRREQ \
		\n%d = WSAENETUNREACH",

		WSANOTINITIALISED,
		WSAENETDOWN,
		WSAEACCES,
		WSAEINPROGRESS,
		WSAEFAULT,
		WSAEINVAL,
		WSAENETRESET,
		WSAENOBUFS,
		WSAENOTCONN,
		WSAENOTSOCK,
		WSAEOPNOTSUPP,
		WSAESHUTDOWN,
		WSAEWOULDBLOCK,
		WSAEMSGSIZE,
		WSAECONNABORTED,
		WSAECONNRESET,
		WSAEADDRNOTAVAIL,
		WSAEAFNOSUPPORT,
		WSAEDESTADDRREQ,
		WSAENETUNREACH);
		system("PAUSE");
	}
	
	return 1;
}
Topic archived. No new replies allowed.