classfree 2004年12月24日
帮我看看这个UDP通信的代码, 为什么会出问题?
运行一段时间就会提示运行是错误. 时间没定,有时候很久没有出问题~~

#include <windows.h>
#include <winsock.h>
#include <tlhelp32.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")


DWORD WINAPI MyThreadProc(LPVOID lpParameter);
void RecvData();
BOOL EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable);
void KillProcess(char *filename1, char *filename2);
SOCKET sockfd;
struct sockaddr_in serv_addr;
struct sockaddr_in client_addr;
struct hostent *host;
DWORD TID = 0;
OSVERSIONINFO info;


//----------------------------------------------------------------------
BOOL APIENTRY DllMain( HINSTANCE hInstance,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
HANDLE hEvent = CreateEvent(NULL, FALSE, TRUE, "HDDGMon");
WORD wVersionRequested = MAKEWORD( 2, 2 );
WSADATA wsadata;
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
WSAStartup(wVersionRequested, &wsadata);
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
break;
case DLL_PROCESS_DETACH:
if(TID != 0)
{
PostThreadMessage(TID,WM_QUIT,0,0);
TID = 2;
}
WSACleanup();
break;
}

if(TID == 0)
{
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) != SOCKET_ERROR)
{
serv_addr.sin_port = htons(1790);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.S_un.S_addr = INADDR_ANY;
if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != SOCKET_ERROR)
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadProc, 0, 0, &TID);
}
}
}
return TRUE;
}

void SpChar(char *input, OUT char *cmd, OUT char *spath, OUT char *apath)
{
char strtmp[256];
strcpy(strtmp, input);
char *temp;
char *p1;
char *p2;
char *p3;
try
{
for (p1=strtmp; *p1 != ','; p1++);//找逗号
*p1 = '\0';
temp = p1 + 1;
p1 = strtmp;

for(p2=temp; *p2!=','; p2++);
*p2 = '\0';
p3 = p2 + 1;
p2 = temp;
}
catch(...)
{
}
strcpy(cmd, p1);
strcpy(spath, p2);
strcpy(apath, p3);
}

void GetPath(TCHAR *sPath, OUT TCHAR *ourpath)
{
TCHAR sDrive[_MAX_DRIVE];
TCHAR sDir[_MAX_DIR];
TCHAR sFname[_MAX_FNAME];
TCHAR sExt[_MAX_EXT];

_splitpath(sPath, sDrive, sDir, sFname, sExt);

strcpy(ourpath, sDrive);
strcat(ourpath, sDir);

if(sPath[strlen(ourpath) - 1] != '\\')
strcat(ourpath, "\\");
}

void DeleteFolder(char *FolderPath, bool bShow)
{
char *buf;
int n = strlen(FolderPath);
buf = new char[n+4];
try
{
memset(buf, 0, n+4);
strcpy(buf, FolderPath);

SHFILEOPSTRUCT st;
memset(&st, 0, sizeof(SHFILEOPSTRUCT));

st.hwnd = NULL;
st.fFlags = FOF_MULTIDESTFILES|FOF_NOCONFIRMATION;
if(!bShow)
st.fFlags = st.fFlags|FOF_SILENT;
st.wFunc = FO_DELETE;
st.pFrom = buf;
st.pTo = NULL;
SHFileOperation(&st);
delete buf;
}
catch(...)
{
delete buf;
}
}

