void CNetMonDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
if(m_pThread==NULL)
{
m_pThread=(EnumThread *)AfxBeginThread(RUNTIME_CLASS(EnumThread),0,0,CREATE_SUSPENDED,0);
m_pThread->SetOwner(this);
m_pThread->ResumeThread();
}
}
int EnumThread::Run()
{
// TODO: Add your specialized code here and/or call the base class
const int STA_LINK=1;
const int STA_LEAVE=2;
const int STA_CLOSE=0;
struct FileStatus{
DWORD fileID;
int Status;
struct FileStatus *Next;
}*RootP=NULL,*tempP=NULL,*newP;
DWORD res=0,i;
DWORD er=0,ta=0;
PFILE_INFO_3 m=NULL,n;
char temp[1024];
CString str;
while(!m_bExit)
{
Sleep(1);
res=NetFileEnum(NULL,NULL,NULL,3,(PBYTE *)&m,MAX_PREFERRED_LENGTH,&er,&ta,NULL);
if(res==0)
{
if(er>0)
{
n=m;
for(i=0;i<er;i++)
{
bool Found=false;
tempP=RootP;
while(tempP)
{
if(tempP->fileID==n->fi3_id)
{
Found=true;
tempP->Status=STA_LINK;
break;
}
tempP=tempP->Next;
}
if(!Found)
{
newP=new struct FileStatus;
newP->fileID=n->fi3_id;
newP->Status=STA_LINK;
newP->Next=NULL;
tempP=RootP;
if(tempP==NULL)
{
RootP=newP;
}
else
{
while(tempP->Next)tempP=tempP->Next;
tempP->Next=newP;
}
WideCharToMultiByte(CP_ACP,
0,
(WORD *)n->fi3_username,
wcslen((WORD *)n->fi3_username)+1,
temp,
1024,
NULL,
NULL);
str=temp;
str+="正在访问";
WideCharToMultiByte(CP_ACP,
0,
(WORD *)n->fi3_pathname,
wcslen((WORD *)n->fi3_pathname)+1,
temp,
1024,
NULL,
NULL);
str+=temp;
m_Owner->m_List.AddString(str);
}
n++;
}
}
tempP=RootP;
while(tempP)
{
if(tempP->Status==STA_LEAVE)
{
if(RootP==tempP)
{
newP=tempP;
tempP=tempP->Next;
RootP=tempP;
newP->Next=NULL;
delete newP;
newP=tempP;
}
else
{
newP->Next=tempP->Next;
tempP->Next=NULL;
delete tempP;
tempP=newP->Next;
}
}
else
{
newP=tempP;
tempP=tempP->Next;
}
}
tempP=RootP;
while(tempP)
{
tempP->Status=STA_LEAVE;
tempP=tempP->Next;
}
NetApiBufferFree(m);
}
}
return 0;
} 运行成功 但是m_list文本框中没有东西,调试没有进入run
{
// TODO: Add extra validation here
//CDialog::OnOK();
if(m_pThread==NULL)
{
m_pThread=(EnumThread *)AfxBeginThread(RUNTIME_CLASS(EnumThread),0,0,CREATE_SUSPENDED,0);
m_pThread->SetOwner(this);
m_pThread->ResumeThread();
}
}
int EnumThread::Run()
{
// TODO: Add your specialized code here and/or call the base class
const int STA_LINK=1;
const int STA_LEAVE=2;
const int STA_CLOSE=0;
struct FileStatus{
DWORD fileID;
int Status;
struct FileStatus *Next;
}*RootP=NULL,*tempP=NULL,*newP;
DWORD res=0,i;
DWORD er=0,ta=0;
PFILE_INFO_3 m=NULL,n;
char temp[1024];
CString str;
while(!m_bExit)
{
Sleep(1);
res=NetFileEnum(NULL,NULL,NULL,3,(PBYTE *)&m,MAX_PREFERRED_LENGTH,&er,&ta,NULL);
if(res==0)
{
if(er>0)
{
n=m;
for(i=0;i<er;i++)
{
bool Found=false;
tempP=RootP;
while(tempP)
{
if(tempP->fileID==n->fi3_id)
{
Found=true;
tempP->Status=STA_LINK;
break;
}
tempP=tempP->Next;
}
if(!Found)
{
newP=new struct FileStatus;
newP->fileID=n->fi3_id;
newP->Status=STA_LINK;
newP->Next=NULL;
tempP=RootP;
if(tempP==NULL)
{
RootP=newP;
}
else
{
while(tempP->Next)tempP=tempP->Next;
tempP->Next=newP;
}
WideCharToMultiByte(CP_ACP,
0,
(WORD *)n->fi3_username,
wcslen((WORD *)n->fi3_username)+1,
temp,
1024,
NULL,
NULL);
str=temp;
str+="正在访问";
WideCharToMultiByte(CP_ACP,
0,
(WORD *)n->fi3_pathname,
wcslen((WORD *)n->fi3_pathname)+1,
temp,
1024,
NULL,
NULL);
str+=temp;
m_Owner->m_List.AddString(str);
}
n++;
}
}
tempP=RootP;
while(tempP)
{
if(tempP->Status==STA_LEAVE)
{
if(RootP==tempP)
{
newP=tempP;
tempP=tempP->Next;
RootP=tempP;
newP->Next=NULL;
delete newP;
newP=tempP;
}
else
{
newP->Next=tempP->Next;
tempP->Next=NULL;
delete tempP;
tempP=newP->Next;
}
}
else
{
newP=tempP;
tempP=tempP->Next;
}
}
tempP=RootP;
while(tempP)
{
tempP->Status=STA_LEAVE;
tempP=tempP->Next;
}
NetApiBufferFree(m);
}
}
return 0;
} 运行成功 但是m_list文本框中没有东西,调试没有进入run
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <svrapi.h>const short MAX_ENTRIES = 20;int main(int argc, char FAR * argv[])
{
char FAR * pszServerName = NULL;
short nLevel = 50;
struct file_info_50* pBuf = NULL;
struct file_info_50* pTmpBuf = NULL;
short cbBuffer;
short nEntriesRead = 0;
short nTotalEntries = 0;
short nTotalCount = 0;
int i;
NET_API_STATUS nStatus;
//
// ServerName can be NULL to indicate the local computer.
//
if (argc > 2)
{
printf("Usage: %s [\\\\ServerName]\n", argv[0]);
exit(1);
} if (argc == 2)
pszServerName = argv[1];
//
// Allocate the memory required to receive a maximum of
// 20 file_info_50 structures.
//
cbBuffer = MAX_ENTRIES * sizeof(struct file_info_50); pBuf = malloc(cbBuffer); if (pBuf == NULL)
printf("No memory\n"); // Call the NetFileEnum function to list the
// open files, specifying information level 50.
//
nStatus = NetFileEnum(pszServerName,
NULL,
nLevel,
(char FAR *)pBuf,
cbBuffer,
&nEntriesRead,
&nTotalEntries);
//
// Loop through the entries; process errors.
//
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
for (i = 0; (i < nEntriesRead); i++)
{
assert(pTmpBuf != NULL); if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}
//
// Display the information for each entry retrieved.
//
printf("\tShare: %s\n", pTmpBuf->fi50_sharename);
printf("\n\tPath: %s\n", pTmpBuf->fi50_pathname);
printf("\tUser: %s\n", pTmpBuf->fi50_username);
printf("\tID: %d\n", pTmpBuf->fi50_id); pTmpBuf++;
nTotalCount++;
}
}
}
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Display a warning if the buffer was not large enough
// to contain all available entries.
//
if ((nEntriesRead < nTotalEntries) || (nStatus == ERROR_MORE_DATA))
fprintf(stderr, "Not all entries have been enumerated\n");
//
// Free the allocated memory.
//
if (pBuf != NULL)
free(pBuf); fprintf(stderr, "\nTotal of %d entries enumerated\n", nTotalCount); return 0;
}