CreateFileMapping,SendMessage,XMLDocument

wanlanqiao 2006-03-22 10:36:14
服务器端:
.h
//---------------------------------------------------------------------------

#ifndef NetServerH
#define NetServerH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>

#include "NetEventDetect.h"
#include <msxmldom.hpp>
#include <XMLDoc.hpp>
#include <xmldom.hpp>
#include <XMLIntf.hpp>
#define WM_MYMESSAGE WM_USER+1
# define WM_DATACOMING WM_USER+100
//---------------------------------------------------------------------------
class TNetSend : public TForm
{
__published: // IDE-managed Components
private: // User declarations
COPYDATASTRUCT scds;
public: // User declarations
__fastcall TNetSend(TComponent* Owner);
protected:
virtual void _fastcall WMUser(TMessage &Msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_MYMESSAGE,TMessage,WMUser)
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TNetSend *NetSend;
//---------------------------------------------------------------------------
#endif



.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "NetServer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TNetSend *NetSend;
//---------------------------------------------------------------------------
__fastcall TNetSend::TNetSend(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TNetSend::WMUser(TMessage &Msg)
{
int hFile = FileOpen("NetworkEvent.xml",fmOpenRead);
int FileLength = FileSeek(hFile,0,2);
HANDLE hFilemap=CreateFileMapping((HANDLE)0xFFFFFFFF, //共享文件为文件句柄,共享内存为(HANDLE)0xFFFFFFFF
NULL, //文件映象的安全属性结构,一般为NULL
PAGE_READWRITE, //文件视图的保护类型
0, //高32位
FileLength, //低32位
"SHARED"); //映像名
//返回文件映象的句柄
LPVOID pVoid=MapViewOfFile(hFilemap,FILE_MAP_ALL_ACCESS,0,0,0);//返回数据映射的起始地址

FileSeek(hFile,0,0);
char* pszBuffer = new char[FileLength+1];
FileRead(hFile,pszBuffer,FileLength);
FileClose(hFile);

memset(pVoid,0,FileLength);
strcpy((char *)pVoid,pszBuffer); //Returns a pointer to psz1
HANDLE hDes=FindWindow("TNetInspector","网络事件探测器"); // 对象窗口的名称
SendMessage(hDes, WM_DATACOMING,(WPARAM)FileLength,0);
CloseHandle(hFilemap);
UnmapViewOfFile(pVoid);
}
//----------------------------------------------------------------------------
...全文
155 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanlanqiao 2006-03-22
  • 打赏
  • 举报
回复
客户端:
.cpp
//---------------------------------------------------------------------------
void __fastcall TNetInspector::ComboBox1Change(TObject *Sender)
{
//ShowMessage(ListView1->Column[1]->Caption); //发生时间
//ShowMessage(ListView1->Items->ClassName()); //TListItems
//ShowMessage(ListView1->Columns->Items[2]->Caption);//物理地址
//ShowMessage(ListView1->Items->Item[1]->Caption); //非法连接
//ShowMessage(ListView1->Items->Item[1]->SubItems->Strings[2]); //192.168.1.100
switch(ComboBox1->ItemIndex)
{
case 0:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
break;
case 1:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption != ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
//ListView1->Items->Delete(i);
}
else
{

}
}
break;
case 2:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption!=ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
}
else
{}
}
break;
case 3:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption!=ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
}
else
{}
}
break;
case 4:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption!=ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
}
else
{}
}
break;
case 5:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption!=ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
}
else
{}
}
break;
case 6:
ListView1->Clear();
ShowXML("NetworkEvent.xml");
for(int i=0;i<ListView1->Items->Count;i++)
{
if(ListView1->Items->Item[i]->Caption!=ComboBox1->Text)
{
ListView1->Items->Item[i]->Delete();
}
else
{}
}
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::Button5Click(TObject *Sender)
{
AnsiString AppPath = ExtractFileDir(Application->ExeName);
_di_IXMLNode rootNode,childNode,grandchildNode;
_di_IXMLDocument xmlDetect = NewXMLDocument();
xmlDetect->Version = "1.0";
TXMLDocOptions options;
options<<doNodeAutoIndent;
xmlDetect->SetOptions(options);
xmlDetect->Encoding = "gb2312";

rootNode = xmlDetect->AddChild(WideString("NetEventDetect"));

childNode = rootNode->AddChild(WideString("DetectName"));
childNode->SetText(WideString("NetworkEventDetect"));
for(int i=0;i<ListView1->Items->Count;i++)
{
childNode = rootNode->AddChild(WideString("NetEvent"));
childNode->Attributes["Kind"] = WideString(ListView1->Items->Item[i]->Caption);
childNode->Attributes["OccurTime"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[1]);
childNode->Attributes["PhysicsAdd"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[2]);
childNode->Attributes["Source"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[3]);
childNode->Attributes["ComputerName"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[4]);
childNode->Attributes["DescribeInfo"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[5]);
childNode->Attributes["Area"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[6]);
childNode->Attributes["Department"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[7]);
childNode->Attributes["DeposeInfo"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[8]);
childNode->Attributes["DeposeTime"] = WideString(ListView1->Items->Item[i]->SubItems->Strings[9]);
}
xmlDetect->SaveToFile("newNetEvent.xml");
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::ShowXML(AnsiString fileName)
{
XMLDocument1->LoadFromFile(fileName);
XMLDocument1->Active=true;
ListView1->Items->BeginUpdate();
ListView1->Items->Clear();
_di_IXMLNode node=XMLDocument1->DocumentElement;
_di_IXMLNodeList nodes=node->ChildNodes;
for(int n=1;n<nodes->Count;n++)
{
TListItem* li=ListView1->Items->Add();
node=nodes->Nodes[n]->AttributeNodes->Nodes[0];
li->Caption=nodes->Nodes[n]->AttributeNodes->Nodes[0]->GetText();
for(int j = 1;j<=10;j++)
li->SubItems->Add(nodes->Nodes[n]->AttributeNodes->Nodes[j]->GetText());
ListView1->Items->EndUpdate();
}
}
//----------------------------------------------------------------------------
wanlanqiao 2006-03-22
  • 打赏
  • 举报
回复
客户端:
.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "NetEventDetect.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TNetInspector *NetInspector;
//---------------------------------------------------------------------------
__fastcall TNetInspector::TNetInspector(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::Button1Click(TObject *Sender)
{
HWND hwnd = FindWindow("TNetSend","网络事件服务器");
SendMessage(hwnd,WM_MYMESSAGE,0,0);
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::WMComing(TMessage &msg)
{
HANDLE hFilemap=OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,"SHARED");//返回文件映象的句柄
LPVOID pVoid=MapViewOfFile(hFilemap,FILE_MAP_ALL_ACCESS,0,0,0); //返回数据映射的起始地址
int hFile = FileCreate("D:\\qiao.xml");
FileWrite(hFile,pVoid,msg.WParam);
FileClose(hFile);
CloseHandle(hFilemap);
UnmapViewOfFile(pVoid);

XMLDocument1->LoadFromFile("D:\\qiao.xml");
XMLDocument1->Active=true;
ListView1->Items->BeginUpdate();
ListView1->Items->Clear();
_di_IXMLNode node=XMLDocument1->DocumentElement;
_di_IXMLNodeList nodes=node->ChildNodes;
for(int n=1;n<nodes->Count;n++)
{
TListItem* li=ListView1->Items->Add();
node=nodes->Nodes[n]->AttributeNodes->Nodes[0];
li->Caption=nodes->Nodes[n]->AttributeNodes->Nodes[0]->GetText();
for(int j = 1;j<=10;j++)
li->SubItems->Add(nodes->Nodes[n]->AttributeNodes->Nodes[j]->GetText());
ListView1->Items->EndUpdate();
}
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::Button3Click(TObject *Sender)
{
ListView1->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TNetInspector::Button4Click(TObject *Sender)
{
if(OpenDialog1->Execute())
XMLDocument1->LoadFromFile(OpenDialog1->FileName);
XMLDocument1->Active=true;
ListView1->Items->BeginUpdate();
ListView1->Items->Clear();
_di_IXMLNode node=XMLDocument1->DocumentElement;
_di_IXMLNodeList nodes=node->ChildNodes;
for(int n=1;n<nodes->Count;n++)
{
TListItem* li=ListView1->Items->Add();
// node=nodes->Nodes[n]->AttributeNodes->Nodes[0];
li->Caption=nodes->Nodes[n]->AttributeNodes->Nodes[0]->GetText();
for(int j = 1;j<=10;j++)
li->SubItems->Add(nodes->Nodes[n]->AttributeNodes->Nodes[j]->GetText());
ListView1->Items->EndUpdate();
}
}
//----------------------------------------------------------------------------
wanlanqiao 2006-03-22
  • 打赏
  • 举报
回复
客户端:
.h
//---------------------------------------------------------------------------

#ifndef NetEventDetectH
#define NetEventDetectH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>
#include <msxmldom.hpp>
#include <XMLDoc.hpp>
#include <xmldom.hpp>
#include <XMLIntf.hpp>
#include <Dialogs.hpp>

#define WM_MYMESSAGE WM_USER+1
#define WM_DATACOMING WM_USER+100
//---------------------------------------------------------------------------
class TNetInspector : public TForm
{
__published: // IDE-managed Components
TPanel *Panel1;
TPanel *Panel2;
TComboBox *ComboBox1;
TPanel *Panel3;
TPanel *Panel4;
TListView *ListView1;
TButton *Button1;
TButton *Button3;
TButton *Button4;
TButton *Button5;
TButton *Button6;
TLabel *Label1;
TXMLDocument *XMLDocument1;
TOpenDialog *OpenDialog1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button6Click(TObject *Sender);
void __fastcall ComboBox1Change(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TNetInspector(TComponent* Owner);
void __fastcall ShowXML(AnsiString fileName);
protected:
virtual void __fastcall WMComing(TMessage &msg);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DATACOMING,TMessage,WMComing);
END_MESSAGE_MAP(TForm);
};
//---------------------------------------------------------------------------
extern PACKAGE TNetInspector *NetInspector;
//---------------------------------------------------------------------------
#endif

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