惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Recent Announcements
Recent Announcements
D
Docker
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
U
Unit 42
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC
B
Blog
博客园_首页
罗磊的独立博客
D
DataBreaches.Net
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
L
LangChain Blog
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

博客园 - Enli

IOS 命令行安装备忘 IOS开发备忘 MAC 编制计划任务 弹出式窗口管理单元备忘 Wininet请求包装类简稿 自备工具库 多屏开发的备忘 界面方面的备忘 rc资源文件的中英文应用备忘 Wininet下载类初稿 Dephi调用C#编写的WebService的一些问题与解决 (转) 关于Window的快捷方式,图标缓存的清理 http断点续传的单元 Delphi 代码优化(转) [转载]Delphi的四舍五入函数 同名派生的应用(转) 关于delphi的程序在英文操作系统下乱码问题 - Enli - 博客园 TWebBrowser备忘 组件开发中的技巧(转)
Delphi 备忘五
Enli · 2011-05-13 · via 博客园 - Enli

1.  Variant和Stream的互换

procedure VarToStream(var AStm: TStream; var AOvar: Olevariant);
var
  p: Pointer;
begin
  AStm:= TMemoryStream.Create;
  AStm.Position := 0;
  p := VarArrayLock(AOvar);
  AStm.Write(p^, VarArrayHighBound(AOvar, 1));
  VarArrayUnlock(AOvar);
end;

procedure StreamToVar(var AStm: TStream; var AOvar: Olevariant);
var
    p: Pointer;
begin
  AOvar := VarArrayCreate([0, AStm.Size - 1], VarByte);
  p := VarArrayLock(AOvar);
  AStm.ReadBuffer(p^, AStm.Size);
  VarArrayUnlock(AOvar);
end;

2. 判断文件是否在用

{createfile里面的第三个参数为dwShareMode ,使用如下(详见createfile的API)
[in] The sharing mode of an object, which can be read, write, both, or none. 
You cannot request a sharing mode that conflicts with the access mode that is specified in an open request that has an open handle, because that would result in the following sharing violation: ERROR_SHARING_VIOLATION). For more information, see Creating and Opening Files.

If this parameter is 0 (zero) and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed. For more information, see the Remarks section of this topic.

} function IsFileInUse(const AFileName: string): Boolean;
var
  HFileRes: HFILE;
begin
  Result :
= false;
  
if not FileExists(AFileName) then //如果文件不存在
    exit;
  HFileRes :
= CreateFile(pchar(AFileName), GENERIC_READ or GENERIC_WRITE,
    
0 {this is the trick!}nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Result :
= (HFileRes = INVALID_HANDLE_VALUE);
  
if not Result then
    CloseHandle(HFileRes);
end;