如何在c中执行外部程序

时间:2017-02-11 17:28:36

标签: c windows

我想在运行我的代码后启动计算机中的.exe程序,并在程序打开后仍然执行一些操作,但我仍然坚持如何打开它。

 <div id="kc1" class="carousel " data-ride="carousel" data-interval="5000">
 <ol class="carousel-indicators hidden-xs">
 <li data-target="#kc1" data-slide-to="0" class="active"></li>
 <li data-target="#kc1" data-slide-to="1"></li>
 </ol>
 <div class="carousel-inner" role="listbox">         
 <div class="item active" id="websites">
 <a href="websites-systems-trainings.php"><img class="img-responsive" src="img/full-responsive-websites-system-solutions1.jpg";text="Slide+0" alt="Slide 0"></a>
 <div class="carousel-caption img-rounded featurette-heading text-center col-md-offset-1 opa1 onee1"  style="background-color:#004080;">
 <h1 style="font-size:25px;">This is test example and half caption is hiding in left side on mobile device below <=4 inch screen </h1> 
 <h6>
 <a href="websites-systems-trainings.php" class="btn-default btn-sm">More</a> 
 </h6>
 </div>
 </div>
 <div class="item" id="SEO-SEM">
 <a href="seo-sem-ppc.php"> <img class="img-responsive" src="img/seo-sem.jpg";text="Slide+1" alt="Slide 1"></a>
 <div class="carousel-caption img-rounded featurette-heading text-center col-md-offset-2"  style="background-color:#000;">
 <h2> This is test example and half caption is hiding in left side on mobile device below <=4 inch screen</h2>
 <h6>   <a href="seo-sem-ppc.php" class="btn-default btn-sm">More</a> </h6>
 </div>
 </div>
 </div>
 <a class="left carousel-control" href="#kc1" data-slide="prev">
 <span class="glyphicon glyphicon-chevron-left"></span>
 </a>
 <a class="right carousel-control" href="#kc1" data-slide="next">
 <span class="glyphicon glyphicon-chevron-right"></span>
 </a> 
 </div>

 <style>
 <!-- carousel css-->
 .btn-clear {
 color: #FFF;
 border-color: #FFF;
 border-width: 2px;
 margin-right: 15px;
 }
 btn-clear:hover {
 color: #000;
 background-color: #6699CC;
 }
 #kc1
 {
 border-radius: 0px 0px 0px 0px; /* slider.css ln-17*/
 /* added by kk */
 overflow: hidden;
 /* added by kk */
 margin-top: 1px;
 min-height: 300px;
 min-width: 100%;
 }
 #kc1 img {
 min-height: 300px;
 min-width: 100%;       
 }
 #kc1 > .carousel-indicators > li {
 border-radius: 2px;
 min-width: 2px;
 background-color:#D4FF00;
 border: 1px solid black;
 margin-right: 1px;;
 margin-left: 1px;;
 }
 #kc1 > .carousel-indicators > .active {
 background-color:#2AFF00;
 }
 #kc1 .carousel-caption {
 color: blue;   
 right: 50%;
 text-align: center;
 background:#fff;   
 left: auto;
 top:12%;
 bottom: initial;
 transform: translateY(-50%);
 transform:translateX(50%);
 color:#FFF;
 max-width: 1200px; 
 }
 .item {
 -webkit-transform-style: preserve-3d;
 -moz-transform-style: preserve-3d;
 transform-style: preserve-3d;
 -webkit-border-radius: 0;
 -moz-border-radius: 0;
 border-radius: 0;
 -webkit-
 }
 </style> 

由于两个原因,系统功能对我不起作用;它暂停代码直到应用程序退出,当我尝试运行代码时,它说他无法找到C:Riot。

2 个答案:

答案 0 :(得分:0)

有几个问题是字符串"start C:\Riot Games\League of Legends\LeagueClient.exe"

首先,\字符用于转义字符,这意味着如果直接插入字符串中,则输入其他字符。例如,要在字符串中编写",您应该使用\",因为"本身就意味着它是字符串的结尾。同样,\n表示换行符,因为您无法在字符串中直接写入换行符。在这里,\R中的C:\Riot Games表示您正在逃避没有任何意义的R字符。编译器将\R解释为简单R(同样适用于\L),因此将字符串"start C:\Riot Games\League of Legends\LeagueClient.exe"转换为"start C:Riot GamesLeague of LegendsLeagueClient.exe"。要转义\字符,请使用\\。所以到目前为止,字符串应该是system("start C:\\Riot Games\\League of Legends\\LeagueClient.exe")

该字符串还有另一个问题,即命令行中的空格通常意味着您使用空格之前指定的参数在空格之前运行程序。这通常用于打开带有程序的文件。为简单起见,"start C:\\Riot Games\\League of Legends\\LeagueClient.exe"表示您要运行程序C:\Riot并使其打开文件Games\League of Legends\LeagueClient.exe。正如我们之前所说,编译器会将代码中的C:\Riot转换为C:Riot,因此它会尝试运行程序C:Riot,当然它无法找到它,所以它给你你提到的错误。无论如何,要告诉计算机空格实际上是文件名的一部分,您必须在文件名周围加上引号"。如前所述,要在字符串中使用引号,请使用\"。所以正确的方法是system("start \"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"")。此外,start会打开控制台,因此如果您想打开程序本身,只需使用system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"")

您的代码的另一个问题是,true未在C中定义。因此,您应使用while(1)或使用true#define true 1定义为宏

所以正确的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define _WIN32_WINNT 0x0500
#include <windows.h>

#ifndef true    //test if true is defined in case it's already defined somewhere else
#define true 1
#endif

int main () {
    POINT mouse;
    //HWND hWnd = GetConsoleWindow();
    //ShowWindow(hWnd, SW_MINIMIZE); 
    //ShowWindow(hWnd, SW_HIDE);
    // how to open program ?
    system("\"C:\\Riot Games\\League of Legends\\LeagueClient.exe\"");

    while (true) {
        GetCursorPos(&mouse);
        int x = mouse.x;
        int y = mouse.y;
        SetCursorPos(x + rand() % 40 - 20, y + rand() % 40 - 20);
        printf("x = %d ", mouse.x);
        printf("y = %d\n", mouse.y);
        Sleep(1);
    }
}

答案 1 :(得分:0)

使用system()是不安全的,创建进程的好方法是CreateProcess()函数。其他的事情 - system()等待启动的程序停止,并在不并行后执行代码。