Hej udviklere,
Jeg kan ikke connecte til mit program hvis jeg ligger den i min boot.
Men hvis jeg går ind og åbner programmet manuelt, så virker det.
Hvad kan problemet være?
Her er koden:
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
SOCKET Socket;
char IPAddressStr[128];
short SocketPort = 5011;
bool HasRxAddr = true;
struct in_addr RxAddr;
int SetupSocket();
void HandleTx(const char *text);
void HandleRx();
void Restart();
void Shutdown();
void MoniOff();
void CDtrayOpen();
void CDtrayClose();
void DLGBOX();
int main(int argc, char* argv[])
{
SetupSocket();
if(argc == 2)
{ // I'm to close the other PC
strcpy(IPAddressStr, argv[1]);
HandleTx("Please PowerOff");
HandleRx();
}
else
{ // I'm to be closed
while(1)
{
HandleRx();
}
}
}
void HandleTx(const char *text)
{
struct sockaddr_in sockaddr;
memset((char *)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
if(IPAddressStr[0])
sockaddr.sin_addr.s_addr = inet_addr(IPAddressStr);
else if(HasRxAddr)
sockaddr.sin_addr.s_addr = RxAddr.s_addr;
else
{
std::cout << "No Peer Address" << std::endl;
return;
}
if(sockaddr.sin_addr.s_addr == INADDR_NONE)
{
std::cout << "Failed to convert IPAddress!" << std::endl;
return;
}
sockaddr.sin_port = htons(SocketPort);
if(sendto(Socket, text, strlen(text) + 1, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
std::cout << "SendTo Failed" << std::endl;
exit(2);
}
}
void HandleRx()
{
const int MaxLineLength = 256;
char buffer[MaxLineLength + 1];
sockaddr_in RxSockAddr;
int FromLen = sizeof(RxSockAddr);
if(recvfrom(Socket, buffer, MaxLineLength, 0, (sockaddr *)&RxSockAddr, &FromLen) != SOCKET_ERROR)
{
RxAddr = RxSockAddr.sin_addr;
HasRxAddr = true;
std::cout << "Received: " << buffer << std::endl;
if(!strcmp(buffer, "Restart"))
{
HandleTx("Received The Restart Message");
std::cout << "Try To Close" << std::endl;
Sleep(1000);
Restart();
}
else if(!strcmp(buffer, "Shutdown"))
{
HandleTx("Received Shutdown Command");
Sleep(1000);
Shutdown();
}
else if(!strcmp(buffer, "CDTray Open"))
{
HandleTx("Received CD Tray Open Command");
CDtrayOpen();
}
else if(!strcmp(buffer, "CDTray Close"))
{
HandleTx("Received CD Tray Close Command");
CDtrayClose();
}
else if(!strcmp(buffer, "Oops"))
{
HandleTx("Received Oops Command");
DLGBOX();
}
else if(!strcmp(buffer, "Monitor Off"))
{
HandleTx("Received Monitor Off Command");
MoniOff();
}
else if(!strcmp(buffer, "Alive."))
{
HandleTx("I am alive!");
}
else
{
HandleTx("Message Was Received");
}
}
else
{
std::cout << "Failed To Receive: " << WSAGetLastError() << std::endl;
exit(1);
}
}
int SetupSocket()
{
WSADATA wsaData;
struct sockaddr_in sockaddr;
if(WSAStartup(MAKEWORD(1, 1), &wsaData))
{
std::cout << "Failed to start WSA" << std::endl;
exit(3);
}
Socket = socket(AF_INET, SOCK_DGRAM, 0);
if(Socket == INVALID_SOCKET)
{
std::cout << "Failed To Create Socket!" << std::endl;
return 1;
}
memset((char *)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = htons(SocketPort);
BOOL Flag = TRUE;
setsockopt(Socket, SOL_SOCKET, SO_REUSEADDR, (char *)&Flag, sizeof(Flag));
if(bind(Socket, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
{
std::cout << "Failed to bind socket!" << std::endl;
return 1;
}
return 1;
}
void CleanUp()
{
if(Socket)
closesocket(Socket);
WSACleanup();
}
//--------------------START OF FUNCTIONS--------------------------------
void MoniOff()
{
ShellExecute(0, "Open", "003.exe", 0, 0, SW_SHOW);
}
void CDtrayOpen()
{
ShellExecute(0, "Open", "001.exe", 0, 0, SW_SHOW);
}
void CDtrayClose()
{
ShellExecute(0, "Open", "002.exe", 0, 0, SW_SHOW);
}
void DLGBOX()
{
MessageBox(NULL, "DANGER! DANGER!", "hmm?", MB_OK);
}
void Restart()
{
HANDLE Token;
LUID luid;
TOKEN_PRIVILEGES tp;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_WRITE, &Token))
{
std::cout << "OpenThreadToken error: " << GetLastError() << std::endl;
return;
}
if(!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luid))
{
std::cout << "LookupPrivilegeValue error: " << GetLastError() << std::endl;
return;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(Token,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES )NULL,
(PDWORD )NULL);
if(GetLastError() != ERROR_SUCCESS)
{
std::cout << "AdjustTokenPrivileges error: " << std::endl;
return;
}
ExitWindowsEx(EWX_REBOOT, 0);
}
void Shutdown()
{
HANDLE Token;
LUID luid;
TOKEN_PRIVILEGES tp;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_WRITE, &Token))
{
std::cout << "OpenThreadToken error: " << GetLastError() << std::endl;
return;
}
if(!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &luid))
{
std::cout << "LookupPrivilegeValue error: " << GetLastError() << std::endl;
return;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(Token,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES )NULL,
(PDWORD )NULL);
if(GetLastError() != ERROR_SUCCESS)
{
std::cout << "AdjustTokenPrivileges error: " << std::endl;
return;
}
ExitWindowsEx(EWX_SHUTDOWN, 0);
}
//-----------------------END OF FUNCTIONS------------------------------
Indlæg senest redigeret d. 25.11.2009 21:34 af Bruger #10613