CVI调用外部程序

#include <USERINT.H>    
#include "CVIShell.h"    
#include <WINDOWS.H>            
#include <SHELLAPI.H>    
#include <UTILITY.H>    
//#include <WINUSER.H>    
//#include <CVIRTE.H>           
//#include <USERINT.H>    
#include "d:\CVI例程\CVIShell的六种调用例程\CVIShell.h"   

/*  
typedef struct _SHELLEXECUTEINFOA  
{  
DWORD cbSize;  
ULONG fMask;  
HWND hwnd;  
LPCSTR   lpVerb;  
LPCSTR   lpFile;  
LPCSTR   lpParameters;  
LPCSTR   lpDirectory;  
int nShow;  
HINSTANCE hInstApp;  
// Optional fields  
LPVOID lpIDList;  
LPCSTR   lpClass;  
HKEY hkeyClass;  
DWORD dwHotKey;  
union {  
HANDLE hIcon;  
HANDLE hMonitor;  
} DUMMYUNIONNAME;  
HANDLE hProcess;  
} SHELLEXECUTEINFOA, *LPSHELLEXECUTEINFOA; 

ShellExecuteExA(  
LPSHELLEXECUTEINFOA lpExecInfo  
);  
SHSTDAPI_(HINSTANCE) ShellExecuteA(  
HWND hwnd,   
LPCSTR lpOperation,   
LPCSTR lpFile,   
LPCSTR lpParameters,   
LPCSTR lpDirectory,   
INT nShowCmd  
); 

CreatePipe(  
OUT PHANDLE hReadPipe,  
OUT PHANDLE hWritePipe,  
IN LPSECURITY_ATTRIBUTES lpPipeAttributes,  
IN DWORD nSize  
); 

CreateProcessA(  
IN LPCSTR lpApplicationName,  
IN LPSTR lpCommandLine,  
IN LPSECURITY_ATTRIBUTES lpProcessAttributes,  
IN LPSECURITY_ATTRIBUTES lpThreadAttributes,  
IN BOOL bInheritHandles,  
IN DWORD dwCreationFlags,  
IN LPVOID lpEnvironment,  
IN LPCSTR lpCurrentDirectory,  
IN LPSTARTUPINFOA lpStartupInfo,  
OUT LPPROCESS_INFORMATION lpProcessInformation  
); 

WinExec(  
IN LPCSTR lpCmdLine,  
IN UINT uCmdShow  
); 

FindWindowA(  
IN LPCSTR lpClassName,  
IN LPCSTR lpWindowName  
); 

SetParent(  
IN HWND hWndChild,  
IN HWND hWndNewParent  
);  
*/  

static int handle;   
static int panelHandle;   
void WinExecAndWait32(char * FileName, int ShowMode);  

void WinExecAndWait32(char * FileName, int ShowMode)   
{   
SECURITY_ATTRIBUTES sa;   
HANDLE hReadPipe;   
HANDLE hWritePipe;   
STARTUPINFOA StartupInfo;   
PROCESS_INFORMATION ProcessInfo;   
DisableBreakOnLibraryErrors (); //关闭异常显示    
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));   
sa.nLength = sizeof(SECURITY_ATTRIBUTES);   
sa.bInheritHandle = TRUE;   
sa.lpSecurityDescriptor = NULL;   
if (CreatePipe(&hReadPipe, &hWritePipe, &sa, 0)) {   
 memset(&StartupInfo, 0, sizeof(STARTUPINFOA));   
 StartupInfo.cb = sizeof(STARTUPINFOA);   
 StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;   
 StartupInfo.wShowWindow = ShowMode;   
 StartupInfo.hStdOutput = hWritePipe;   
 StartupInfo.hStdError = hWritePipe;   
 CreateProcess(   
  (LPCSTR)NULL,//lpApplicationName    
  (LPSTR)FileName,//lpCommandLine    
  (LPSECURITY_ATTRIBUTES)&sa,//lpProcessAttributes    
  (LPSECURITY_ATTRIBUTES)&sa,//lpThreadAttributes    
  (BOOL)TRUE,//bInheritHandles    
  (DWORD)NORMAL_PRIORITY_CLASS,//dwCreationFlags    
  (LPVOID)NULL,//lpEnvironment    
  (LPCSTR)NULL,//lpCurrentDirectory    
  (LPSTARTUPINFOA)&StartupInfo,//lpStartupInfo    
  (LPPROCESS_INFORMATION)&ProcessInfo //lpProcessInformation    
  );   
 CloseHandle(ProcessInfo.hProcess);   
 CloseHandle(ProcessInfo.hThread);   
}   
CloseHandle(hReadPipe);   
CloseHandle(hWritePipe);    
EnableBreakOnLibraryErrors (); //打开异常显示    
}  

int main (int argc, char *argv[])   
{   
if (InitCVIRTE (0, argv, 0) == 0)   
 return -1;  /* out of memory */   
if ((panelHandle = LoadPanel (0, "CVIShell.uir", PANEL)) < 0)   
 return -1;   
DisplayPanel (panelHandle);   
GetPanelAttribute (panelHandle, ATTR_SYSTEM_WINDOW_HANDLE, &handle);   
RunUserInterface ();   
DiscardPanel (panelHandle);   
return 0;   
}  

