首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

从Entry Point到main函数调用(四)

2012-10-27 
从Entry Point到main函数调用(4)先撇开_ioinit()不谈,IO有点儿麻烦,待有空再去挖掘这些东西。GetCommandLin

从Entry Point到main函数调用(4)

先撇开_ioinit()不谈,IO有点儿麻烦,待有空再去挖掘这些东西。

GetCommandLineA

该函数的汇编代码短得令人发指。

?

可见实质上,__crtGetEnvironmentStringsA 函数依然是调用了win API,可能是:

GetEnvironmentStringsW? 或 GetEnvironmentStringsA

?

有人也许奇怪,为什么__crtGetEnvironmentStringsA 不直接去调用GetEnvironmentStringsA 呢?原因在于,即使是用GetEnvironmentStringsA ,所获取的也并非是 ANSI 的环境变量。

returns the strings in the OEM code page, not the ANSI code page. If you need the returned strings in the ANSI code page call the Unicode version (GetEnvironmentStringsW) and translate the returned wide-string to an 8-bit string using a conversion function such as WideCharToMultiByte. Or call GetEnvironmentVariableA, which correctly uses the ANSI code page.

这里很清楚地写到,GetEnvironmentStringsA 返回的其实是OEM 字符,并非 ANSI。如果最终需要获得 ANSI 的环境变量,可以使用 GetEnvironmentStringsW ,然后将所获得的结果从 wide-string 转化成 8-bit string 。

?

因此,__crtGetEnvironmentStringsA 中首先调用了 GetEnvironmentStringsW 之后,设置 f_use = USE_W , 再进入if (USE_W == f_use) 的 语句块,进入转化成ANSI的处理。

?

?

这里仅分析_setargv 中最为核心的代码行。

?

numargs 和 numchars 可能比较难以理解。举例来说,现有test0.exe,运行时附加了三个参数 a b c

?

cmdstart 指向的内存地址是0x00141ee0:

从Entry Point到main函数调用(四)

这里一共占用了 33 个byte (包含末尾的 00)

?

最后指针 P 指向的内存地址是0x003812c0:

从Entry Point到main函数调用(四)

很显然,0x003812c0 开始是指针数组,其中包含了4个有效指针,1个空指针。接着指针数组后的是4个ANSI字符串(红色线框标出),每个字符串以"00"结尾。

所以,0x003812c0 的大小 = 5个指针 + 4个字符串。

numargs?? = 5,表示指针数组的大小 ; numchars = 31(0x1F),表示四个字符串一共占用的字节数 。

?

__argc = 5-1

__argv = 0x003812c0

注意,最后返回的时候,需要将numargs-1,毕竟最后1个是空指针。

?

?

?

?

?

?

热点排行