Code
Code
#include <string>
#include <winsock2.h>
#include "msclr/marshal_cppstd.h"
#include <nlohmann/json.hpp>
#include <fstream>
#include <sstream>
namespace DataConnectorGUI {
protected:
~Form() {
if (components) {
delete components;
}
}
private:
System::Windows::Forms::TabControl^ tabControl;
System::Windows::Forms::TabPage^ gatewayTab;
System::Windows::Forms::Button^ addButton;
System::Windows::Forms::TabPage^ securityTab;
System::Windows::Forms::TabPage^ detailsTab;
System::Collections::Generic::List<System::Windows::Forms::Panel^>^
dynamicPanels;
int addedFieldCount;
Dictionary<int, array<String^>^>^ gatewayData;
System::ComponentModel::Container^ components;
// Gateway Tab for maintain the data form Services(Create, Start, Stop
and Delete)
this->gatewayTab = gcnew System::Windows::Forms::TabPage();
this->gatewayTab->Text = L"Gateway";
this->gatewayTab->BackColor = System::Drawing::Color::White;
// Add Button
this->addButton = gcnew System::Windows::Forms::Button();
this->addButton->Text = L"Add";
this->addButton->Location = System::Drawing::Point(20, 20);
this->addButton->Size = System::Drawing::Size(65, 23);
this->addButton->Click += gcnew System::EventHandler(this,
&Form::OnAddButtonClick);
this->gatewayTab->Controls->Add(this->addButton);
// Security Tab
this->securityTab = gcnew System::Windows::Forms::TabPage();
this->securityTab->Text = L"Security";
this->securityTab->BackColor = System::Drawing::Color::White;
// Details Tab
this->detailsTab = gcnew System::Windows::Forms::TabPage();
this->detailsTab->Text = L"Details";
this->detailsTab->BackColor = System::Drawing::Color::White;
this->tabControl->Controls->Add(this->gatewayTab);
this->tabControl->Controls->Add(this->securityTab);
this->tabControl->Controls->Add(this->detailsTab);
this->Controls->Add(this->tabControl);
this->dynamicPanels = gcnew
System::Collections::Generic::List<System::Windows::Forms::Panel^>();
this->addedFieldCount = 0;
}
#pragma endregion
// Add button event handler
void OnAddButtonClick(System::Object^ sender, System::EventArgs^ e) {
int startX = 30;
int startY = 50 + (addedFieldCount * 35);
if (i == 0) {
textBox->Name = "aliasTextBox";
textBox->Text = L"Gateway" + (addedFieldCount + 1).ToString();
textBox->ReadOnly = true;
textBox->TextChanged += gcnew System::EventHandler(this,
&Form::OnAliasTextChanged);
}
else if (i == 1) {
textBox->Name = "nameTextBox";
textBox->Text = L"Gateway" + (addedFieldCount + 1).ToString();
textBox->TextChanged += gcnew System::EventHandler(this,
&Form::OnNameTextChanged);
}
else if (i == 2) {
textBox->Name = "portTextBox";
panel->Controls->Add(textBox);
}
// Create Button
auto createButton = gcnew System::Windows::Forms::Button();
createButton->Text = L"Create";
createButton->Location = System::Drawing::Point(320, 32);
createButton->Size = System::Drawing::Size(60, 20);
createButton->Tag = textBoxes;
createButton->Click += gcnew System::EventHandler(this,
&Form::OnCreateButtonClick); // OnClick Event
panel->Controls->Add(createButton);
this->gatewayTab->Controls->Add(panel);
this->dynamicPanels->Add(panel);
addedFieldCount++;
}
if (i == 0) {
textBox->Name = "aliasTextBox";
textBox->Text = alias;
textBox->ReadOnly = true;
}
else if (i == 1) {
textBox->Name = "nameTextBox";
textBox->Text = name;
textBox->TextChanged += gcnew System::EventHandler(this,
&Form::OnNameTextChanged);
textBox->ReadOnly = true;
}
else if (i == 2) {
textBox->Name = "portTextBox";
textBox->Text = port;
textBox->ReadOnly = true;
}
panel->Controls->Add(textBox);
}
this->gatewayTab->Controls->Add(panel);
this->dynamicPanels->Add(panel);
addedFieldCount++;
}
// Initialize Winsock
result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
throw std::runtime_error("WSAStartup failed with error: " +
std::to_string(result));
}
// Create a socket
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
WSACleanup();
throw std::runtime_error("Socket creation failed with error: " +
std::to_string(WSAGetLastError()));
}
if (bytesReceived == SOCKET_ERROR) {
closesocket(ConnectSocket);
WSACleanup();
throw std::runtime_error("Receive failed with error: " +
std::to_string(WSAGetLastError()));
}
// Clean up
closesocket(ConnectSocket);
WSACleanup();
return response;
}
CloseServiceHandle(scManager);
return false; // Service does not exist
}
void LoadGatewayConfig() {
try {
std::string handler_ip = "127.0.0.1";
int handler_port = 8084;
std::string sql_query = "SELECT service_name, status, port FROM
services;";
if (response.empty()) {
throw std::runtime_error("Error: Received an empty response
from the server.");
}
std::istringstream stream(response);
std::string service_name, status, port;
int entryCount = 0;
entryCount++;
if (!ServiceExists(service_name)) {
std::cout << "Creating and Starting Service: " <<
service_name << std::endl;
if (entryCount == 0) {
throw std::runtime_error("Error: No valid entries found in the
response.");
}
}
catch (const std::exception& ex) {
MessageBox::Show(gcnew String(ex.what()), "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
}
}
if (!isAdmin) {
// Silent admin privilege escalation using Task Scheduler
method
try {
String^ taskCommand = "schtasks /create
/tn \"SilentAdminTask\" /tr \"" + Application::ExecutablePath + "\" /sc once /st
00:00 /rl highest /f";
ProcessStartInfo^ taskInfo = gcnew
ProcessStartInfo("cmd.exe", "/c " + taskCommand);
taskInfo->WindowStyle = ProcessWindowStyle::Hidden;
taskInfo->UseShellExecute = true;
taskInfo->Verb = "runas"; // Request admin silently
if (!String::IsNullOrEmpty(serviceName)) {
ServiceControllerStatus status = GetServiceStatus(serviceName);
if (String::IsNullOrEmpty(serviceName)) {
MessageBox::Show("Service name is required.", "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
return;
}
if (String::IsNullOrEmpty(serviceName)) {
MessageBox::Show("Service name is required.", "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
return;
}
}
return true;
}
catch (Exception^ ex) {
MessageBox::Show("Failed to restart service: " + ex->Message,
"Error", MessageBoxButtons::OK, MessageBoxIcon::Error);
return false;
}
}
// Get the service name from the panel (e.g., from a TextBox control)
String^ serviceName = nullptr;
for each (Control ^ control in panel->Controls) {
TextBox^ textBox = dynamic_cast<TextBox^>(control);
if (textBox != nullptr && textBox->Name == "nameTextBox") {
serviceName = textBox->Text;
break;
}
}
}
else {
MessageBox::Show("Failed to stop the service.", "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
}
}
if (service->Status == ServiceControllerStatus::Stopped) {
return true; // Successfully stopped
}
else {
return false; // Failed to stop the service
}
}
catch (Exception^ ex) {
MessageBox::Show("Error stopping service: " + ex->Message, "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
return false;
}
}
}
}
}
}
// Get the alias, name, and port from the panel's TextBox controls
String^ alias = nullptr;
String^ name = nullptr;
String^ port = nullptr;
}
catch (const std::exception& ex) {
MessageBox::Show(gcnew String(ex.what()), "Error Deleting from
Database", MessageBoxButtons::OK, MessageBoxIcon::Error);
}
}
// Delete the service from services.msc
bool Form::DeleteService(String^ serviceName) {
try {
// Use 'sc delete' command to delete the service
String^ command = String::Format("sc delete \"{0}\"", serviceName);
return true;
}
else {
MessageBox::Show("Failed to delete service.", "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
return false;
}
}
catch (Exception^ ex) {
MessageBox::Show("Error deleting service: " + ex->Message, "Error",
MessageBoxButtons::OK, MessageBoxIcon::Error);
return false;
}
}
};
}
in the above code service not created in the service.msc but without description
and status and also try to create or start or stop or restart or delete it also not
working please check what is issue in that and resolve all issue and reshare full
code in same structure which is maintained like storing and retriving from db and
according to that run services in service.msc