void RecvData()
{
try
{
char buf[512];
int namelen = sizeof(serv_addr);
recvfrom(sockfd, buf, 512, 0, (struct sockaddr *)&serv_addr, &namelen);

if(strlen(buf) > 2)
{
char msg[] = "#成功#";
char cmd[16];
char sourpath[256];
char amipath[256];
char temp[256];
SpChar(buf, cmd, sourpath, amipath);
if(stricmp(cmd, "#RUNFILE#") == 0)
{
//运行文件
GetPath(sourpath, temp);
ShellExecute(NULL, "Open", sourpath, amipath, temp, SW_NORMAL);
//sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr*)&remote_addr, sizeof(remote_addr));
}
else if(stricmp(cmd, "#DELFILE#") == 0)
{
//删除文件
SetFileAttributes(sourpath, FILE_ATTRIBUTE_ARCHIVE);
DeleteFile(sourpath);
}
else if(stricmp(cmd, "#DELFOLDER#") == 0)
{
//删除目录
DeleteFolder(sourpath, false);
}
else if(stricmp(cmd, "#COPYFILE#") == 0)
{
//复制文件
CopyFile(sourpath, amipath, false);
}
else if(stricmp(cmd, "#MOVEFILE#") == 0)
{
//移动文件或者重命名
MoveFile(sourpath, amipath);
}
else if(stricmp(cmd, "#MSGBOX#") == 0)
{
//消息筐
MessageBox(NULL, sourpath, amipath, MB_OK|MB_ICONQUESTION);
}
else if(stricmp(cmd, "#KILLPROCESS#") == 0)
{
//结束进程
EnablePrivilege(SE_DEBUG_NAME, TRUE);
KillProcess(sourpath, amipath);
}
else if(stricmp(cmd, "#SLEEP#") == 0)
{
//进入休眠
if(info.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
}
SetSystemPowerState(FALSE, TRUE);
}
else if(stricmp(cmd, "#REBOOT#") == 0)
{
//重新启动计算机
if(info.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
}
ExitWindowsEx(EWX_FORCE|EWX_REBOOT, 0);
}
else if(stricmp(cmd, "#CLOSEPOWER#") == 0)
{
//关闭计算机
if(info.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
EnablePrivilege(SE_SHUTDOWN_NAME, TRUE);
//InitiateSystemShutdown(NULL, "即将关闭计算机", 1000, FALSE, TRUE);
}
ExitWindowsEx(EWX_FORCE|EWX_SHUTDOWN, 0);
}
}
}
catch(...)
{
}
}

DWORD WINAPI MyThreadProc(LPVOID lpParameter)
{
MSG msg;
UINT hTimer;
hTimer = SetTimer(NULL,NULL,10000,0);

while(GetMessage(&msg, NULL, 0, 0))
{
switch(msg.message)
{
case WM_TIMER:
RecvData();
break;
default:
break;
}
}
if(hTimer)
KillTimer(NULL, hTimer);
return 0;
}

//提升权限
BOOL EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable)
{
HANDLE hToken;
TOKEN_PRIVILEGES tp;
LUID luid;
BOOL ret;

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ,
&hToken))
return FALSE;

if (!LookupPrivilegeValue(NULL, lpszPrivilegeName, &luid))
return FALSE;

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;

ret = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
CloseHandle(hToken);

return ret;
}

char * GetMyPath(char *fullpath)
{
char sDrive[_MAX_DRIVE];
char sDir[_MAX_DIR];
char sFname[_MAX_FNAME];
char sExt[_MAX_EXT];
_splitpath(fullpath, sDrive, sDir, sFname, sExt);

return strcat(sFname, sExt);
}
//结束进程
void KillProcess(char *filename1, char *filename2)
{
HANDLE snapshot;
PROCESSENTRY32 processinfo;
processinfo.dwSize = sizeof(processinfo) ;
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(snapshot == NULL)
return;

bool status = Process32First (snapshot, &processinfo);
while(status)
{
char *sFname;
sFname = GetMyPath(processinfo.szExeFile);
if(stricmp(filename1, sFname) == 0 || stricmp(filename2, sFname) == 0)
{
HANDLE handle = OpenProcess(PROCESS_TERMINATE, FALSE, processinfo.th32ProcessID);
TerminateProcess(handle, 0);
CloseHandle(handle);
}

status = Process32Next(snapshot, &processinfo);
}
CloseHandle(snapshot);
}
...全文
91 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
网络编程
创建于2007-09-28

7878

社区成员

6.4w+

社区内容

VC/MFC 网络编程
社区公告
暂无公告