窗体应用程序变量定义问题

kenshinbc 2019-02-15 03:45:08
请教一个菜鸟问题,vc++2010,Windows Form程序,我定义了一个char数组在form1.h在上面
#include <WINSOCK2.H>
#include <stdio.h>
#include <string>
#include "stdafx.h"
using namespace std;
#pragma comment(lib,"ws2_32.lib")

#pragma once
extern bool connected,timer_en;
extern SOCKET sockClient;
extern char recvBuf_vb0[100];
char sendvBuf3[31];//就是这个数组变量

该数组sendvBuf3,在form1的那个控件事件里要被赋值和访问,但是为何我赋值不了,写不了值,就是不变。我用断点看了,定义为char就是赋值,数组就不能。求教

...全文
556 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kenshinbc 2019-02-26
  • 打赏
  • 举报
回复
定义在按钮事件内的数组可以赋值,定义在form1最上面的bool变量也可以赋值,但是定义在form1最上面的数组就不能在按钮事件内赋值。。。
kenshinbc 2019-02-15
  • 打赏
  • 举报
回复
#include <WINSOCK2.H>
#include <stdio.h>
#include <string>
#include "stdafx.h"
using namespace std;
#pragma comment(lib,"ws2_32.lib")

#pragma once
extern bool connected,timer_en;
extern SOCKET sockClient;
extern char recvBuf_vb0[100];
char sendvBuf3[31];

namespace s200 {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Form1 摘要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
}
private: System::Windows::Forms::Label^ label1;
public:
System::Windows::Forms::Timer^ timer1;

protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::TextBox^ textBox1;
//private: System::Windows::Forms::Timer^ timer1;
private: System::ComponentModel::IContainer^ components;
protected:

private:
/// <summary>
/// 必需的设计器变量。
/// </summary>


#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button2
//
this->button2->Location = System::Drawing::Point(120, 263);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(139, 60);
this->button2->TabIndex = 0;
this->button2->Text = L"连接读取";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(378, 263);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(139, 60);
this->button3->TabIndex = 1;
this->button3->Text = L"断开退出";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(243, 55);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(119, 21);
this->textBox1->TabIndex = 2;
//
// timer1
//
this->timer1->Interval = 500;
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(158, 58);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(41, 12);
this->label1->TabIndex = 3;
this->label1->Text = L"label1";
//
// Form1
//
this->ClientSize = System::Drawing::Size(674, 376);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Name = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion


private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
WORD wVersionRequested;
WSADATA wsaData;
int err,i;
char recvBuf[100];
//int send_byte0[22]={03,00,00,16,11,E0,00,00,00,01,00,C0,01,0A,C1,02,02,00,C2,02,02,00};
int send_byte1[22]={03,00,00,22,17,224,00,00,00,01,00,192,01,10,193,02,02,00,194,02,02,00};
int send_byte2[25]={03,00,00,25,2,240,128,50,1,00,00,3,00,00,8,00,00,240,00,00,01,00,01,01,224};
int send_byte3[31]={03,00,00,31,2,240,128,50,1,00,00,5,00,00,14,00,00,04,01,18,10,16,02,00,8,00,01,132,00,00,00};
//char send_byte1[66]="03 00 00 16 11 e0 00 00 00 01 00 c0 01 0a c1 02 02 00 c2 02 02 00";
//char send_byte2[81]="03 00 00 19 02 f0 80 32 01 00 00 03 00 00 08 00 00 f0 00 00 01 00 01 01 e0";
//char send_byte3[99]="03 00 00 1f 02 f0 80 32 01 00 00 05 00 00 0e 00 00 04 01 12 0a 10 02 00 08 00 01 84 00 00 00";
char sendvBuf1[22],sendvBuf2[25];
wVersionRequested = MAKEWORD( 1, 1 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}

if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}
sockClient=socket(AF_INET,SOCK_STREAM,0);
for (i=0;i<23;i++){
sendvBuf1[i]=char(send_byte1[i]);
}
for (i=0;i<26;i++){
sendvBuf2[i]=char(send_byte2[i]);
}
for (i=0;i<32;i++){
sendvBuf3[i]=char(send_byte3[i]);
}

SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("10.50.222.90");//服务器端的地址
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(102);
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));


//第一次握手
//for(int i=1;i<=22;i++)
//{itoa(send_int[i],sendvBuf[i],16);
//}
send(sockClient,sendvBuf1,22,0);
recv(sockClient,recvBuf,100,0);
if ((recvBuf[0]=char("3"))&& (recvBuf[3]=char("22")) && (recvBuf[4]=char("17"))){
//第2次握手
send(sockClient,sendvBuf2,25,0);
recv(sockClient,recvBuf,100,0);
if ((recvBuf[0]=char("3"))&& (recvBuf[3]=char("27")) && (recvBuf[4]=char("17"))){
connected=true;
}}
}

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
if (connected){
//读VB0
send(sockClient,sendvBuf3,31,0);
recv(sockClient,recvBuf_vb0,100,0);
this->label1->Text=System::Runtime::InteropServices::Marshal::PtrToStringAnsi((IntPtr)recvBuf_vb0[27]);

}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
closesocket(sockClient);
WSACleanup();
this->~Form1();
}
};
}

kenshinbc 2019-02-15
  • 打赏
  • 举报
回复
编译没问题,但是赋值不了,值不变。就是一些数字,初始化以后就一直是那些值。
qybao 2019-02-15
  • 打赏
  • 举报
回复
报什么错了?debug看看sendvBuf3内存里都是什么值
kenshinbc 2019-02-15
  • 打赏
  • 举报
回复
哦,后面有一个按钮事件触发这个赋值

int send_byte3[31]={03,00,00,31,2,240,128,50,1,00,00,5,00,00,14,00,00,04,01,18,10,16,02,00,8,00,01,132,00,00,00};

for (i=0;i<32;i++){
sendvBuf3[i]=char(send_byte3[i]);
}
qybao 2019-02-15
  • 打赏
  • 举报
回复
你这么赋值的,不会是直接等吧?你要strcpy或者memset

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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