.NET Main函数参数解释过程和特殊规则
来源:互联网 发布:淘宝充值 编辑:程序博客网 时间:2024/04/29 14:06
实际上传入的参数是C:/Program Files” 。其实这里不仔细看可能发现不了问题。在原来的命令行中,第一个双引号代表一个参数的开始/结束,后面/”因为.NET的解释规则不同,代表实际的双引号,而非参数的开始/结束,因此最后的结果会多出一个双引号,并且缺少一个反斜杠。
因此,正确的方式是第二个,也就是”C:/Program Files//”。
HRESULT CorCommandLine::SetArgvW(LPCWSTR lpCommandLine)
HRESULT hr = S_OK;
// 分析命令行
m_ArgvW = SegmentCommandLine(lpCommandLine, &m_NumArgs);
// CLR特有的命令行处理,主要是和ClickOnce有关的
if (m_ArgvW)
else
}
return hr;
HRESULT ClassLoader::RunMain(MethodDesc *pFD ,
PTRARRAYREF *stringArgs /*=NULL*/)
wzArgs = CorCommandLine::GetArgvW(&cCommandArgs);
// 创建一个托管数组
StrArgArray = (PTRARRAYREF) AllocateObjectArray((cCommandArgs - numSkipArgs), g_pStringClass);
// 创建对应的托管字符串并赋给托管数组的每个元素
for( arg = numSkipArgs; arg < cCommandArgs; arg++) {
STRINGREF sref = COMString::NewString(wzArgs[arg]);
StrArgArray->SetAt(arg-numSkipArgs, (OBJECTREF) sref);
}
ARG_SLOT stackVar = ObjToArgSlot(StrArgArray); // 将数组元素转为函数参数
*piRetVal = (INT32)threadStart.Call_RetArgSlot(&stackVar); // 调用主函数(EntryPoint)
return hr;
// Splits a command line into argc/argv lists, using the VC7 parsing rules.
// This functions interface mimics the CommandLineToArgvW api.
// If function suceeds, call delete [] on return pointer when done.
LPWSTR *SegmentCommandLine(LPCWSTR lpCmdLine, DWORD *pNumArgs)
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_NOTRIGGER;
STATIC_CONTRACT_FAULT;
*pNumArgs = 0;
int nch = (int)wcslen(lpCmdLine);
// Calculate the worstcase storage requirement. (One pointer for
// each argument, plus storage for the arguments themselves.)
int cbAlloc = (nch+1)*sizeof(LPWSTR) + sizeof(WCHAR)*(nch + 1);
LPWSTR pAlloc = new (nothrow) WCHAR[cbAlloc / sizeof(WCHAR)];
if (!pAlloc)
return NULL;
LPWSTR *argv = (LPWSTR*) pAlloc; // We store the argv pointers in the first halt
LPWSTR pdst = (LPWSTR)( ((BYTE*)pAlloc) + sizeof(LPWSTR)*(nch+1) ); // A running pointer to second half to store arguments
LPCWSTR psrc = lpCmdLine;
WCHAR c;
BOOL inquote;
BOOL copychar;
int numslash;
// First, parse the program name (argv[0]). Argv[0] is parsed under
// special rules. Anything up to the first whitespace outside a quoted
// subtring is accepted. Backslashes are treated as normal characters.
argv[ (*pNumArgs)++ ] = pdst;
inquote = FALSE;
do {
if (*psrc == L'"' )
{
}
*pdst++ = *psrc;
c = *psrc++;
} while ( (c != L'/0' && (inquote || (c != L' ' && c != L'/t'))) );
if ( c == L'/0' ) {
psrc--;
} else {
*(pdst-1) = L'/0';
}
inquote = FALSE;
/* loop on each argument */
for(;;)
{
if ( *psrc )
{
while (*psrc == L' ' || *psrc == L'/t')
}
if (*psrc == L'/0')
/* scan an argument */
argv[ (*pNumArgs)++ ] = pdst;
/* loop through scanning one argument */
for (;;)
{
/* Rules: 2N backslashes + " ==> N backslashes and begin/end quote
2N+1 backslashes + " ==> N backslashes + literal "
N backslashes ==> N backslashes */
/* count number of backslashes for use below */
/* if 2N backslashes before, start/end quote, otherwise
if (psrc[1] == L'"')
/* skip first quote char and copy second */
/* if at end of arg, break loop */
if (*psrc == L'/0' || (!inquote && (*psrc == L' ' || *psrc == L'/t')))
/* copy character into argument */
}
/* null-terminate the argument */
*pdst++ = L'/0'; /* terminate string */
}
/* We put one last argument in -- a null ptr */
argv[ (*pNumArgs) ] = NULL;
_ASSERTE((BYTE*)pdst <= (BYTE*)pAlloc + cbAlloc);
return argv;
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1852691
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- .NET Main函数参数解释过程和特殊规则
- 如何在NTFS文件系统中创建文件的Link
- .NET中的幕后英雄:MSCOREE.DLL
- 使用WinDbg对Windows Live Toolbar死锁的简单分析
- ZZ:使用.NET语言开发Silverlight应用程序入门(一):了解项目结构
- 使用UltraISO给Windows ISO镜像添加引导记录
- .NET Main函数参数解释过程和特殊规则
- .NET中的幕后英雄:MSCOREE.DLL
- Calling Convention的总结
- ZZ:使用.NET语言开发Silverlight应用程序入门(二):界面和对象模型
- .NET/CLR好书推荐
- Visual Studio中的Class Designer设计工具需要您的建议!
- 可扩展能力还是够用就好?
- 微软宣布将发布.NET Framework Library源代码
- 如何构造一个元素类型为Struct的SafeArray