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

推荐订阅源

H
Help Net Security
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
博客园_首页
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
B
Blog
D
DataBreaches.Net
腾讯CDC
C
Check Point Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
月光博客
月光博客
V
V2EX
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
The Cloudflare Blog
博客园 - 叶小钗
Y
Y Combinator Blog

博客园 - 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;