Shellexec über Consolapplikatons parameter



  • Hallo Forum,

    Ich möchte mich zur Zeit mal etwas tiefer mit C++ beschäftigen und dachte mir am besten lernt man programmieren durch programmieren (und ein gutes Buch + noch besseres Forum) Also habe ich mir (so dachte ich mir jedenfalls bisher einfach mal ein Problem herausgepickt...) Doch nach tagelanger Suche bin ich nun fast am verzweifeln. So nun genug geschwafelt.

    Win XP mit VC++ / Consolapplication (non Unicode)

    Problem:

    Ich übergebe einer demo.exe einen Parameter z.b. test und möchte nach Verarbeitung der Eingabe (es soll dem eingegebenen Wert immer der gleiche Teil angefügt werden und damit die Shellexec ausgeführt werden.

    Mein derzeitige Code:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include "shellapi.h"
    #include <tchar.h> 
    
    using namespace std;
    
    int main (int argc, const char *argv[])
    
    {
    	// wurde ein parameter uebergeben
    	// nein
    	if (argc < 2) {
    		cout << "usage" << endl;
    	// ja
    	} else {
    	// start kommando
    	LPSTR first = LPSTR(argv[1]);
    	LPSTR startcommand = "C:\\Programme\\notepad++\\notepad++.exe ";
    
    	/////
    	HINSTANCE hInst = ShellExecute(0,                           
                                       "open",                      // Operation to perform
                                       startcommand,  // Application name
                                       first,           // Additional parameters
                                       0,                           // Default directory
                                       SW_SHOW);
        if(reinterpret_cast<int>(hInst) <= 32)
        {
          // Could not start application
          switch(reinterpret_cast<int>(hInst))
          {
            case 0:
              // The operating system is out of memory or resources.
              break;
    
            case ERROR_FILE_NOT_FOUND:
              // The specified file was not found.
              break;
    
            case ERROR_PATH_NOT_FOUND:
              // The specified path was not found.
              break;
    
            case ERROR_BAD_FORMAT:
              // The .exe file is invalid (non-Microsoft Win32 .exe or error in .exe image).
              break;
    
            case SE_ERR_ACCESSDENIED:
              // The operating system denied access to the specified file.
              break;
    
            case SE_ERR_ASSOCINCOMPLETE:
              // The file name association is incomplete or invalid.
              break;
    
            case SE_ERR_DDEBUSY:
              // The Dynamic Data Exchange (DDE) transaction could not be completed because
              // other DDE transactions were being processed.
              break;
    
            case SE_ERR_DDEFAIL:
              // The DDE transaction failed.
              break;
    
            case SE_ERR_DDETIMEOUT:
              // The DDE transaction could not be completed because the request timed out.
              break;
    
            case SE_ERR_DLLNOTFOUND:
              // The specified dynamic-link library (DLL) was not found.
    
            case SE_ERR_NOASSOC:
              // There is no application associated with the given file name extension.
              // This error will also be returned if you attempt to print a file that is
              // not printable.
              break;
    
            case SE_ERR_OOM:
              // There was not enough memory to complete the operation.
              break;
    
            case SE_ERR_SHARE:
              // A sharing violation occurred.
              break;
          }
        }
    
    	}
    return 0;
    
    }
    

    Funktioniert auch soweit ohne Probleme. Ich möchte aber jetzt im Programm dem eingegebenen Parameter noch einen festen Teil voranstellen.

    So nach dem Motto:
    string zusatz ="festerzusatz";
    first = zusatz + first;

    Geht aber leider nicht gibt es immer nur Mecker wegen Konvertierung von sting in LPSTR. 😞

    Bin für jeden Tipp dankbar.

    Gruß,
    Marcus



  • std::string hat keine implizite Umwandlung nach LPTSTR (aka "char*"), da mußt du mit c_str() rangehen (liefert btw einen "const char*"/LPCSTR - aber der reicht ShellExecute auch aus).



  • Hallo...

    Jo, geht vor allem wenn man auch c_str() nutzt und es nicht stunden lang mit c_str versucht und sich dann wundert, wenn es nicht geht.

    Gruß,
    Marcus


Anmelden zum Antworten