⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvishell.c

📁 LabWindowsCVI调用外部程序的六种方法源代码。labwindowsCVI是一种良好的虚拟机编程环境
💻 C
字号:
#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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -