谁能把我的这段代码翻成VB的代码,谢谢了 。

flxa 2003-12-15 12:03:10
procedure TIniFile.ReadSections(Strings: TStrings);
const
BufSize = 16384;
var
Buffer, P: PChar;
begin
GetMem(Buffer, BufSize);
try
Strings.BeginUpdate;
try
Strings.Clear;
if GetPrivateProfileString(nil, nil, nil, Buffer, BufSize,
PChar(FFileName)) <> 0 then
begin
P := Buffer;
while P^ <> #0 do
begin
Strings.Add(P);
Inc(P, StrLen(P) + 1);
end;
end;
finally
Strings.EndUpdate;
end;
finally
FreeMem(Buffer, BufSize);
end;
end;
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lazygod 2003-12-16
  • 打赏
  • 举报
回复
if secLen = nSize + 1 then
GetSections = 1
else
S = Left$(Buffer, secLen)
Sections = Split(Sections, Chr(0) & Chr(0))
GetSections = 0
//少了个end if
lazygod 2003-12-16
  • 打赏
  • 举报
回复
public sub ReadSections(GetSections(Sections() as String)
错了
public sub ReadSections(Sections() as String)
lazygod 2003-12-16
  • 打赏
  • 举报
回复
public const BufSize = 16384
public sub ReadSections(Sections() as String)
dim Buffer as String * BufSize
dim S as String
dim secLen as long

on error goto errHandler
secLen = GetPrivateProfileString(vbNullString, vbNullString, "", Buffer, BufSize, FileName)
if secLen > 0 then
S = Left$(Buffer, secLen)
Sections = Split(Sections, Chr(0) & Chr(0))
end if
errHandler:
end sub

现改的,大概能用。不过你原代码有问题,看原型:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);

Return Values
The return value is the number of characters copied to the buffer, not including the terminating null character.

If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one.

If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two.

因此,取出来的可能不完整。
所以:
public const BufSize = 16384
public function GetSections(Sections() as String, ByVal nSize as long) as long
dim Buffer as String * nSize
dim S as String
dim secLen as long

GetSections = -1
on error goto errHandler
secLen = GetPrivateProfileString(vbNullString, vbNullString, vbNullString, Buffer, nSize, FileName)
if secLen > 0 then
if secLen = nSize + 1 then
GetSections = 1
else
S = Left$(Buffer, secLen)
Sections = Split(Sections, Chr(0) & Chr(0))
GetSections = 0
end if
errHandler:
end sub

public sub ReadSections(GetSections(Sections() as String)
dim nSize as long

nSize = BufSize
while GetSections(Sections, nSize) = 1
nSize = nSize * 2
GetSections(Sections, nSize)
wend
end sub
wjpop3 2003-12-16
  • 打赏
  • 举报
回复
这是干吗??

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