当前位置:编程学习 > C/C++ >>

硬链接

#include <windows.h>
#include <string.h>
#include <stdio.h>

typedef BOOL (__stdcall *pCreateHardLink)(
                                          LPCTSTR lpFileName,
                                          LPCTSTR lpExistingFileName,
                                          LPSECURITY_ATTRIBUTES lpSecurityAttributes
                                          );

int main()
{
    HMODULE hlib = LoadLibrary("kernel32.dll");
    pCreateHardLink fCreateHardLink = (pCreateHardLink) GetProcAddress(hlib,"CreateHardLinkA"); 
    
    
    LPCTSTR str1 = "C:\\1.txt";
    LPCTSTR str2 = "c:\\2.txt";
    
    
    //保证2.txt在C盘根目录是存在的
    //且硬盘的分区格式为NTFS
    BOOL b = fCreateHardLink(str1,str2,NULL);
    
    if(b)    {
        HANDLE hFile = CreateFile("c:\\1.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
        
        DWORD dwNum = 0;
        
        char *pszStr = "asdf";
        
        //对1.txt进行写入观察2.txt的内容
        WriteFile(hFile,(LPCTSTR)pszStr,strlen("asdf"),&dwNum,NULL);
        
        CloseHandle(hFile);
        
    }
    else
    {
        printf("%d",GetLastError());
    }
    return 0;
}

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,