PARAMETERS filename,pathname *----------------------------------------------------------------------------------------------------------------------------------------- * IN *2009-03-20 YangAifeng *注册外部 Windows 32 位动态链接库 ( .DLL ) 中的一个函数。 *----------------------------------------------------------------------------------------------------------------------------------------- *SHFileOperation *函数功能描述:文件操作,与 Shell 的动作相同. DECLARE Integer SHFileOperation IN SHELL32.dll String @ LPSHFILEOPSTRUCT *------------------------------------------------------------------------------------------------------------------------------------------ *函数功能:该函数可以获得与调用线程的消息队列相关的活动窗口的窗口句柄。 DECLARE Integer GetActiveWindow IN WIN32API *------------------------------------------------------------------------------------------------------------------------------------------ *ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。 DECLARE INTEGER ShellExecute IN "Shell32.dll" ; INTEGER hwnd, ; STRING lpVerb, ; STRING lpFile, ; STRING lpParameters, ; STRING lpDirectory, ; LONG nShowCmd oHeap =CREATEOBJECT("Heap") #define FO_COPY 2 #define FOF_NOCONFIRMATION 0x10 && 不提示用户. #define FOF_NOCONFIRMMKDIR 0x200 && 不创建新目录 *cSourceString = HOME() +"tools/*.*" +CHR(0) +CHR(0) **cSourceString = JUSTPATH(getdir()) +"/*.*"+CHR(0) +CHR(0) &&目录下所有的文件拷贝 cSourceString =filename+ CHR(0) +CHR(0) MESSAGEBOX(cSourceString,0,"信息窗口") &&拷贝单个的文件 cDestString = pathname + CHR(0) +CHR(0) *cDestString = "C:/temp" + CHR(0) +CHR(0) nStringBase = oHeap.AllocBlob(cSourceString+cDestString) cFileOpStruct = NumToDWORD(GetActiveWindow())+; NumToDWORD(FO_COPY)+; NumToDWORD(nStringBase)+; NumToDWORD(nStringBase + LEN(cSourceString))+; NumToDWORD(FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR)+; CHR(0)+; NumToDWORD(0)+NumToDWORD(0) IF SHFileOperation(cFileOpStruct) = 0 MESSAGEBOX("文件已成功地复制",0,"信息窗口" ) * =ShellExecute(0, "open","//shenzhen-pic/test$", NULL, NULL, 1) &&打开目录 ELSE MESSAGEBOX("复制文件时发生错误",0,"信息窗口" ) * WAIT WINDOW NOWAIT"复制文件时发生错误" ENDIF oHeap = '' *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ************************************************************************************************************************* *创建一个用户自定义类或子类,并为创建的类或子类指定属性、事件和方法。 *通过指定 ParentClass 为 Custom,可以创建一个非可视的用户自定义类。 DEFINE CLASS heap AS custom PROTECTED inHandle, inNumAllocsActive,iaAllocs[1,3] inHandle = NULL inNumAllocsActive = 0 iaAllocs = NULL Name = "heap" *---------------------------------------------------------------------------------- PROCEDURE Alloc * 分配一个块, 返回一个指向它的指针 LPARAMETER nSize DECLARE INTEGER HeapAlloc IN WIN32API AS HAlloc; INTEGER hHeap, ; INTEGER dwFlags, ; INTEGER dwBytes DECLARE INTEGER HeapSize IN WIN32API AS HSize ; INTEGER hHeap, ; INTEGER dwFlags, ; INTEGER lpcMem LOCAL nPtr WITH this nPtr = HAlloc(.inHandle, 0, @nSize) IF nPtr # 0 * 块分配数组 .inNumAllocsActive = .inNumAllocsActive + 1 DIMENSION .iaAllocs[.inNumAllocsActive,3] * 指针 .iaAllocs[.inNumAllocsActive,1] = nPtr * 分配的实际大小 - 由HeapSize() 获得 .iaAllocs[.inNumAllocsActive,2] = HSize(.inHandle, 0, nPtr) .iaAllocs[.inNumAllocsActive,3] = .T. ELSE * HeapAlloc() 失败- 返回NULL指针 nPtr = NULL ENDIF ENDWITH RETURN nPtr ENDPROC *------------------------------------------------------------------------------------ FUNCTION AllocBLOB * 分配一块能传递BString大小的内存, The LPARAMETER cBStringToCopy LOCAL nAllocPtr WITH this nAllocPtr = .Alloc(LEN(cBStringToCopy)) IF ! ISNULL(nAllocPtr) .CopyTo(nAllocPtr,cBStringToCopy) ENDIF ENDWITH RETURN nAllocPtr ENDFUNC *------------------------------------------------------------------------------------ FUNCTION AllocString LPARAMETER cString RETURN this.AllocBLOB(cString + CHR(0)) ENDFUNC *------------------------------------------------------------------------------------ FUNCTION AllocInitAs * 分配一块内存初始化成CHR(nByteValue) LPARAMETER nSizeOfBuffer, nByteValue IF TYPE('nByteValue') # 'N' OR ! BETWEEN(nByteValue,0,255) * 缺省初始化为nulls nByteValue = 0 ENDIF RETURN this.AllocBLOB(REPLICATE(CHR(nByteValue),nSizeOfBuffer)) ENDFUNC *------------------------------------------------------------------------------------ PROCEDURE DeAlloc * 丢弃以前的分配块 LPARAMETER nPtr DECLARE INTEGER HeapFree IN WIN32API AS HFree ; INTEGER hHeap, ; INTEGER dwFlags, ; INTEGER lpMem LOCAL nCtr nCtr = NULL WITH this nCtr = .FindAllocID(nPtr) IF ! ISNULL(nCtr) =HFree(.inHandle, 0, nPtr) .iaAllocs[nCtr,3] = .F. ENDIF ENDWITH RETURN ! ISNULL(nCtr) ENDPROC *-------------------------------------------------------------------------------------- PROCEDURE CopyTo * 复制一个VFP字符串到块 LPARAMETER nPtr, cSource DECLARE RtlMoveMemory IN WIN32API AS RtlCopy ; INTEGER nDestBuffer, ; STRING @pVoidSource, ; INTEGER nLength LOCAL nCtr nCtr = NULL IF TYPE('nPtr') = 'N' AND TYPE('cSource') $ 'CM' ; AND ! (ISNULL(nPtr) OR ISNULL(cSource)) WITH this * 通过nPtr查找分配点 nCtr = .FindAllocID(nPtr) IF ! ISNULL(nCtr) * 复制较小的缓冲区或原字符串 =RtlCopy((.iaAllocs[nCtr,1]), ; cSource, ; MIN(LEN(cSource),.iaAllocs[nCtr,2])) ENDIF ENDWITH ENDIF RETURN ! ISNULL(nCtr) ENDPROC *------------------------------------------------------------------------------------- PROCEDURE CopyFrom * 复制缓冲区内容到VFP LPARAMETER nPtr DECLARE RtlMoveMemory IN WIN32API AS RtlCopy ; STRING @DestBuffer, ; INTEGER pVoidSource, ; INTEGER nLength LOCAL nCtr, uBuffer uBuffer = NULL nCtr = NULL IF TYPE('nPtr') = 'N' AND ! ISNULL(nPtr) WITH this * 查找地址为nPtr的分配块 nCtr = .FindAllocID(nPtr) IF ! ISNULL(nCtr) * 在VFP中分配一个足够大的缓冲区来接收块 uBuffer = REPL(CHR(0),.iaAllocs[nCtr,2]) =RtlCopy(@uBuffer, ; (.iaAllocs[nCtr,1]), ; (.iaAllocs[nCtr,2])) ENDIF ENDWITH ENDIF RETURN uBuffer ENDPROC *------------------------------------------------------------------------------------ PROTECTED FUNCTION FindAllocID LPARAMETER nPtr LOCAL nCtr WITH this FOR nCtr = 1 TO .inNumAllocsActive IF .iaAllocs[nCtr,1] = nPtr AND .iaAllocs[nCtr,3] EXIT ENDIF ENDFOR RETURN IIF(nCtr <= .inNumAllocsActive,nCtr,NULL) ENDWITH ENDPROC *------------------------------------------------------------------------------------- PROCEDURE SizeOfBlock * 检索一个分配块的实际内存大小 LPARAMETERS nPtr LOCAL nCtr, nSizeOfBlock nSizeOfBlock = NULL WITH this * 查找地址为nPtr的分配块 nCtr = .FindAllocID(nPtr) RETURN IIF(ISNULL(nCtr),NULL,.iaAllocs[nCtr,2]) ENDWITH ENDPROC *------------------------------------------------------------------------------------ PROCEDURE Destroy DECLARE HeapDestroy IN WIN32API AS HDestroy ; INTEGER hHeap LOCAL nCtr WITH this FOR nCtr = 1 TO .inNumAllocsActive IF .iaAllocs[nCtr,3] .Dealloc(.iaAllocs[nCtr,1]) ENDIF ENDFOR HDestroy[.inHandle] ENDWITH DODEFAULT() ENDPROC *------------------------------------------------------------------------------------ PROCEDURE Init DECLARE INTEGER HeapCreate IN WIN32API AS HCreate ; INTEGER dwOptions, ; INTEGER dwInitialSize, ; INTEGER dwMaxSize #DEFINE SwapFilePageSize 4096 #DEFINE BlockAllocSize 2 * SwapFilePageSize WITH this .inHandle = HCreate(0, BlockAllocSize, 0) DIMENSION .iaAllocs[1,3] .iaAllocs[1,1] = 0 .iaAllocs[1,2] = 0 .iaAllocs[1,3] = .F. .inNumAllocsActive = 0 ENDWITH RETURN (this.inHandle # 0) ENDPROC ENDDEFINE *********************************************************************************************************************** *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX *--------------------------------------------------------------------------------------- FUNCTION SetMem LPARAMETERS nPtr, cSource DECLARE RtlMoveMemory IN WIN32API AS RtlCopy ; INTEGER nDestBuffer, ; STRING @pVoidSource, ; INTEGER nLength RtlCopy(nPtr, ; cSource, ; LEN(cSource)) RETURN .T. *--------------------------------------------------------------------------------------- FUNCTION GetMem LPARAMETERS nPtr, nLen DECLARE RtlMoveMemory IN WIN32API AS RtlCopy ; STRING @DestBuffer, ; INTEGER pVoidSource, ; INTEGER nLength LOCAL uBuffer * 分配一个足够大的缓冲区来接收块 uBuffer = REPL(CHR(0),nLen) =RtlCopy(@uBuffer, ; nPtr, ; nLen) RETURN uBuffer *--------------------------------------------------------------------------------------- FUNCTION GetMemString LPARAMETERS nPtr, nSize DECLARE INTEGER lstrcpyn IN WIN32API AS StrCpyN ; STRING @ lpDestString, ; INTEGER lpSource, ; INTEGER nMaxLength LOCAL uBuffer IF TYPE('nSize') # 'N' OR ISNULL(nSize) nSize = 512 ENDIF * 分配一个足够大的缓冲区来接收数据 uBuffer = REPL(CHR(0), nSize) IF StrCpyN(@uBuffer, nPtr, nSize-1) # 0 uBuffer = LEFT(uBuffer, MAX(0,AT(CHR(0),uBuffer) - 1)) ELSE uBuffer = NULL ENDIF RETURN uBuffer *---------------------------------------------------------------------------------------- FUNCTION SHORTToNum * 转变结构中的16位带符号整数到VFP数值型 LPARAMETER tcInt LOCAL b0,b1,nRetVal b0=asc(tcInt) b1=asc(subs(tcInt,2,1)) if b1<128 nRetVal=b1 * 256 + b0 else b1=255-b1 b0=256-b0 nRetVal= -( (b1 * 256) + b0) endif return nRetVal *----------------------------------------------------------------------------------------- FUNCTION NumToSHORT LPARAMETER tnNum LOCAL b0,b1,x IF tnNum>=0 x=INT(tnNum) b1=INT(x/256) b0=MOD(x,256) ELSE x=INT(-tnNum) b1=255-INT(x/256) b0=256-MOD(x,256) IF b0=256 b0=0 b1=b1+1 ENDIF ENDIF RETURN CHR(b0)+CHR(b1) *------------------------------------------------------------------------------ FUNCTION DWORDToNum LPARAMETER tcDWORD LOCAL b0,b1,b2,b3 b0=asc(tcDWORD) b1=asc(subs(tcDWORD,2,1)) b2=asc(subs(tcDWORD,3,1)) b3=asc(subs(tcDWORD,4,1)) RETURN ( ( (b3 * 256 + b2) * 256 + b1) * 256 + b0) *------------------------------------------------------------------------------ FUNCTION NumToDWORD LPARAMETER tnNum RETURN NumToLong(tnNum) FUNCTION WORDToNum LPARAMETER tcWORD RETURN (256 * ASC(SUBST(tcWORD,2,1)) ) + ASC(tcWORD) *------------------------------------------------------------------------------ FUNCTION NumToWORD LPARAMETER tnNum LOCAL x x=INT(tnNum) RETURN CHR(MOD(x,256))+CHR(INT(x/256)) *------------------------------------------------------------------------------ FUNCTION NumToLong LPARAMETER tnNum DECLARE RtlMoveMemory IN WIN32API AS RtlCopyLong ; STRING @pDestString, ; INTEGER @pVoidSource, ; INTEGER nLength LOCAL cString cString = SPACE(4) =RtlCopyLong(@cString, BITOR(tnNum,0), 4) RETURN cString *------------------------------------------------------------------------------ FUNCTION LongToNum LPARAMETER tcLong DECLARE RtlMoveMemory IN WIN32API AS RtlCopyLong ; INTEGER @ DestNum, ; STRING @ pVoidSource, ; INTEGER nLength LOCAL nNum nNum = 0 =RtlCopyLong(@nNum, tcLong, 4) RETURN nNum *------------------------------------------------------------------------------ FUNCTION AllocNetAPIBuffer LPARAMETER nSize IF TYPE('nSize') # 'N' OR nSize <= 0 * 传递地非法参数,所以返回NULL RETURN NULL ENDIF IF ! 'NT' $ OS() * 仅支持在NT下API调用, 所以返回失败 RETURN NULL ENDIF DECLARE INTEGER NetApiBufferAllocate IN NETAPI32.DLL ; INTEGER dwByteCount, ; INTEGER lpBuffer LOCAL nBufferPointer nBufferPointer = 0 IF NetApiBufferAllocate(INT(nSize), @nBufferPointer) # 0 * 调用错误,所以返回一个NULL值 nBufferPointer = NULL ENDIF RETURN nBufferPointer *------------------------------------------------------------------------------ FUNCTION DeAllocNetAPIBuffer LPARAMETER nPtr IF TYPE('nPtr') # 'N' * 传递了非法参数,所以返回失败 RETURN .F. ENDIF IF ! 'NT' $ OS() * 仅支持在NT下API调用, 所以返回失败 RETURN .F. ENDIF DECLARE INTEGER NetApiBufferFree IN NETAPI32.DLL ; INTEGER lpBuffer RETURN (NetApiBufferFree(INT(nPtr)) = 0) Function CopyDoubleToString LPARAMETER nDoubleToCopy DECLARE RtlMoveMemory IN WIN32API AS RtlCopyDbl ; STRING @DestString, ; DOUBLE @pVoidSource, ; INTEGER nLength LOCAL cString cString = SPACE(8) =RtlCopyDbl(@cString, nDoubleToCopy, 8) RETURN cString *------------------------------------------------------------------------------ FUNCTION DoubleToNum LPARAMETER cDoubleInString DECLARE RtlMoveMemory IN WIN32API AS RtlCopyDbl ; DOUBLE @DestNumeric, ; STRING @pVoidSource, ; INTEGER nLength LOCAL nNum nNum = 0.000000000000000000 =RtlCopyDbl(@nNum, cDoubleInString, 8) RETURN nNum