?? u3util.c
字號:
/* u3util.c * Utility routines for U3 device support * * $Id: u3util.c 23433 2007-11-12 07:34:32Z ulfl $ * * Wireshark - Network traffic analyzer * By Gerald Combs <gerald@wireshark.org> * Copyright 1998 Gerald Combs * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Adapted from Microsoft Knowledge Base Article 178893 * * http://support.microsoft.com/?kbid=178893 * * and the U3 Answer 106 * * https://u3.custhelp.com/cgi-bin/u3/php/enduser/std_adp.php?p_faqid=106 * * Indentation logic: 2-space */#include <windows.h>#include <winreg.h>#include <shlobj.h>#define WIRESHARK_ASSOC "u3-wireshark-file"#define WIRESHARK_DESC "U3 Wireshark File"#define SHELL "\\Shell"#define SHELL_OPEN "\\Shell\\open"#define SHELL_OPEN_COMMAND "\\Shell\\open\\command"#define DEFAULT_ICON "\\DefaultIcon"#define WINPCAP_PACKAGE "\\WinPcap_4_0_2.exe"#define WINPCAP_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\WinPcapInst"#define WINPCAP_UNINSTALL "UninstallString"#define WINPCAP_U3INSTALLED "U3Installed" /* indicate the U3 device that installed WinPcap */#define MY_CAPTURES "\\My Captures"#define BUFSIZ 256#define FILEBUFSIZ 4096#define ENV_FILENAME "\\u3_environment.txt"#define U3UTIL_APPSTART "\\u3util.exe appStart %1"#define WIRESHARK_EXE "\\wireshark.exe"static char *extensions[] = { ".5vw", ".acp", ".apc", ".atc", ".bfr", ".cap", ".enc", ".erf", ".fdc", ".pcap", ".pkt", ".tpc", ".tr1", ".trace", ".trc", ".wpc", ".wpz", /* and BER encoded files */ ".cer", ".crt", ".crl", ".p12", ".pfx", ".asn", ".spf", ".p7c", ".p7s", ".p7m", NULL};static char *environmentvars[] = { "U3_DEVICE_SERIAL", "U3_DEVICE_PATH", "U3_DEVICE_DOCUMENT_PATH", "U3_DEVICE_VENDOR", "U3_DEVICE_PRODUCT", "U3_DEVICE_VENDOR_ID", "U3_APP_DATA_PATH", "U3_HOST_EXEC_PATH", "U3_DEVICE_EXEC_PATH", "U3_ENV_VERSION", "U3_ENV_LANGUAGE", NULL,};#define TA_FAILED 0#define TA_SUCCESS_CLEAN 1#define TA_SUCCESS_KILL 2#define TA_SUCCESS_16 3DWORD TerminateApp( DWORD dwPID, DWORD dwTimeout ) ;DWORD Terminate16App( DWORD dwPID, DWORD dwThread, WORD w16Task, DWORD dwTimeout );#include <vdmdbg.h>typedef struct{ DWORD dwID ; DWORD dwThread ;} TERMINFO ;/* Declare Callback Enum Functions. */BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ) ;BOOL CALLBACK Terminate16AppEnum( HWND hwnd, LPARAM lParam ) ;/*---------------------------------------------------------------- DWORD TerminateApp( DWORD dwPID, DWORD dwTimeout ) Purpose: Shut down a 32-Bit Process (or 16-bit process under Windows 95) Parameters: dwPID Process ID of the process to shut down. dwTimeout Wait time in milliseconds before shutting down the process. Return Value: TA_FAILED - If the shutdown failed. TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE. TA_SUCCESS_KILL - if the process was shut down with TerminateProcess(). NOTE: See header for these defines. ----------------------------------------------------------------*/DWORD TerminateApp( DWORD dwPID, DWORD dwTimeout ){ HANDLE hProc ; DWORD dwRet ; // If we can't open the process with PROCESS_TERMINATE rights, // then we give up immediately. hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, dwPID); if(hProc == NULL){ return TA_FAILED; } if(dwTimeout) { /* we are prepared to wait */ /* TerminateAppEnum() posts WM_CLOSE to all windows whose PID */ /* matches your process's. */ EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ; /* Wait on the handle. If it signals, great. If it times out, */ /* then you kill it. */ if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0) dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED); else dwRet = TA_SUCCESS_CLEAN ; } else { /* we immediately kill the proces */ dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED); } CloseHandle(hProc) ; return dwRet ;}/*---------------------------------------------------------------- DWORD Terminate16App( DWORD dwPID, DWORD dwThread, WORD w16Task, DWORD dwTimeout ) Purpose: Shut down a Win16 APP. Parameters: dwPID Process ID of the NTVDM in which the 16-bit application is running. dwThread Thread ID of the thread of execution for the 16-bit application. w16Task 16-bit task handle for the application. dwTimeout Wait time in milliseconds before shutting down the task. Return Value: If successful, returns TA_SUCCESS_16 If unsuccessful, returns TA_FAILED. NOTE: These values are defined in the header for this function. NOTE: You can get the Win16 task and thread ID through the VDMEnumTaskWOW() or the VDMEnumTaskWOWEx() functions. ----------------------------------------------------------------*/DWORD Terminate16App( DWORD dwPID, DWORD dwThread, WORD w16Task, DWORD dwTimeout ){ HINSTANCE hInstLib ; TERMINFO info ; /* You will be calling the functions through explicit linking */ /* so that this code will be binary compatible across */ /* Win32 platforms. */ BOOL (WINAPI *lpfVDMTerminateTaskWOW)(DWORD dwProcessId, WORD htask) ; hInstLib = LoadLibraryA( "VDMDBG.DLL" ) ; if( hInstLib == NULL ) return TA_FAILED ; // Get procedure addresses. lpfVDMTerminateTaskWOW = (BOOL (WINAPI *)(DWORD, WORD )) GetProcAddress( hInstLib, "VDMTerminateTaskWOW" ) ; if( lpfVDMTerminateTaskWOW == NULL ) { FreeLibrary( hInstLib ) ; return TA_FAILED ; } /* Post a WM_CLOSE to all windows that match the ID and the */ /* thread. */ info.dwID = dwPID ; info.dwThread = dwThread ; EnumWindows((WNDENUMPROC)Terminate16AppEnum, (LPARAM) &info) ; /* Wait. */ Sleep( dwTimeout ) ; /* Then terminate. */ lpfVDMTerminateTaskWOW(dwPID, w16Task) ; FreeLibrary( hInstLib ) ; return TA_SUCCESS_16 ;}BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam ){ DWORD dwID ; GetWindowThreadProcessId(hwnd, &dwID) ; if(dwID == (DWORD)lParam) { PostMessage(hwnd, WM_CLOSE, 0, 0) ; } return TRUE ;}BOOL CALLBACK Terminate16AppEnum( HWND hwnd, LPARAM lParam ){ DWORD dwID ; DWORD dwThread ; TERMINFO *termInfo ; termInfo = (TERMINFO *)lParam ; dwThread = GetWindowThreadProcessId(hwnd, &dwID) ; if(dwID == termInfo->dwID && termInfo->dwThread == dwThread ) { PostMessage(hwnd, WM_CLOSE, 0, 0) ; } return TRUE ;}void ExecuteAndWait(char *buffer){ STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); if(CreateProcess(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { /* wait for the uninstall to finish */ (void) WaitForSingleObject(pi.hProcess, INFINITE); (void)CloseHandle(pi.hProcess); (void)CloseHandle(pi.hThread); }}void app_start(int argc, char *argv[]){ char *u3hostexecpath; char *envvar; char *end; char buffer[BUFSIZ+1]; char inBuffer[FILEBUFSIZ+1]; HANDLE *file; DWORD numRead = 0; int i; /* read any environment variables that may be set as we are probably running this from a file association */ buffer[0] = '\0'; strncat(buffer, argv[0], strlen(argv[0]) + 1); /* truncate at last \\ */ if(end = strrchr(buffer, '\\')) *end = '\0'; strncat(buffer, ENV_FILENAME, strlen(ENV_FILENAME) + 1); /* open the file */ if((file = CreateFile(buffer, FILE_READ_DATA, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) { /* read the whole file in in one go */ if(ReadFile(file, &inBuffer, FILEBUFSIZ, &numRead, NULL) != 0) { /* we were successful - parse the lines */ inBuffer[numRead] = '\0'; /* null terminate the data */ envvar = inBuffer; while(end = strchr(envvar, '\n')) { /* we have a line */ *end++ = '\0'; _putenv(envvar); /* point the next envar to the end */ envvar = end; } } /* close the file */ CloseHandle(file); } /* exec wireshark */ if((u3hostexecpath = getenv("U3_HOST_EXEC_PATH")) != NULL) { buffer[0] = '\0'; strncat(buffer, u3hostexecpath, strlen(u3hostexecpath) + 1); strncat(buffer, WIRESHARK_EXE, strlen(WIRESHARK_EXE) + 1); /* copy the remaining arguments across */ for(i = 2; i < argc; i++) { strncat(buffer, " ", 2); strncat(buffer, argv[i], strlen(argv[i]) + 1); } ExecuteAndWait(buffer); }}void app_stop(DWORD timeOut){ DWORD pid = 0; HANDLE hFind = INVALID_HANDLE_VALUE; WIN32_FIND_DATA find_file_data; DWORD dwError; char *u3_host_exec_path; char dir_spec[MAX_PATH+1]; char file_name[MAX_PATH+1]; u3_host_exec_path = getenv("U3_HOST_EXEC_PATH"); strncpy(dir_spec, u3_host_exec_path, strlen(u3_host_exec_path) + 1);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -