int _tmain(int argc, TCHAR** argv) { _tprintf(_T("argc=%d\n"), argc); for (size_t i = 0; i < argc; i++) _tprintf(_T("argv[%d]=%s\n"), i, argv[i]); system("pause"); return0; }
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow) { int nNumArgv; PWSTR* ppArgv = CommandLineToArgvW(GetCommandLineW(), &nNumArgv); for (size_t i = 0; i < nNumArgv; i++) { TCHAR info[256]; swprintf_s(info, _T("argv[%d]=%s"), i, ppArgv[i]); MessageBox(NULL, info, _T("温馨提示"), MB_OK); } return0; }
int _tmain(int argc, TCHAR** argv) { int nNumArgv; PWSTR* ppArgv = CommandLineToArgvW(GetCommandLineW(), &nNumArgv); for (size_t i = 0; i < nNumArgv; i++) wprintf(L"argv[%d]=%s\n", i, ppArgv[i]);
system("pause"); return0; }
环境变量
系统会为进程设置初始环境变量,其来源包括3个部分:内置环境变量、用户环境变量和系统环境变量。
子进程可以继承父进程的环境变量。
1、在控制台程序中通过实参获取环境变量
1 2 3 4 5 6 7 8 9 10
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int i = 0; // envp最后一个是nullptr while (envp[i++] != nullptr) { wprintf(L"%s\n", envp[i]); }
/// @brief 打印指定名称的环境变量 /// @param name 环境变量名 voidPrintEnviromentVariable(PCTSTR name) { PTSTR value = NULL; DWORD res = GetEnvironmentVariable(name, value, 0); if (res != 0) { DWORD size = res * sizeof(TCHAR); value = (PTSTR)malloc(size); GetEnvironmentVariable(name, value, size); _tprintf(_T("%s=%s\n"), name, value); free(value); } else _tprintf(_T("'%s'=<unknown value>\n"), name); }
int _tmain() { // 打印环境变量名 wprintf(L"环境变量:\n"); PrintEnviromentVariable(_T("env1")); PrintEnviromentVariable(_T("env2"));
// 打印命令行 wprintf(L"\n命令行:\n"); int nNumArgv; PWSTR* ppArgv = CommandLineToArgvW(GetCommandLineW(), &nNumArgv); for (size_t i = 0; i < nNumArgv; i++) wprintf(L"argv[%d]=%s\n", i, ppArgv[i]);
using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Data; using System.Diagnostics; using System.IO;
namespaceTest { classProgram { staticvoidMain(string[] args) { ProcessStartInfo process = new ProcessStartInfo(); process.UseShellExecute = false; process.FileName = @"LHTest.exe"; process.Arguments = string.Format("{0} {1}", "C#", "Windows"); process.Environment.Add("env1", "This is env1"); process.Environment.Add("env2", "This is env2"); Process.Start(process);