Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Acquisire SSID della rete WiFi connessa (Windows)
Forum - C/C++ - Acquisire SSID della rete WiFi connessa (Windows)

Avatar
a_butta (Member)
Expert


Messaggi: 578
Iscritto: 16/03/2010

Segnala al moderatore
Postato alle 14:25
Lunedì, 05/12/2016
Ciao a tutti.

Sto sviluppando un'applicazione con le librerie GTK+ 3 (IDE Eclipse) ma da far girare sotto Windows (dunque non ho bisogno necessariamente di soluzione multipiattaforma).
Non è un'applicazione console, per cui utilizzo il comando -mswindow nel linker per evitare di far apparire la console nel lancio dell'exe. Ho bisogno di una routine di estrema semplicità che mi restituisca l' SSID della rete WiFi a cui si è attualmente connessi (o eventualmente un flag negativo nel caso in cui si sia offline).
La soluzione adottata sinora è quella di invocare da programma C++ il comando
Codice sorgente - presumibilmente Plain Text

  1. netsh wlan show interfaces


e acquisendo il responso come stringa recupero le info che mi servono. Il grave fastidio che ciò comporta è che ogni volta che eseguo un controllo sull'SSID si apre e si chiude una finestra del prompt, e la mia applicazione dovrebbe effettuare questo controllo con una certa frequenza (tra l'altro è pensata per operare in TrayIcon). Soluzioni:
-) C'è qualche modo di sfruttare l'applicativo netsh.exe o equivalente senza aprire una finestra cmd?
-) Qualche idea si soluzioni di diverso tipo? Ho provato a cercare su Internet se vi sono delle API che fanno al caso mio, ma non ho trovato nulla di semplice o che comunque matchi il mio caso (vorrei evitare librerie .NET perchè non vorrei che il programma non fosse compatibile magari su alcuni Windows che non le abbiano).

Qualche aiuto?

Grazie anticipatamente!

Ultima modifica effettuata da a_butta il 05/12/2016 alle 14:27
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 16:58
Lunedì, 05/12/2016
Passa CREATE_NO_WINDOW nel parametro dwCreationFlags di CreateProcess.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms ...
https://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

Purtroppo non penso ci sia una maniera piu' semplice per ignorare la creazione della finestra... (usare system, ad esempio)... Windows e' cosi'.


Il mio blog: https://piero.dev
PM Quote
Avatar
a_butta (Member)
Expert


Messaggi: 578
Iscritto: 16/03/2010

Segnala al moderatore
Postato alle 18:54
Martedì, 06/12/2016
Testo quotato

Postato originariamente da pierotofy:

Passa CREATE_NO_WINDOW nel parametro dwCreationFlags di CreateProcess.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms ...
https://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

Purtroppo non penso ci sia una maniera piu' semplice per ignorare la creazione della finestra... (usare system, ad esempio)... Windows e' cosi'.



Grazie mille Piero! Io stavo usando dei semplici pipe e popen. Con CreateProcess() e il parametro CREATE_NO_WINDOW ottengo quello che serve a me. Non sono un esperto di WinAPI, per cui ammetto di aver preso il codice da Internet relativo al tuo suggerimento e di averlo adattato al mio caso banale. Di seguito il codice nel caso dovesse servire a qualcuno:

Codice sorgente - presumibilmente Delphi

  1. HANDLE hStdOutRead = NULL;
  2.         HANDLE hStdOutWrite = NULL;
  3.         HANDLE hChildStdErrRd = NULL;
  4.         HANDLE hChildStdErrWr = NULL;
  5.        
  6.  
  7. string GetWiFiInfo(void) {
  8.  
  9.     SECURITY_ATTRIBUTES sa;
  10.     // Set the bInheritHandle flag so pipe handles are inherited.
  11.     sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  12.     sa.bInheritHandle = TRUE;
  13.     sa.lpSecurityDescriptor = NULL;
  14.     // Create a pipe for the child process's STDERR.
  15.     if ( ! CreatePipe(&hChildStdErrRd, &hChildStdErrWr, &sa, 0) ) {
  16.         exit(1);
  17.     }
  18.     // Ensure the read handle to the pipe for STDERR is not inherited.
  19.     if ( ! SetHandleInformation(hChildStdErrRd, HANDLE_FLAG_INHERIT, 0) ){
  20.         exit(1);
  21.     }
  22.     // Create a pipe for the child process's STDOUT.
  23.     if ( ! CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0) ) {
  24.         exit(1);
  25.     }
  26.     // Ensure the read handle to the pipe for STDOUT is not inherited
  27.     if ( ! SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0) ){
  28.         exit(1);
  29.     }
  30.     // Create the child process.
  31.     PROCESS_INFORMATION piProcInfo = CreateChildProcess();
  32.  
  33.     // Read from pipe that is the standard output for child process.
  34.     return ReadFromPipe(piProcInfo);
  35. }
  36.  
  37. PROCESS_INFORMATION CreateChildProcess(void) {
  38.     // Set the text I want to run
  39.     char szCmdline[]="netsh wlan show interfaces";
  40.     PROCESS_INFORMATION piProcInfo;
  41.     STARTUPINFO siStartInfo;
  42.     bool bSuccess = FALSE;
  43.  
  44.     // Set up members of the PROCESS_INFORMATION structure.
  45.     ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
  46.  
  47.     // Set up members of the STARTUPINFO structure.
  48.     // This structure specifies the STDERR and STDOUT handles for redirection.
  49.     ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
  50.     siStartInfo.cb = sizeof(STARTUPINFO);
  51.     siStartInfo.hStdError = hChildStdErrWr;
  52.     siStartInfo.hStdOutput = hStdOutWrite;
  53.     siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  54.  
  55.     // Create the child process.
  56.     bSuccess = CreateProcess(NULL,
  57.         szCmdline,     // command line
  58.         NULL,          // process security attributes
  59.         NULL,          // primary thread security attributes
  60.         TRUE,          // handles are inherited
  61.                 CREATE_NO_WINDOW,             // creation flags
  62.         NULL,          // use parent's environment
  63.         NULL,          // use parent's current directory
  64.         &siStartInfo,  // STARTUPINFO pointer
  65.         &piProcInfo);  // receives PROCESS_INFORMATION
  66.     CloseHandle(hChildStdErrWr);
  67.     CloseHandle(hStdOutWrite);
  68.     // If an error occurs, exit the application.
  69.     if ( ! bSuccess ) {
  70.         exit(1);
  71.     }
  72.     return piProcInfo;
  73. }
  74.  
  75. string ReadFromPipe(PROCESS_INFORMATION _PROCESS_INFORMATION) {
  76.     DWORD dwRead;
  77.     CHAR chBuf[BUFSIZE];
  78.     bool bSuccess = FALSE;
  79.     std::string out = "";
  80.     for (;;) {
  81.         bSuccess=ReadFile( hStdOutRead, chBuf, BUFSIZE, &dwRead, NULL);
  82.         if( ! bSuccess || dwRead == 0 ) break;
  83.  
  84.         std::string s(chBuf, dwRead);
  85.         out += s;
  86.     }
  87.                
  88.                 // Non uso il controllo sull'Errore!
  89.     return out;
  90. }


PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 23:04
Martedì, 06/12/2016
:k:


Il mio blog: https://piero.dev
PM Quote