Autor Beitrag
stigge
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 00:01 
Schon einmal vorab: Ich bin (noch) ein ziemlicher Noob. Wäre aber trotzdem nett, wenn ich eine gute Antwort bekäme.

Also ich habe folgende Frage:

Ich möchte ein Programm schreiben, das wenn man einen Knopf drückt das Programm startet, dessen Pfad in edit1.text steht. Ich weiß allerdings nicht den Code, um ein externes Programm zu starten. Kann mir es jemand anhand eines Beispiels erklären? Danke im voraus!


Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Mi 03.01.2007 um 10:12
andras
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 460

Win XP, Win Vista Home Premium, Ubuntu Dapper Drake
Delphi 2005 Pers
BeitragVerfasst: Mi 03.01.07 00:05 
ausblenden Delphi-Quelltext
1:
2:
3:
uses shellapi;
....
shellExecute(Handle,PChar('open'),PChar('Programmpfad'),nil,nil,SW_NORMAL);
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 12:49 
Danke für die Antwort. Werde es gleich mal ausprobieren!
Allerdings würde mich auch mal interesieren, was das alles heißt in dem Code...?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8549
Erhaltene Danke: 478

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 03.01.07 13:26 
ShellApi ist die Unit, die eingebunden werden muss, damit das funktioniert.

ShellExecute Ist der Befehl, der ein Programm ausführt. Also das, was du machen willst. Die Parameter im einzelnen bring ich auch immer durcheinander. Schau da am besten in der Hilfe nach. Die sorgen dafür was wo wie getan wird. In deinem Fall wird ein Programm geöffnet (open) und das Programmfenster normal angezeigt (SW_NORMAL).

_________________
We are, we were and will not be.
Timosch
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1314

Debian Squeeze, Win 7 Prof.
D7 Pers
BeitragVerfasst: Mi 03.01.07 13:40 
Dazu findet man auch jede Menge mit der Suche... :D

_________________
If liberty means anything at all, it means the right to tell people what they do not want to hear. - George Orwell
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 13:46 
Noch eine Erklärung zu dem "PChar(":
In den einfachen Anführungszeichen steht ein String. Der Befehl ShellExecute erwartet jedoch einen PChar. Das ist so etwas ähnliches wie ein String, nur in etwas anderer Form. Windows selbst benutzt in den zur Verfügung gestellten Funktionen eben PChar, deshalb musst du immer die Strings zu PChars casten. Casten bedeutet, dass du einen bestimmten Variablentyp hast (hier einen String) und ihn in einen anderen Typ (hier PChar) umwandeln willst.

Falls du genauer wissen willst, was der Unterschied zwischen String und PChar ist:
www.dsdt.info/grundl.../sprache/strings.php

Zu den Parametern: (ich hab sie mir inzwischen gemerkt ;-))
Als erstes übergibst du das Handle eines Fenster, nämlich deines Fensters von wo du aus den Befehl ausführst.
Als zweites, was mit dem angegebenen Befehl gemacht werden soll. Mit open sagst du, dass "das passieren soll, was bei einem Doppelklick im Windows Explorer passiert".
Nun muss der Befehl an sich übergeben werden, danach die Parameter ('/a /v' zum Beispiel) und dann noch der Ordner, in dem das ganze ausgeführt werden soll. Zum Beispiel kannst du als Befehl cmd.exe (die Kommandozeile also) nehmen. Wenn du dann als Arbeitsordner 'c:\windows' angibst, dann sollte die Kommandozeile mit diesem Ordner als aktuellen Ordner gestartet werden.
Als letztes kommt noch wie die Zielanwendung gestartet werden soll: maximiert, minimiert, versteckt, normal, ...

Hier findest du mehr dazu:
msdn.microsoft.com/l...ons/shellexecute.asp
Das ist die Referenz von Microsoft selbst, für alle Systembefehle kannst du dort am besten nachsehen.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 15:24 
Aha, so das Grobe habe ich jetzt verstanden.
Allerdings funktionierts immer noch nicht so richtig.
Ich kriege immer noch 3 Fehler und zwar 3 Mal ungültige Typenumwandlung.
Liegt's vielleicht daran, das ich Delphi 8 Architekt benutze?
An diesem shellapi kanns nicht liegen, des habe ich ganz sicher richtig eingebaut.
Kann mir nicht jemand den Code erstellen?
Ich möchte einfach nur das es mir bei Druck auf button1 ganz normal das Programm rar.exe im Verzeichnis C :// Winrar / öffnet.

Normal müsste das ja ganz einfach gehen aber ich glaube ich bin trorzdem zu blöd dafür.
Vielleicht blick ich's ja wen ich einen funktionierenden Code bekomme (und wenn ich die Erklärung von jaenicke genau durchgelesen habe^^).

