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

推荐订阅源

Scott Helme
Scott Helme
N
Netflix TechBlog - Medium
AI
AI
Security Latest
Security Latest
GbyAI
GbyAI
P
Proofpoint News Feed
Y
Y Combinator Blog
A
Arctic Wolf
G
Google Developers Blog
U
Unit 42
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
Latest news
Latest news
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
WordPress大学
WordPress大学
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
Simon Willison's Weblog
Simon Willison's Weblog
V
V2EX
Project Zero
Project Zero
博客园_首页

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