int CVICALLBACK QUITCB (int panel, int control, int event,   
     void *callbackData, int eventData1, int eventData2)   
{   
switch (event)   
{   
case EVENT_COMMIT:   
 QuitUserInterface (0);   
 break;   
}   
return 0;   
}  

/*--------------------------------------------------------------  
下面的回调函数是计算器和记事本事件共有的回调  
---------------------------------------------------------------*/   
int CVICALLBACK CVISHELLCB (int panel, int control, int event,   
      void *callbackData, int eventData1, int eventData2)   
{   
int CtrlID;   
HWND hwnd;   
int handletemp;   
char Operation[2][5] = {   
 "open",   
 "open"   
};   
char FileName[2][12] = {   
 "calc.exe",   
 "notepad.exe"   
};   
char WindowName[512];   
char TextFileName[512];   
char CommandLine[512];   
char FileNames[512];   
char PathNames[512];   
char DriveNames[10];   
int ExecNum;   
//SHELLEXECUTEINFO ExecInfo;    
switch (event)   
{   
case EVENT_COMMIT:   
 GetCtrlVal (panelHandle, PANEL_RING, &ExecNum);//取出何种方式调用    
 CtrlID = GetActiveCtrl (panelHandle) - PANEL_CALCBUTTON;//区别哪个控件产生的    
 CommandLine[0] = '\0';   
 strcat(CommandLine, FileName[CtrlID]);    
 WindowName[0] = '\0';   
 if (CtrlID == 0) {//计算器    
  strcat(WindowName, "计算器");   
 }   
 else {//记事本    
  GetCtrlVal (panelHandle, PANEL_STRING, TextFileName);    
  if (*TextFileName == 0) {   
   strcat(WindowName, "无标题 - 记事本");   
  }   
  else {   
   DriveNames[0] = '\0';   
   FileNames[0] = '\0';   
   PathNames[0] = '\0';   
   //MakePathname()有bug                    
   //                  MakePathname (TextFileName, FileNames, PathNames);                  
   SplitPath (TextFileName, DriveNames, PathNames, FileNames);   
   strcat(WindowName, FileNames);   
   strcat(WindowName, " - 记事本");   
  }   
  strcat(CommandLine, " ");   
  strcat(CommandLine, TextFileName);     
 }   
 switch (ExecNum) {   
case 0://API函数WinExec()    
 WinExec(CommandLine, SW_NORMAL);   
 //以下代码是将外部程序面板套在本主窗体面板的内部    
 hwnd = FindWindow((LPCSTR)NULL, (LPCSTR)WindowName);    
 SetParent(hwnd, (HWND)handle);   
 break;   
case 1://API函数CreateProcess()    
 WinExecAndWait32(CommandLine, SW_NORMAL);   
 break;   
case 2://CVI函数LaunchExecutable()    
 LaunchExecutable (CommandLine);   
 break;   
case 3://CVI函数LaunchExecutableEx()    
 LaunchExecutableEx (CommandLine, SW_NORMAL, &handletemp);   
 break;   
 //由于CVI的shellapi.h有bug,以下代码无法编译通过    
 /*  
 case 4://API函数ShellExecute()  
 ShellExecute((HWND)handle, (LPCSTR)Operation[CtrlID], (LPCSTR)CommandLine, (LPCSTR)NULL, (LPCSTR)NULL, (INT)SW_NORMAL);  
 break;  
 */   
 //由于CVI的shellapi.h有bug,以下代码无法编译通过    
 /*                    
 case 5://API函数ShellExecuteEx()  
 ExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);  
 ExecInfo.fMask=SEE_MASK_NOCLOSEPROCESS;  
 ExecInfo.hwnd = 0;  
 ExecInfo.lpFile = CommandLine;  
 ExecInfo.lpParameters =NULL;  
 ExecInfo.lpDirectory = NULL;  
 ExecInfo.nShow = SW_SHOWNORMAL;  
 ShellExecuteEx(&ExecInfo);  
 break;  
 */   
 }   
 break;   
}   
return 0;   
}  


int CVICALLBACK OPENFILECB (int panel, int control, int event,   
      void *callbackData, int eventData1, int eventData2)   
{   
char TextFileName[512];   
switch (event)   
{   
case EVENT_COMMIT:   
 if (FileSelectPopup ("", "*.Txt", "*.Text", "选择记事本文件", VAL_SELECT_BUTTON, 0, 0, 1, 0,   
  TextFileName))    
 {   
  SetCtrlVal (panelHandle, PANEL_STRING, TextFileName);   
 }   
 break;   
}   
return 0;   
}  

int CVICALLBACK GETFILENAMECB (int panel, int control, int event,   
         void *callbackData, int eventData1, int eventData2)   
{   
char TextFileName[512];   
switch (event)   
{   
case EVENT_LEFT_DOUBLE_CLICK:   
 if (FileSelectPopup ("", "*.Txt", "*.Text", "选择记事本文件", VAL_SELECT_BUTTON, 0, 0, 1, 0,   
  TextFileName))    
 {   
  SetCtrlVal (panelHandle, PANEL_STRING, TextFileName);   
 }   
 break;   
}   
return 0;   
}

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值