Home » Windows API

lstrcpyn

說明:
lstrcpyn 函式可指定要複製的字元數(Bytes)到一記憶體空間


引用函式庫:
Kernel32


適用系統:
Windows NT 3.5 或 Windows 95以上


函式原型:

Declare Function lstrcpyn Lib "kernel32" Alias "lstrcpynA" ( _
    ByVal lpString1 As String, _
    ByVal lpString2 As String, _
    ByVal iMaxLength As Long _
    ) As Long



參數型態及說明:
lpString1String 要存放字串的記憶體空間
lpString2String 要放到記憶體空間的字串
iMaxLengthLong 要放到記憶體空間的字元數(Bytes)

回傳值:
Long 若呼叫成功的話,將會回傳複製到記憶體空間的指標位置;若呼叫失敗的話,則回傳 NULL

.NET Framework API:
System.String.CopyTo
System.String.ToCharArray


VB範例:

檢視原始碼 Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'VB的宣告
Private Declare Function lstrcpyn Lib "kernel32" Alias "lstrcpynA" ( _
	ByVal lpString1 As String, _
	ByVal lpString2 As String, _
	ByVal iMaxLength As Long _
	) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
	ByVal lpString As String _
	) As Long
 
'宣告一個記憶體空間
Dim str1 As String
 
str1 = String(200, 0)
 
'要複製的字串
Dim str2 As String
 
str2 = "Windows API"
 
Dim rtvl As Long
 
'把str2全部複製到str1中
rtvl = lstrcpyn(str1, str2, lstrlen(str2) + 1)

RB範例:

檢視原始碼 REALBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'RB的宣告
Declare Function lstrcpyn Lib "kernel32" Alias "lstrcpynA" ( _
	lpString1 As Ptr, _
	lpString2 As CString, _
	iMaxLength As Integer _
	) As Integer
Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" ( _
	lpString2 As CString _
	) As Integer
 
'宣告一個記憶體空間
Dim str1 As New MemoryBlock(200)
 
'要複製的字串
Dim str2 As String
 
str2 = "Windows API"
 
Dim rtvl As Integer
 
'把str2全部複製到str1中
rtvl = lstrcpyn(str1, str2, lstrlen(str2) + 1)

VB.NET範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'VB.NET的宣告
<DllImport("kernel32.dll")> _
Shared Function lstrcpyn( _
	ByVal lpString As StringBuilder, _
	ByVal lpString2 As String, _
	ByVal iMaxLength As Integer _
	) As IntPtr
End Function
<DllImport("kernel32.dll")> _
Shared Function lstrlen( _
	ByVal lpString As String _
	) As Integer
End Function
 
'宣告一個StringBuilder
Dim str1 As New StringBuilder()
 
'要複製的字串
Dim str2 As String
 
str2 = "Windows API"
 
Dim rtvl As IntPtr
 
'取得str的字元數
rtvl = lstrcpyn(str1, str2, lstrlen(str2) + 1)

C#範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//C#的宣告
[DllImport("kernel32")]
public static extern IntPtr lstrcpyn(
	StringBuilder lpString, 
	string lpString2, 
	int iMaxLength
	);
[DllImport("kernel32")]
public static extern int lstrlen(
	string lpString
	);
 
//宣告一個StringBuilder
StringBuilder str1 = new StringBuilder();
 
//要複製的字串
string str2;
 
str2 = "Windows API";
 
IntPtr rtvl;
 
//把str2全部複製到str1中
rtvl = lstrcpyn(str1, str2, lstrlen(str2) + 1);

發表迴響