Oppure

Loading
05/12/16 13:25
a_butta
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
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 05/12/16 13:27
aaa
05/12/16 15:58
pierotofy
Passa CREATE_NO_WINDOW nel parametro dwCreationFlags di CreateProcess.

msdn.microsoft.com/en-us/library/windows/desktop/…(v=vs.85).aspx
msdn.microsoft.com/en-us/library/…(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: piero.dev
06/12/16 17:54
a_butta
Postato originariamente da pierotofy:

Passa CREATE_NO_WINDOW nel parametro dwCreationFlags di CreateProcess.

msdn.microsoft.com/en-us/library/windows/desktop/…(v=vs.85).aspx
msdn.microsoft.com/en-us/library/…(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:

	HANDLE hStdOutRead = NULL;
	HANDLE hStdOutWrite = NULL;
	HANDLE hChildStdErrRd = NULL;
	HANDLE hChildStdErrWr = NULL;
	

string GetWiFiInfo(void) {

    SECURITY_ATTRIBUTES sa;
    // Set the bInheritHandle flag so pipe handles are inherited.
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;
    // Create a pipe for the child process's STDERR.
    if ( ! CreatePipe(&hChildStdErrRd, &hChildStdErrWr, &sa, 0) ) {
        exit(1);
    }
    // Ensure the read handle to the pipe for STDERR is not inherited.
    if ( ! SetHandleInformation(hChildStdErrRd, HANDLE_FLAG_INHERIT, 0) ){
        exit(1);
    }
    // Create a pipe for the child process's STDOUT.
    if ( ! CreatePipe(&hStdOutRead, &hStdOutWrite, &sa, 0) ) {
        exit(1);
    }
    // Ensure the read handle to the pipe for STDOUT is not inherited
    if ( ! SetHandleInformation(hStdOutRead, HANDLE_FLAG_INHERIT, 0) ){
        exit(1);
    }
    // Create the child process.
    PROCESS_INFORMATION piProcInfo = CreateChildProcess();

    // Read from pipe that is the standard output for child process.
    return ReadFromPipe(piProcInfo);
}

PROCESS_INFORMATION CreateChildProcess(void) {
    // Set the text I want to run
    char szCmdline[]="netsh wlan show interfaces";
    PROCESS_INFORMATION piProcInfo;
    STARTUPINFO siStartInfo;
    bool bSuccess = FALSE;

    // Set up members of the PROCESS_INFORMATION structure.
    ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );

    // Set up members of the STARTUPINFO structure.
    // This structure specifies the STDERR and STDOUT handles for redirection.
    ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
    siStartInfo.cb = sizeof(STARTUPINFO);
    siStartInfo.hStdError = hChildStdErrWr;
    siStartInfo.hStdOutput = hStdOutWrite;
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

    // Create the child process.
    bSuccess = CreateProcess(NULL,
        szCmdline,     // command line
        NULL,          // process security attributes
        NULL,          // primary thread security attributes
        TRUE,          // handles are inherited
		CREATE_NO_WINDOW,             // creation flags
        NULL,          // use parent's environment
        NULL,          // use parent's current directory
        &siStartInfo,  // STARTUPINFO pointer
        &piProcInfo);  // receives PROCESS_INFORMATION
    CloseHandle(hChildStdErrWr);
    CloseHandle(hStdOutWrite);
    // If an error occurs, exit the application.
    if ( ! bSuccess ) {
        exit(1);
    }
    return piProcInfo;
}

string ReadFromPipe(PROCESS_INFORMATION _PROCESS_INFORMATION) {
    DWORD dwRead;
    CHAR chBuf[BUFSIZE];
    bool bSuccess = FALSE;
    std::string out = "";
    for (;;) {
        bSuccess=ReadFile( hStdOutRead, chBuf, BUFSIZE, &dwRead, NULL);
        if( ! bSuccess || dwRead == 0 ) break;

        std::string s(chBuf, dwRead);
        out += s;
    }
		
		// Non uso il controllo sull'Errore!
    return out;
}
aaa
06/12/16 22:04
pierotofy
:k:
Il mio blog: piero.dev