zoukankan      html  css  js  c++  java
  • Delphi 备忘五

    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;
  • 相关阅读:
    Apache OFBiz 学习笔记 之 实体引擎
    Apache OFBiz 学习笔记 之 服务引擎 二
    Apache OFBiz 学习笔记 之 服务引擎 一
    [OFBiz]开发 五
    [OFBiz]开发 四
    [OFBiz]开发 三
    [OFBiz]开发 二
    通过Wmi实现Hyper-V远程管理(一)
    wifi智能插座 一键扫描局域网内插座Ip及其它信息 Python源代码API
    .NET4.0下使用Net2.0类库或程序集
  • 原文地址:https://www.cnblogs.com/enli/p/2045694.html
Copyright © 2011-2022 走看看