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;
  • 相关阅读:
    leetcode 912. 排序数组
    leetcode 633. 平方数之和
    leetcode 1512. 好数对的数目
    leetcode 1822. 数组元素积的符号
    leetcode 145. 二叉树的后序遍历
    leetcode 11. 盛最多水的容器
    leetcode 28 实现strStr()
    leetcode 27. 移除元素
    leetcode 26. 删除有序数组中的重复项
    产品化思维之公式系统
  • 原文地址:https://www.cnblogs.com/enli/p/2045694.html
Copyright © 2011-2022 走看看