_popen nicht in MFC ?



  • _popen funktioniert in VS unter c++ anscheinend nicht ganz korrekt. Jedenfalls wird meine Pipe in einer MFC Dialog Anwendung einfach nicht geöffnet.
    In der Kommandozeile, funktioniert der selbe Code wunderbar wenn ich eine einfache "main" benutze. Kennt jemand den Grund dafür? Ich persönlich find das ziemlich merkwürdig.

    void CSetRouteDlg::OnCheckRoute() 
    {
    	FILE *fpipe = NULL;
    	char *befehl = "e:\\winnt\\system32\\route.exe print";
    	char zeile[250];
    	char *strings[] = {"10.0.0.0", "10.0.1.0", "10.0.2.0" };
    	char nichtvorhanden[250];
    	int netz=-1;
    	int i;
    
    	memset(nichtvorhanden, 0, 250);
    	for(i = 0; i<3; i++)
    	{
    		memset(zeile, 0, 250);
    		fpipe = _popen(befehl, "r");
    		if(fpipe != NULL)
    		{
    			while(!feof(fpipe))
    			{
    				fgets(zeile, 250, fpipe);
    				if(strstr(zeile, strings[i]) == 0)
    				{
    					netz = i;					
    					break;
    				}			
    			}
    			fclose(fpipe);
    			if ( netz < 0)
    			{
    				if(i==0)
    					strcpy(nichtvorhanden, "Fehlende Routen: ");
    				strcat (nichtvorhanden, strings[i]);
    			}
    			else
    				netz=-1;
    		}
    	}
    	if(strlen(nichtvorhanden) == 0)
    	{
    		strcpy(nichtvorhanden, "Alles in Ordnung");
    	}
    	m_strShowMissing = _T(nichtvorhanden);
    	UpdateData(FALSE);
    }
    


  • Hallo,

    du kannst _popen nicht in einer Windows-Applikation benutzen. Dies ist auch deutlich dokumentiert:

    MSDN schrieb:

    Note The _popen function returns an invalid file handle, if used in a Windows program, that will cause the program to hang indefinitely. _popen works properly in a Console application. To create a Windows application that redirects input and output, read the section "Creating a Child Process with Redirected Input and Output" in the Win32 SDK.

    aus:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt__popen.2c_._wpopen.asp

    du mußt also den umständlicheren Weg mit WinAPI-Aufrufen gehen.

    MfG


Anmelden zum Antworten