分享
 
 
 

GetCommandLine

王朝百科·作者佚名  2010-03-27
窄屏简体版  字體: |||超大  

GetCommandLine

The GetCommandLine function retrieves the command-line string for the current process.

LPTSTR GetCommandLine(void);

Parameters

This function has no parameters.

Return Values

The return value is a pointer to the command-line string for the current process.

Remarks

ANSI console processes written in C can use the argc and argv arguments of the main function to access the command-line arguments. ANSI GUI applications can use the lpCmdLine parameter of the WinMain function to access the command-line string, excluding the program name. The reason that main and WinMain cannot return Unicode strings is that argc, argv, and lpCmdLine use the LPSTR data type for parameters, not the LPTSTR data type . The GetCommandLine function can be used to access Unicode strings, because it uses the LPTSTR data type.

To convert the command line to an argv style array of strings, call the CommandLineToArgvW function.

Note The name of the executable in the command line that the operating system provides to a process is not necessarily identical to that in the command line that the calling process gives to the CreateProcess function. The operating system may prepend a fully qualified path to an executable name that is provided without a fully qualified path.

Requirements

Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.

Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.

Unicode: Implemented as Unicode and ANSI versions on all platforms.

Header: Declared in Winbase.h; include Windows.h.

Library: Use Kernel32.lib.

See Also

Processes and Threads Overview, Process and Thread Functions, CommandLineToArgvW, CreateProcess, WinMain

[声明]

Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" () As String

[说明]

获得指向当前命令行缓冲区的一个指针

[返回值]

Long,命令行缓冲区在内存中的地址

[其它]

Visual Basic Command函数更易获取参数,但它未提供可执行的名称。使用这个函数时,要求进行内存复制操作

[注释]

使用这个函数用的时候,不应该直接使用诸如 str=GetCommandLine() (str是一个字符串)这样的语句,这样有可能会导致内存的错误。

正确的做法是应该把 声明 中的 "As String" 改为 "As Long",再用 lstrlen 获取长度,并用这个长度加一来设置一个字符串的长度,最后用 lstrcpy 复制到字符串中。

代码如下:

Private Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineA" () As Long

Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long

Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long

Public Property Get CommandLine() As String

Dim length As Long, pstr As Long

pstr = GetCommandLine() '获取字符串的指针

length = lstrlen(pstr) '获取字符串的长度

CommandLine = String(length + 1, 0) '调整字符串的大小

lstrcpy CommandLine, pstr '复制字符串

End Property

Private Sub Form_Load()

MsgBox CommandLine

End Sub

下面给出一个实例,描述如何使用该函数

#include <windows.h>

#include <stdio.h>

#include <shellapi.h>

int __cdecl main()

{

LPWSTR *szArglist;

int nArgs;

int i;

szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);

if( NULL == szArglist )

{

wprintf(L"CommandLineToArgvW failed

");

return 0;

}

else

{

for( i=0; i<nArgs; i++)

printf("%d: %ws

", i, szArglist[i]);

LocalFree(szArglist);// Free memory allocated for CommandLineToArgvW arguments.

return(1);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有