原创 Labwindows/cvi8.5学习日志(48)----执行exe文件

2010-8-12 16:45 5502 11 11 分类: 软件与OS


f8c94e05-283f-4407-975f-3a97dc07d9e1.jpg


//*****************************************************************************
// Labwindows/cvi8.5  执行exe文件
// NI官方自带的例程
// szlihongtao
// 2010-08-12
//***************************************************************************** 
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* FILE:    launchexe.c                                                      */
/*                                                                           */
/* PURPOSE: This example illustrates how to use the Launching Executables    */
/*          extended functions in the Utility Library to launch and terminate*/
/*          applications external to the main CVI application.  When you     */
/*          launch an executable with LaunchExecutableEx, you obtain a handle*/
/*          to its instance.  You can use that handle in                     */
/*          ExecutableHasTerminated to determine whether or not the          */
/*          application is currently running, and in TerminateExecutable to  */
/*          terminate it.  In general, it is best practice not to terminate  */
/*          executables this way, but to allow them to terminate naturally.  */
/*
/*          In this application, we launch an executable and then enable a   */
/*          timer to periodically check its state.  See the function panel   */
/*          documentation for more information on using these functions on   */
/*          different operating systems.
/*                                                                           */
/*---------------------------------------------------------------------------*/ 
#include <cvirte.h>
#include <userint.h>
#include <utility.h>
#include "launchexe.h"


/*---------------------------------------------------------------------------*/
/* Module-globals                                                            */
/*---------------------------------------------------------------------------*/
static int g_exeHandle;


/*---------------------------------------------------------------------------*/
/* This is the application's entry-point.                                    */
/*---------------------------------------------------------------------------*/
int main (int argc, char *argv[])
{
    int panelHandle;
   
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;
    if ((panelHandle = LoadPanel (0, "launchexe.uir", PANEL)) < 0)
        return -1;
    DisplayPanel (panelHandle);
    RunUserInterface ();
    DiscardPanel (panelHandle);
    CloseCVIRTE ();
    return 0;
}       
/*---------------------------------------------------------------------------*/
/* Launch the executable and get a handle to it.                             */
/*---------------------------------------------------------------------------*/
int CVICALLBACK LaunchAppCB (int panel, int control, int event,
                             void *callbackData, int eventData1, int eventData2)
{
    if (event == EVENT_COMMIT)
   {
        char exeFile[MAX_PATHNAME_LEN];
       
        GetCtrlVal (panel, PANEL_FILE, exeFile);   // 待执行的文件名
        if (!LaunchExecutableEx (exeFile, LE_HIDE, &g_exeHandle))   // exe文件正常显示
        {
            SetInputMode (panel, PANEL_LAUNCH, 0);    // Launch 命令按钮变灰
            SetCtrlVal (panel, PANEL_APPRUNNINGLED, 1);   // 指示灯点亮
            SetInputMode (panel, PANEL_TERMINATE, 1);   // 允许 Terminate 命令按钮的使用
            SetCtrlAttribute (panel, PANEL_CHECKAPPTIMER, ATTR_ENABLED, 1);   // 定时器使能
        }
        else
            MessagePopup ("ERROR", "Failed to launch executable!");   // 文件出错,无法打开
    }
    return 0;
}


/*---------------------------------------------------------------------------*/
/* Periodically check to see if the application is still running             */
// 定时检查,看看是否在运行应用程序
/*---------------------------------------------------------------------------*/
int CVICALLBACK CheckAppTimerCB (int panel, int control, int event,
                                 void *callbackData, int eventData1, int eventData2)
{
    if (event == EVENT_TIMER_TICK)
    {
  // 检查应用程序是否在运行中,有的时候应用程序被用户关掉了
        if (ExecutableHasTerminated (g_exeHandle)) // Determines whether an application started with LaunchExecutableEx has terminated.
        {
            SetInputMode (panel, PANEL_LAUNCH, 1);    // 允许 Launch 命令按钮的使用    
            SetCtrlVal (panel, PANEL_APPRUNNINGLED, 0); // 指示灯off
            SetInputMode (panel, PANEL_TERMINATE, 0);   // Terminate 命令按钮变灰  
            SetCtrlAttribute (panel, PANEL_CHECKAPPTIMER, ATTR_ENABLED, 0);   // 定时器禁止   
   
   /* Informs the LabWindows/CVI Utility Library that you no longer intend to use the handle you acquired from LaunchExecutableEx. */
            RetireExecutableHandle (g_exeHandle);
        }
    }
    return 0;
}


/*---------------------------------------------------------------------------*/
/* Terminate the executable explicitly.  This is generally worse practice    */
/* than letting the executable terminate on its own, but is sometimes        */
/* necessary.                                                                */
/*---------------------------------------------------------------------------*/
int CVICALLBACK TerminateAppCB (int panel, int control, int event,
                                void *callbackData, int eventData1, int eventData2)
{
    if (event == EVENT_COMMIT)
        TerminateExecutable (g_exeHandle);  // Terminates an executable if it has not already terminated.
    return 0;
}


/*---------------------------------------------------------------------------*/
/* Allow the user to select an executable to launch.                         */
// 选择待执行的文件名
/*---------------------------------------------------------------------------*/
int CVICALLBACK BrowseCB (int panel, int control, int event,
                          void *callbackData, int eventData1, int eventData2)
{
    if (event == EVENT_COMMIT)
    {
        int  stat;
        char exeFile[MAX_PATHNAME_LEN];
       
        stat = FileSelectPopup ("", "*.exe", "*.exe",
                                "Select an executable to launch...",
                                VAL_SELECT_BUTTON, 0, 1, 1, 0, exeFile);
        if (stat == VAL_EXISTING_FILE_SELECTED)
            SetCtrlVal (panel, PANEL_FILE, exeFile);   // 显示文件名
   }
    return 0;
}
//*****************************************************************************
int CVICALLBACK QuitCB (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    if (event == EVENT_COMMIT)         
 {
   TerminateExecutable (g_exeHandle);  // Terminates an executable if it has not already terminated.
  RetireExecutableHandle (g_exeHandle);


        QuitUserInterface (0);
 }
    return 0;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
11
关闭 站长推荐上一条 /3 下一条