Usuário com melhor resposta
Pegar IP e checar MySQL

Pergunta
-
Como pegar IP da rede da pessoa que iniciar um programa em C++?
Eu estou criando um programa que quando o usuário abrir ele, o ip dele é pego e é verificado no banco de dados se existe algum registro com o IP, se tiver ele abre o site que está registrado com o IP dele.
Meu código é (Windows Form C++)
MyForm.h
#pragma once #include <stdio.h> #include <windows.h> namespace GE { 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 MySql::Data::MySqlClient; /// <summary> /// Summary for MyForm /// </summary> public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~MyForm() { if (components) { delete components; } } private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::TextBox^ textBox1; protected: private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->SuspendLayout(); // // button1 // this->button1->Location = System::Drawing::Point(80, 145); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(84, 26); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // // textBox1 // this->textBox1->Location = System::Drawing::Point(41, 43); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(185, 20); this->textBox1->TabIndex = 1; // // MyForm // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(254, 205); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); this->Name = L"MyForm"; this->Text = L"MyForm"; this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { String^ constring = L"datasource=localhost;port=3306;username=root;password="; MySqlConnection^ conDataBase = gcnew MySqlConnection(constring); MySqlCommand ^cmdDataBase = gcnew MySqlCommand("SELECT * FROM `a`.`sites` WHERE IP='" + "FUNÇÃO QUE PEGA O IP" + "';", conDataBase); MySqlDataReader ^myReader; try { conDataBase->Open(); myReader = cmdDataBase->ExecuteReader(); while (myReader->Read()) { System::Diagnostics::Process::Start(myReader->GetString("SiteDivulgado")); } } catch (Exception^ ex) { MessageBox::Show(ex->Message); } } }; }
MyForm.cpp
#include "MyForm.h" using namespace System; using namespace System::Windows::Forms; using namespace MySql::Data::MySqlClient; [STAThread] void main(array<String^>^ args) { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); AX::MyForm form; Application::Run(%form); }
- Editado xx111111 sábado, 22 de novembro de 2014 20:12
Respostas
-
Veja se funciona:
//Example: b1 == 192, b2 == 168, b3 == 0, b4 == 100 struct IPv4 { unsigned char b1, b2, b3, b4; }; bool getMyIP(IPv4 & myIP) { char szBuffer[1024]; #ifdef WIN32 WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); if(::WSAStartup(wVersionRequested, &wsaData) != 0) return false; #endif if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR) { #ifdef WIN32 WSACleanup(); #endif return false; } struct hostent *host = gethostbyname(szBuffer); if(host == NULL) { #ifdef WIN32 WSACleanup(); #endif return false; } //Obtain the computer's IP myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1; myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2; myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3; myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4; #ifdef WIN32 WSACleanup(); #endif return true; }
Att,
Ricardo Cortes Microsoft Contingent Staff
Esse contedo e fornecido sem garantias de qualquer tipo, seja expressa ou implicita.
MSDN Community Support
- Marcado como Resposta xx111111 sábado, 22 de novembro de 2014 22:54
Todas as Respostas
-
-
Veja se funciona:
//Example: b1 == 192, b2 == 168, b3 == 0, b4 == 100 struct IPv4 { unsigned char b1, b2, b3, b4; }; bool getMyIP(IPv4 & myIP) { char szBuffer[1024]; #ifdef WIN32 WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); if(::WSAStartup(wVersionRequested, &wsaData) != 0) return false; #endif if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR) { #ifdef WIN32 WSACleanup(); #endif return false; } struct hostent *host = gethostbyname(szBuffer); if(host == NULL) { #ifdef WIN32 WSACleanup(); #endif return false; } //Obtain the computer's IP myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1; myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2; myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3; myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4; #ifdef WIN32 WSACleanup(); #endif return true; }
Att,
Ricardo Cortes Microsoft Contingent Staff
Esse contedo e fornecido sem garantias de qualquer tipo, seja expressa ou implicita.
MSDN Community Support
- Marcado como Resposta xx111111 sábado, 22 de novembro de 2014 22:54
-
-
Boa tarde,
Você quer somente pegar o ip ou quer aplicar alguma função também?
Att,
Ricardo Cortes Microsoft Contingent Staff
Esse contedo e fornecido sem garantias de qualquer tipo, seja expressa ou implicita.
MSDN Community Support
quero pegar o IP Externo dele e depois fazer uma verificação no Banco de Dados pra ver se existe algum registro com esse IP.
O código
String^ constring = L"datasource=localhost;port=3306;username=root;password="; MySqlConnection^ conDataBase = gcnew MySqlConnection(constring); MySqlCommand ^cmdDataBase = gcnew MySqlCommand("SELECT * FROM `a`.`registers` WHERE IP='" + FUNÇÃO PRA PEGAR O IP DO USUÁRIO + "';", conDataBase); MySqlDataReader ^myReader;
Veja se funciona:
//Example: b1 == 192, b2 == 168, b3 == 0, b4 == 100 struct IPv4 { unsigned char b1, b2, b3, b4; }; bool getMyIP(IPv4 & myIP) { char szBuffer[1024]; #ifdef WIN32 WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 0); if(::WSAStartup(wVersionRequested, &wsaData) != 0) return false; #endif if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR) { #ifdef WIN32 WSACleanup(); #endif return false; } struct hostent *host = gethostbyname(szBuffer); if(host == NULL) { #ifdef WIN32 WSACleanup(); #endif return false; } //Obtain the computer's IP myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1; myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2; myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3; myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4; #ifdef WIN32 WSACleanup(); #endif return true; }
Att,
Ricardo Cortes Microsoft Contingent Staff
Esse contedo e fornecido sem garantias de qualquer tipo, seja expressa ou implicita.
MSDN Community Support
- Editado xx111111 sábado, 22 de novembro de 2014 19:56
-