Stigge
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 15:31 
An welcher Stelle deines Codes bekommst du die Typumwandlungsfehler? Den solltest du da immer mit angeben.

Aber ich werds mal mit .NET ausprobieren...
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 15:46 
Ich mache Fortschritte aber ich habe immernoch ein Problem:
Wenn ich auf den Button klicke bekomme ich einen Fehler:
"Ungültige Variantentypkonvertierung der Variante Null in String."
shellapi habe ich eingebunden.
Ich benutze übrigens immer eine VCL-Formularanwendung (falls dies wichtig sein sollte).
Mein Quelltext:
ausblenden Quelltext
1:
2:
3:
4:
5:
uses .... shellapi,....

procedure button1.klick (oder so ähnlich, stimmt aber auf jeden Fall da automatisch erzeugt)

ShellExecute (handle, 'open', '(C://winrar.exe)', NULL, NULL, SW_SHOWNORMAL);


Und was ist jetzt falsch?
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mi 03.01.07 15:51 
ersetz mal "C://" durch "C:\"!
;-)

lg el

//edit:
ausblenden Delphi-Quelltext
1:
shellExecute(Handle,PChar('open'),PChar('C:\Winrar.exe'),nil,nil,SW_NORMAL);					

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.


Zuletzt bearbeitet von elundril am Mi 03.01.07 15:52, insgesamt 1-mal bearbeitet
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 15:52 
user profile iconelundril hat folgendes geschrieben:
ersetz mal "C://" durch "C:\"!
;-)

lg el


Ok ich probiers mal...
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 15:52 
nil nicht NULL! So war es doch oben auch...
Null ist in Delphi .NET ein Variant-Typ, der aber kein String sein kann.

So gehts:
ausblenden Delphi-Quelltext
1:
  ShellExecute(Handle, 'open''cmd.exe''/k dir'nil, SW_NORMAL);					
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 15:53 
user profile iconelundril hat folgendes geschrieben:
ersetz mal "C://" durch "C:\"!
;-)

lg el

//edit:
ausblenden Delphi-Quelltext
1:
shellExecute(Handle,PChar('open'),PChar('C:\Winrar.exe'),nil,nil,SW_NORMAL);					

Der Cast zu PChar ist in Delphi 8 nicht nur unnötig sondern nicht möglich ;-). Auch Variablen mit + zu verketten geht ohne den Cast. Und nur ohne!
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 15:58 
Mal diesen Quelltext ausprobieren....
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mi 03.01.07 15:58 
*grml* ich mag delphi 8 nicht!

lg el

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 16:04 
elundril: Ich auch nicht mehr^^.
jaenicke: Rekord an Fehlern^^. 2 Mal die Typenumwandlung und einmal "es gibt keine überladene version von shellexecute, die man mit diesen argumenten aufrufen könnte".



Ich verzweifel langsam.............
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 16:06 
Ähh, ich hab es hier in Delphi 8 ausprobiert... Es geht!! Kann es sein, dass du den Quelltext aus dem Zitat anstatt meinem genommen hast?

Meiner ist das:
user profile iconjaenicke hat folgendes geschrieben:
So gehts:
ausblenden Delphi-Quelltext
1:
  ShellExecute(Handle, 'open''cmd.exe''/k dir'nil, SW_NORMAL);					
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 16:06 
Ohne diese vielen PChar habe ich viel weniger Fehler...

EDIT:

Oh, stimmt...falscher Text^^
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 03.01.07 16:09 
Ich habe doch schon geschrieben, dass der Cast in Delphi 8 nicht nur unnötig sondern unmöglich ist...

user profile iconjaenicke hat folgendes geschrieben:
Der Cast zu PChar ist in Delphi 8 nicht nur unnötig sondern nicht möglich ;-). Auch Variablen mit + zu verketten geht ohne den Cast. Und nur ohne!
stigge Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 426

WinXP
Delphi 2007
BeitragVerfasst: Mi 03.01.07 16:17 
user profile iconjaenicke hat folgendes geschrieben:
Ähh, ich hab es hier in Delphi 8 ausprobiert... Es geht!! Kann es sein, dass du den Quelltext aus dem Zitat anstatt meinem genommen hast?

Meiner ist das:
user profile iconjaenicke hat folgendes geschrieben:
So gehts:
ausblenden Delphi-Quelltext
1:
  ShellExecute(Handle, 'open''cmd.exe''/k dir'nil, SW_NORMAL);					


Danke Mann, es funktioniert!
Endlich hab ich's geblickt!
Und sorry für die Umstände!

Bis zum nächsten Mal (und das ist wahrscheinlich bald^^),


Stigge