C#调用c++ DLL
参数是一个链表指针,结构体这是C++ 的
struct HostList{
char ahost[16];
char aport[4];
char IsNeedconn;
char Connstate;
char GatherCycle;
char ReconnectTimes;
char ReconnectCycle;
int Type;
int GatherTime;
char data[5120];
struct HostList *next; // Next HostList struct pointer
};
struct HostList * gpStructHostHeaderApp;
gpStructHostHeaderApp = HostHeader;
GetDataFromHMI(gpStructHostHeaderApp);
在C#是要调用 GetDataFromHMI(gpStructHostHeaderApp);
这个在C#中要怎样声明C++相同的结构和怎样调用并返回结构指针 --------------------编程问答-------------------- public class Pscollector
{
[DllImport("PsCollector.dll",EntryPoint = "GetDataFromHMI")]
public unsafe extern static HostList GetDataFromHMI(ref HostList hostlist);
}
public class HostList
{
public string _ahost;
string _aport ;
byte _isNeedconn;
byte _connState;
byte _gatherCycle;
byte _reconnectTimes;
byte _reconnectCycle;
Int32 _type;
Int32 _gatherTime;
string data ;
HostList next;
public HostList Next
{
get
{
return next;
}
set
{
next = value;
}
}
public string Data
{
get
{
return data;
}
set
{
data = value;
}
}
public string Host
{
get
{
return _ahost;
}
set
{
_ahost = value;
}
}
public string Port
{
get
{
return _aport;
}
set
{
_aport = value;
}
}
public byte Gathercycle
{
get
{
return _gatherCycle;
}
set
{
_gatherCycle = value;
}
}
public byte Needconn
{
get
{
return _isNeedconn;
}
set
{
_isNeedconn = value;
}
}
public byte ConnState
{
get
{
return _connState;
}
set
{
_connState = value;
}
}
public byte ReconnTimes
{
get
{
return _reconnectTimes;
}
set
{
_reconnectTimes = value;
}
}
public byte ReconnCycle
{
get
{
return _reconnectCycle;
}
set
{
_reconnectCycle = value;
}
}
public Int32 Type
{
get
{
return _type;
}
set
{
_type = value;
}
}
public Int32 GatherTime
{
get
{
return _gatherTime;
}
set
{
_gatherTime = value;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
hostList.Host = "192.168.0.250";
hostList.Port = "6666";
hostList.Needconn = 1;
hostList.ConnState = 1;
hostList.Gathercycle = 5;
hostList.ReconnCycle = 1;
hostList.ReconnTimes = 1;
hostList.Type = 0;
hostList.GatherTime = 0;
hostList.Next = null;
Pscollector.GetDataFromHMI(ref hostList);
textBox1.Text = hostList.Data;
}
}
我这样调用以后 运行出现了 "尝试读取或写入受保护的内存。这通常指示其他内存已损坏。" 请问这个该怎么调用 --------------------编程问答--------------------
[StructLayout(LayoutKind.Sequential)]
struct HostList
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
byte[] ahost;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
byte[] aport;
byte IsNeedconn;
byte Connstate;
byte GatherCycle;
byte ReconnectTimes;
byte ReconnectCycle;
int Type;
int GatherTime;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5120)]
byte[] data;
IntPtr next;
}
[DllImport("dllpath")]
private extern void GetDataFromHMI(ref HostList value);
HostHeader 是什么 --------------------编程问答-------------------- GetDataFromHMI 函数 C++ 中返回值是什么? --------------------编程问答--------------------
HostHeader就是结构体的头指针返回的也是这个指针 --------------------编程问答-------------------- GetDataFromHMI(gpStructHostHeaderApp);
没有返回值,要的数据就是HostHeader指针地址 --------------------编程问答-------------------- 感谢avphoenixi
结构体中的IntPtr next 指向空要怎样赋值,直接hostList.next = null 报错,调用时直接把结构传过去吗?
hostList.ahost = "192.168.0.250";
hostList.aport = "6666";
hostList.IsNeedconn = 1;
hostList.Connstate = 1;
hostList.GatherCycle = 5;
hostList.ReconnectCycle = 1;
hostList.ReconnectTimes = 1;
hostList.Type = 0;
hostList.GatherTime = 0;
hostList.next = null ;
Pscollector.GetDataFromHMI(ref hostList);
Pscollector.GetDataFromHMI(ref hostList)这句要怎样调用,不能用ref hostList报错 --------------------编程问答--------------------
[StructLayout(LayoutKind.Sequential)]--------------------编程问答-------------------- 非常感谢avphoenixi 这么详细的帮助我,现在运行时
unsafe struct HostList
{
public fixed byte ahost[16];
public fixed byte aport[4];
public byte IsNeedconn;
public byte Connstate;
public byte GatherCycle;
public byte ReconnectTimes;
public byte ReconnectCycle;
public int Type;
public int GatherTime;
public fixed byte values[5120];
public HostList* next;
}
[DllImport("PsCollector.dll", EntryPoint = "GetDataFromHMI")]
unsafe extern static void GetDataFromHMI(HostList* hostlist);
private unsafe void button1_Click(object sender, EventArgs e)
{
HostList hl;
char[] host = "192.168.0.250".ToCharArray();
fixed (char* cp = host)
{
Encoding.ASCII.GetBytes(cp, host.Length, hl.ahost, 16);
}
char[] port = "6666".ToCharArray();
fixed (char* cp = port)
{
Encoding.ASCII.GetBytes(cp, host.Length, hl.aport, 4);
}
hl.IsNeedconn = 1;
hl.Connstate = 1;
hl.GatherCycle = 5;
hl.ReconnectTimes = 1;
hl.ReconnectCycle = 1;
hl.Type = 0;
hl.GatherTime = 0;
GetDataFromHMI(&hl);
var data = new byte[5120];
Marshal.Copy((IntPtr)hl.values, data, 0, data.Length);
textBox1.Text = Encoding.ASCII.GetString(data);
}
fixed (char* cp = port)
{
Encoding.ASCII.GetBytes(cp, host.Length, hl.aport, 4);
}
运行到这里时出错提示
输出字节缓冲区太小,无法包含编码后的数据,编码“US-ASCII”的操作回退“System.Text.EncoderReplacementFallback”。
参数名: bytes --------------------编程问答-------------------- 运行到这名时
GetDataFromHMI(&hl);
出错提示
对 PInvoke 函数“MesCollector!MesCollector.Form1::GetDataFromHMI”的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。 --------------------编程问答-------------------- 在你的C++代码最外层,加上[assembly:CLSCompliant(true)] --------------------编程问答-------------------- 在你的C++代码最外层,加上[assembly:CLSCompliant(true)] --------------------编程问答-------------------- 调试一下,可能是C++代码里面有不符合cls规范的数据类型。GoodLuck! --------------------编程问答-------------------- C++ dll 是别人做的,我们改不了,只给了一个在C++ 中调用这个dll的demo --------------------编程问答--------------------
这个问题,要了解端口传输的约定,是把端口作为字符串还是作为4字节整数? 如果是字符串,端口是5位时就溢出,如果是整数,就改为这样
*((int*)hl.ahost) = 66666;--------------------编程问答-------------------- 调用 GetDataFromHMI(&hl);
这个后能不能直接访问h1结构中的数据,因为这个结构是个单链表 --------------------编程问答--------------------
函数声明时特性里加个设置
[DllImport("PsCollector.dll", EntryPoint = "GetDataFromHMI", CallingConvention= CallingConvention.Cdecl)]--------------------编程问答--------------------
unsafe extern static void GetDataFromHMI(HostList* hostlist);
HostList* next = hl->next;
next->......
试试成员的值是否正常 --------------------编程问答-------------------- avphoenixi 您好
现在运行是没有错误,但取不到数据,能不能在textBox1中直接得到hl.data.
hl.data 怎样转成string --------------------编程问答--------------------
var data = new byte[5120];
Marshal.Copy((IntPtr)hl.values, data, 0, data.Length);
textBox1.Text = Encoding.ASCII.GetString(data);
这步就是转换,如果没值的话你看下上面几个成员赋的值对不对,如果有 C++ 调用代码贴上来看下 --------------------编程问答-------------------- // CollectorDemoDlg.cpp : implementation file
//
[assembly: CLSCompliant(true)]
#include "stdafx.h"
#include "CollectorDemo.h"
#include "CollectorDemoDlg.h"
#define PRIVATE_DATA_LEN_FROM_SERVER 5120
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCollectorDemoDlg dialog
CCollectorDemoDlg::CCollectorDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCollectorDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCollectorDemoDlg)
m_csReceiveInfor = _T("");
m_csIpAddr = _T("");
m_csReceiveInfor2 = _T("");
m_csReceiveInfor3 = _T("");
m_csReceiveInfor4 = _T("");
m_csReceiveInfor5 = _T("");
m_csReceiveInfor6 = _T("");
m_csReceiveInfor7 = _T("");
m_csReceiveInfor8 = _T("");
m_csReceiveInfor9 = _T("");
m_csConnectState0 = _T("");
m_csConnectState1 = _T("");
m_csConnectState2 = _T("");
m_csConnectState3 = _T("");
m_csConnectState4 = _T("");
m_csConnectState5 = _T("");
m_csConnectState6 = _T("");
m_csConnectState7 = _T("");
m_csConnectState8 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
_ConnectionPtr m_pConnection;
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout = 5;///设置超时时间为5秒
if(m_pConnection->State)
m_pConnection->Close(); ///如果已经打开了连接则关闭它
hr = m_pConnection->Open("driver={SQL Server};Server=192.168.0.4;DATABASE=mesdata;UID=sa;PWD=8202208","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CCollectorDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCollectorDemoDlg)
DDX_Text(pDX, IDC_EDIT_RECEIVE, m_csReceiveInfor);
DDX_Text(pDX, IDC_EDIT_IP_ADDR, m_csIpAddr);
DDX_Text(pDX, IDC_EDIT_RECEIVE2, m_csReceiveInfor2);
DDX_Text(pDX, IDC_EDIT_RECEIVE3, m_csReceiveInfor3);
DDX_Text(pDX, IDC_EDIT_RECEIVE4, m_csReceiveInfor4);
DDX_Text(pDX, IDC_EDIT_RECEIVE5, m_csReceiveInfor5);
DDX_Text(pDX, IDC_EDIT_RECEIVE6, m_csReceiveInfor6);
DDX_Text(pDX, IDC_EDIT_RECEIVE7, m_csReceiveInfor7);
DDX_Text(pDX, IDC_EDIT_RECEIVE8, m_csReceiveInfor8);
DDX_Text(pDX, IDC_EDIT_RECEIVE9, m_csReceiveInfor9);
DDX_Text(pDX, IDC_STATIC_CONNECT0, m_csConnectState0);
DDX_Text(pDX, IDC_STATIC_CONNECT1, m_csConnectState1);
DDX_Text(pDX, IDC_STATIC_CONNECT2, m_csConnectState2);
DDX_Text(pDX, IDC_STATIC_CONNECT3, m_csConnectState3);
DDX_Text(pDX, IDC_STATIC_CONNECT4, m_csConnectState4);
DDX_Text(pDX, IDC_STATIC_CONNECT5, m_csConnectState5);
DDX_Text(pDX, IDC_STATIC_CONNECT6, m_csConnectState6);
DDX_Text(pDX, IDC_STATIC_CONNECT7, m_csConnectState7);
DDX_Text(pDX, IDC_STATIC_CONNECT8, m_csConnectState8);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCollectorDemoDlg, CDialog)
//{{AFX_MSG_MAP(CCollectorDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_EN_CHANGE(IDC_EDIT_SEND_COMMAND, OnChangeEditSendCommand)
ON_BN_CLICKED(IDC_BUTTON_EXIT2, OnButtonExit2)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_ADD_IP, OnButtonAddIp)
ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCollectorDemoDlg message handlers
#include "PsCollector.h"
#pragma comment(lib,"PsCollector.lib")
//extern "C"_declspec(dllimport) int fnPsCollector();
char gastring[8192];
struct HostList2{
char ahost[16]; // need connect IP addr
char aport[4]; // port
char IsNeedconn; // is need connect flag. 0 - needn't 1 - need
char Connstate; // connect state. 0 - not connect 1 - connected
char GatherCycle; // gather data cycle
char ReconnectTimes; // reconnect times if connect failed
char ReconnectCycle; // reconnect cycle when the net is disconnected
int Type; // Gather type default value 0
int GatherTime; // The time get the data from server
/*
* when (short)data[0] == 0xbeef,
* (short)data[1] = (2500 + 1) * 2,
* (short)data[2] ~ (short)data[2501] are data at addr 0 ~ 2500
* (short)data[2502] is check data.
*
* when (short)data[0] == 0xfeed,
* (short)data[1] = (data num + check num) * 2,
* (short)data[2] ~ (short)data[(data num + check num)] are :
* (short)data[2]|(short)data[3] -- (short)data[4]|(short)data[5] -- ...
* | | | |
* addr1 data1 addr2 data2
* (short)data[(short)data[1] / 2 + 1] is check data.
*/
char data[5120]; // Private data from server
struct HostList *next; // Next HostList struct pointer
};
--------------------编程问答-------------------- #include "string.h"
#include "stdlib.h"
char *IPlist[10] = {"168.68.74.5", "168.68.74.6", "168.68.74.7", "168.68.74.8",
"168.68.74.9", "168.68.74.10", "168.68.74.11", "168.68.74.12", "168.68.74.13"};
struct HostList * CreateHostList(char **IPList, int Num)
{
struct HostList *Curr = NULL, *Prev = NULL, *Head = NULL;
int i;
for (i = 0; i < Num; i++)
{
Curr = (struct HostList *)calloc(1, sizeof(struct HostList));
sprintf(Curr->ahost, IPList[i]);
sprintf(Curr->aport, "6666");
Curr->IsNeedconn = 1; //need connect
Curr->Connstate = 0; //not connect
Curr->GatherCycle = 5; //5s
Curr->ReconnectTimes = 1;//8; //8 times
Curr->ReconnectCycle = 1;//5; //5mins
Curr->Type = 0; //default
Curr->GatherTime = 0; //default
if (0 == i)
{
Prev = Head = Curr;
}
else
{
Prev->next = Curr;
Prev = Curr;
}
}
return Head;
}
int AddHostListNode(struct HostList *Head, char *IPddr)
{
struct HostList *tmp = Head;
struct HostList *Curr = (struct HostList *)calloc(1, sizeof(struct HostList));
sprintf(Curr->ahost, IPddr);
sprintf(Curr->aport, "6666");
Curr->IsNeedconn = 1; //need connect
Curr->Connstate = 0; //not connect
Curr->GatherCycle = 5; //5s
Curr->ReconnectTimes = 3;//8; //8 times
Curr->ReconnectCycle = 1;//5; //5mins
Curr->Type = 0; //default
Curr->GatherTime = 0; //default
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = Curr;
return 0;
}
int DelHostListNode(struct HostList *Head, char *IPddr)
{
struct HostList *tmp = Head;
struct HostList *Prev = Head;
while (tmp)
{
if (!strncmp(tmp->ahost, IPddr, 16))
{
Prev->next = tmp->next;
free(tmp);
return 0;
}
Prev = tmp;
tmp = tmp->next;
}
return 1;
}
struct HostList *SeekHostListNode(struct HostList *Head, char *IPddr)
{
struct HostList *tmp = Head;
while (tmp)
{
if (!strncmp(tmp->ahost, IPddr, 16))
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
/*
struct HostList * InitHostList(void)
{
char *IPlist[10] = {"168.68.74.5", "168.68.74.6", "168.68.74.7", / *"168.68.74.8",
"168.68.74.9", "168.68.74.10", "168.68.74.11", * /"168.68.74.12", "168.68.74.13"};
struct HostList *Head = CreateHostList(IPlist, 3);//9
return Head;
}
*/
struct HostList * InitHostList(void)
{
char *IPlist[10] = {"192.168.0.250", "192.168.0.249"};
struct HostList *Head = CreateHostList(IPlist, 2);//9
return Head;
}
struct HostList * gpStructHostHeader;
struct HostList * gpStructHostHeaderApp;
int testIpList (void)
{
struct HostList * HostHeader = InitHostList();
gpStructHostHeaderApp = HostHeader;
gpStructHostHeader = HostHeader;
struct HostList * tmp = HostHeader;
struct HostList * seek = NULL;
while(tmp){
printf("Host IP: %s ", tmp->ahost);
printf("ReconnectCycle : %d\n", tmp->ReconnectCycle);
tmp = tmp->next;
}
printf("\n");
/*DelHostListNode(HostHeader, "168.68.74.7");
tmp = HostHeader;
while(tmp){
printf("Host IP: %s ", tmp->ahost);
printf("ReconnectCycle : %d\n", tmp->ReconnectCycle);
tmp = tmp->next;
}
printf("\n");
AddHostListNode(HostHeader, "168.68.74.7");
tmp = HostHeader;
while(tmp){
printf("Host IP: %s ", tmp->ahost);
printf("ReconnectCycle : %d\n", tmp->ReconnectCycle);
tmp = tmp->next;
}
printf("\n");
seek = SeekHostListNode(HostHeader, "168.68.74.8");
printf("seek IP: %s\n", seek->ahost);*/
return 0;
}
--------------------编程问答-------------------- BOOL CCollectorDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//168.68.74.6;168.68.74.7;port:6666
// TODO: Add extra initialization here
//m_csConnectInfor = "ip:168.68.74.6 wait connect!";
//m_csIpList= "168.68.74.6\r\n168.68.74.7";
UpdateData(FALSE);
::SetTimer(m_hWnd,1,3000,NULL);//100
return TRUE; // return TRUE unless you set the focus to a control
}
void CCollectorDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CCollectorDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CCollectorDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCollectorDemoDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
//fnPsCollector();//test dll call
testIpList();
GetDataFromHMI(gpStructHostHeader);//test
}
void CCollectorDemoDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
testIpList();
GetDataFromHMI(gpStructHostHeaderApp);//test
}
void CCollectorDemoDlg::OnChangeEditSendCommand()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
}
void CCollectorDemoDlg::OnButtonExit2()
{
// TODO: Add your control notification handler code here
exit(1);
}
char gastring1[8192];
char gastring2[8192];
char gastring3[8192];
char gastring4[8192];
char gastring5[8192];
char gastring6[8192];
char gastring7[8192];
char gastring8[8192];
char gastring9[8192];
int Trans(char *src, char *des, int lens)
{
int i;
unsigned short *srctmp = (unsigned short *)src;
//short *destmp = (short *)des;
for(i = 0; i < lens / 4; i++)
{
sprintf(&des[i * 10], "%4x:%4x ", srctmp[i * 2], srctmp[i * 2 + 1]);
}
sprintf(&des[i * 10], "\0\n");
return 0;
}
int giInitIp = 0;
void CCollectorDemoDlg::OnTimer(UINT nIDEvent)
{
//0 disconnected,1 send,2 receivestart 3 wait nmin 4 receivefinish
//char *ConState[5] ={"disconnected","connected","receivestart","wait nmin","receivefinish"};
char *ConState[8] ={"disconnected","connect start","send","wait nmin","receivestart","receivefinish"};
if(0 == giInitIp)
{
testIpList();
giInitIp = 1;
GetDataFromHMI(gpStructHostHeaderApp);//test
}
try{
GetDataFromHMI(gpStructHostHeaderApp);//test`
}
catch(...)
{
AfxMessageBox("GetDataFromHMI");
}
struct HostList * pStruHostListCur = gpStructHostHeaderApp;
int iLengthTest = 10;
char *pDataTmp[9];
pDataTmp[0] = gastring1;
pDataTmp[1] = gastring2;
pDataTmp[2] = gastring3;
pDataTmp[3] = gastring4;
pDataTmp[4] = gastring5;
pDataTmp[5] = gastring6;
pDataTmp[6] = gastring7;
pDataTmp[7] = gastring8;
pDataTmp[8] = gastring9;
int i=0;
int iServerNo = 0;
while(pStruHostListCur)
{
char aconstate = pStruHostListCur->Connstate;
char *pDataTmpTransDst = pDataTmp[i];
Trans(pStruHostListCur->data, pDataTmpTransDst, 100);
switch(iServerNo)
{
case 0:
m_csReceiveInfor.Format("%s",pDataTmpTransDst);
m_csConnectState0.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 1:
m_csConnectState1.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
m_csReceiveInfor2.Format("%s",pDataTmpTransDst);
break;
case 2:
m_csReceiveInfor3.Format("%s",pDataTmpTransDst);
m_csConnectState2.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 3:
m_csReceiveInfor4.Format("%s",pDataTmpTransDst);
m_csConnectState3.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 4:
m_csReceiveInfor5.Format("%s",pDataTmpTransDst);
m_csConnectState4.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 5:
m_csReceiveInfor6.Format("%s",pDataTmpTransDst);
m_csConnectState5.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 6:
m_csReceiveInfor7.Format("%s",pDataTmpTransDst);
m_csConnectState6.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 7:
m_csReceiveInfor8.Format("%s",pDataTmpTransDst);
m_csConnectState7.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
case 8:
m_csReceiveInfor9.Format("%s",pDataTmpTransDst);
m_csConnectState8.Format("%s,%s",pStruHostListCur->ahost,ConState[aconstate]);
break;
}
iServerNo++;
pStruHostListCur = pStruHostListCur->next;
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
void CCollectorDemoDlg::OnButtonAddIp()
{
// TODO: Add your control notification handler code here
AddHostListNode(gpStructHostHeaderApp, "168.68.74.7");
GetDataFromHMI(gpStructHostHeader);//test
}
void CCollectorDemoDlg::OnButtonDel()
{
// TODO: Add your control notification handler code here
DelHostListNode(gpStructHostHeaderApp, "168.68.74.7");
GetDataFromHMI(gpStructHostHeader);//test
}
补充:.NET技术 , C#