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\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);
}
}
相關詞條
-
GetCommandLine
strin strin strin
-
CreateProcess
GetCommandLine函式獲得整個命令行。C語言程式可以使用argc和argv參數...
函式原型 參數 返回值 舉例說明 -
WinMain
執行檔案名稱。獲得整個命令行,參看GetCommandLine。第三個參數...,而不是LPTSTR類型。GetCommandLine函式可以用於進入...
函式原型 訊息 備註 速查 -
API函式
API概述定義作為一個編程初學者來說,API函式也許是一個時常耳聞卻感覺有些神秘的東西。單看它複雜的語法,就足令人望而生畏,但是...
API概述 函式大全 Escape 設備 內部函式 運算函式 -
call
GetCommandLine = raw.remoteApi...","GetCommandLineA") str = GetCommandLine...
REMOTE DOS命令 VB關鍵字 單詞解析 相關句子 -
WindowsAPI函式清單
WindowsAPI簡介:API的英文全稱(Application Programming Interface),WIN32 A...
WindowsAPI簡介: API函式清單: -
LOGOGO
病毒運行後,先通過GetCommandLine判斷是否有參數存在...
威脅情況 -
EnterCriticalSection
(::GetModuleHandle(NULL), NULL, ::GetCommandLine...
函式原型 訪問機制