• 全部
  • 语言基础/算法/系统设计
  • 数据库相关
  • 图形处理/多媒体
  • 网络通信/分布式开发
  • VCL组件开发及应用
  • Windows SDK/API
  • 问答

谁能把我的这段代码翻成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;
...全文
55 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
这是干吗??
回复
发帖
Delphi
创建于2007-08-02

4869

社区成员

Delphi 开发及应用
申请成为版主
帖子事件
创建了帖子
2003-12-15 12:03
社区公告
暂无公告