GetCommandLine

使用這個函式用的時候,不應該直接使用諸如 str=GetCommandLine() (str是一個字元串)這樣的語句,這樣有可能會導致記憶體的錯誤。

基本介紹

  • 中文名:GetCommandLine
  • 外文名:GetCommandLine
  • 性質:函式
  • 用途:編程
基本簡介,實際例子,

基本簡介

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函式更易獲取參數,但它未提供可執行的名稱。使用這個函式時,要求進行記憶體複製操作
[注釋]
正確的做法是應該把 聲明 中的 "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\n");
return 0;
}
else
{
for( i=0; i<nArgs; i++)
printf("%d: %ws\n", i, szArglist[i]);
LocalFree(szArglist);// Free memory allocated for CommandLineToArgvW arguments.
return(1);
}
}

相關詞條

熱門詞條

聯絡我們