2 / 2 / 1
Регистрация: 09.01.2015
Сообщений: 383
|
|
|
|
Инкапсуляция
20.04.2021, 03:37. Показов 1368. Ответов 0
Всем привет ! Подскажите как правильно делать инкапсуляцию .h и .cpp . Немного не понимаю синтаксис .
Я пытаюсь создать класс устройства J2534Device . Потом хочу прочитать все устройства из реестра и названия вывести в combobox на форме .
MainForm.h
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
| #include <iostream>
#include "J2534Detect.h"
namespace J2534Viewer {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace std;
/// <summary>
/// Сводка для MainForm
/// </summary>
public ref class MainForm : public System::Windows::Forms::Form
{
public:
MainForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MainForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ComboBox^ cb_J2534List;
protected:
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->cb_J2534List = (gcnew System::Windows::Forms::ComboBox());
this->SuspendLayout();
//
// cb_J2534List
//
this->cb_J2534List->FormattingEnabled = true;
this->cb_J2534List->Location = System::Drawing::Point(12, 12);
this->cb_J2534List->Name = L"cb_J2534List";
this->cb_J2534List->Size = System::Drawing::Size(121, 21);
this->cb_J2534List->TabIndex = 0;
//
// MainForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(337, 262);
this->Controls->Add(this->cb_J2534List);
this->Name = L"MainForm";
this->Text = L"MainForm";
this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e)
{
this->cb_J2534List->DataSource = J2534Detect::ListDevices();
this->cb_J2534List->DisplayMember = "Name";
}
};
} |
|
MainForm.cpp
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
| #include "MainForm.h"
using namespace System;
using namespace System::Windows::Forms;
using namespace J2534Viewer;
[STAThread]
void Main(cli::array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew MainForm());
} |
|
J2534Device.h
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| #include <iostream>
#include <string>
using namespace std;
public class J2534Device
{
string Vendor;
string Name;
string FunctionLibrary;
string ConfigApplication;
int CANChannels;
int ISO15765Channels;
int J1850PWMChannels;
int J1850VPWChannels;
int ISO9141Channels;
int ISO14230Channels;
public:
J2534Device();
void SetVendor(string J2534_Vendor);
string GetVendor();
void SetName(string J2534_Name);
string GetName();
void SetFunctionLibrary(string J2534_FunctionLibrary);
string GetFunctionLibrary();
void SetConfigApplication(string J2534_ConfigApplication);
string GetConfigApplication();
void SetCANChannels(int j2534_CANChannels);
int GetCANChannels();
void SetISO15765Channels(int j2534_ISO15765Channels);
int GetISO15765Channels();
void SetJ1850PWMChannels(int j2534_J1850PWMChannels);
int GetJ1850PWMChannels();
void SetJ1850VPWChannels(int j2534_J1850VPWChannels);
int GetJ1850VPWChannels();
void SetISO9141Channels(int j2534_ISO9141Channels);
int GetISO9141Channels();
void SetISO14230Channels(int j2534_ISO14230Channels);
int GetISO14230Channels();
bool IsCANSupported();
bool IsISO15765Supported();
bool IsJ1850PWMSupported();
bool IsJ1850VPWSupported();
bool IsISO9141Supported();
bool IsISO14230Supported();
}; |
|
J2534Device.cpp
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| #include "J2534Device.h"
J2534Device::J2534Device(){}
void SetVendor(string J2534_Vendor) { Vendor = J2534_Vendor; }
string GetVendor() { return Vendor; }
void SetName(string j2534_Name) { Name = j2534_Name; }
string GetName() { return Name; }
void SetFunctionLibrary(string j2534_FunctionLibrary) { FunctionLibrary = j2534_FunctionLibrary; }
string GetFunctionLibrary() { return FunctionLibrary; }
void SetConfigApplication(string j2534_ConfigApplication) { ConfigApplication = j2534_ConfigApplication; }
string GetConfigApplication() { return ConfigApplication; }
void SetCANChannels(int j2534_CANChannels) { CANChannels = j2534_CANChannels; }
int GetCANChannels() { return CANChannels; }
void SetISO15765Channels(int j2534_ISO15765Channels) { ISO15765Channels = j2534_ISO15765Channels; }
int GetISO15765Channels() { return ISO15765Channels; }
void SetJ1850PWMChannels(int j2534_J1850PWMChannels) { J1850PWMChannels = j2534_J1850PWMChannels; }
int GetJ1850PWMChannels() { return J1850PWMChannels; }
void SetJ1850VPWChannels(int j2534_J1850VPWChannels) { J1850VPWChannels = j2534_J1850VPWChannels; }
int GetJ1850VPWChannels() { return J1850VPWChannels; }
void SetISO9141Channels(int j2534_ISO9141Channels) { ISO9141Channels = j2534_ISO9141Channels; }
int GetISO9141Channels() { return ISO9141Channels; }
void SetISO14230Channels(int j2534_ISO14230Channels) { ISO14230Channels = j2534_ISO14230Channels; }
int GetISO14230Channels() { return ISO14230Channels; }
bool IsCANSupported() { return CANChannels > 0; }
bool IsISO15765Supported() { return ISO15765Channels > 0; }
bool IsJ1850PWMSupported() { return J1850PWMChannels > 0; }
bool IsJ1850VPWSupported() { return J1850VPWChannels > 0; }
bool IsISO9141Supported() { return ISO9141Channels > 0; }
bool IsISO14230Supported() { return ISO14230Channels > 0; } |
|
J2534Detect.h
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #include <iostream>
#include <list>
#include "J2534Device.h"
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;
using namespace std;
using namespace Microsoft::Win32;
public class J2534Detect
{
private:
//const String PASSTHRU_REGISTRY_PATH = "Software\\PassThruSupport.04.04";
//const String PASSTHRU_REGISTRY_PATH_6432 = "Software\\Wow6432Node\\PassThruSupport.04.04";
static list<J2534Device> j2534Devices;
public:
static list<J2534Device> ListDevices();
}; |
|
J2534Detect.cpp
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| #include "J2534Detect.h"
static list<J2534Device> ListDevices()
{
if (j2534Devices.empty())
{
return j2534Devices;
}
j2534Devices.clear();
RegistryKey^ localMachine;
localMachine = Registry::LocalMachine;
localMachine->OpenSubKey("Software\\PassThruSupport.04.04", false);
if (!localMachine)
{
localMachine->OpenSubKey("Software\\Wow6432Node\\PassThruSupport.04.04", false);
if (!localMachine)
return j2534Devices;
}
localMachine->GetSubKeyNames();
cli::array<System::String^, 1>^ devices = localMachine->GetSubKeyNames();
for each(String^ device in devices)
{
J2534Device tempDevice;
RegistryKey^ deviceKey = localMachine->OpenSubKey(device);
if (!deviceKey)
continue;
tempDevice.SetVendor = deviceKey->GetValue("Vendor", "");
tempDevice.SetName = deviceKey->GetValue("Name", "");
tempDevice.SetConfigApplication = deviceKey->GetValue("ConfigApplication", "");
tempDevice.SetFunctionLibrary = deviceKey->GetValue("FunctionLibrary", "");
tempDevice.SetCANChannels = deviceKey->GetValue("CAN", 0);
tempDevice.SetISO15765Channels = deviceKey->GetValue("ISO15765", 0);
tempDevice.SetJ1850PWMChannels = deviceKey->GetValue("J1850PWM", 0);
tempDevice.SetJ1850VPWChannels = deviceKey->GetValue("J1850VPW", 0);
tempDevice.SetISO9141Channels = deviceKey->GetValue("ISO9141", 0);
tempDevice.SetISO14230Channels = deviceKey->GetValue("ISO14230", 0);
j2534Devices.push_back(tempDevice);
if (deviceKey) deviceKey->Close();
}
if (localMachine) localMachine->Close();
return j2534Devices;
} |
|
0
|