Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

CreateHardLinkA function (winbase.h)

  • 2023-06-01
Feedback

In this article

Establishes a hard link between an existing file and a new file. This function is onlysupported on the NTFS file system, and only for files, not directories.

To perform this operation as a transacted operation, use theCreateHardLinkTransacted function.

Syntax

BOOL CreateHardLinkA(  [in] LPCSTR                lpFileName,  [in] LPCSTR                lpExistingFileName,       LPSECURITY_ATTRIBUTES lpSecurityAttributes);

Parameters

[in] lpFileName

The name of the new file.

This parameter may include the path but cannot specify the name of a directory.

By default, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, seeNaming Files, Paths, and Namespaces.

Tip

Starting with Windows 10, Version 1607, you can opt-in to remove the MAX_PATH limitation without prepending "\\?\". See the "Maximum Path Length Limitation" section ofNaming Files, Paths, and Namespaces for details.

[in] lpExistingFileName

The name of the existing file.

This parameter may include the path cannot specify the name of a directory.

By default, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, seeNaming Files, Paths, and Namespaces.

Tip

Starting with Windows 10, Version 1607, you can opt-in to remove the MAX_PATH limitation without prepending "\\?\". See the "Maximum Path Length Limitation" section ofNaming Files, Paths, and Namespaces for details.

lpSecurityAttributes

Reserved; must beNULL.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero (0). To get extended error information, callGetLastError.

The maximum number of hard links that can be created with this function is 1023 per file. If more than 1023 links are created for a file, an error results.

If you pass a name longer than MAX_PATH characters to thelpFileName orlpExistingFileName parameter of the ANSI version of this function or to the Unicode version of this function without prepending "\\?\" to the path, the function returns ERROR_PATH_NOT_FOUND.

Remarks

Any directory entry for a file that is created withCreateFile orCreateHardLink is a hard link to an associated file.An additional hard link that is created with theCreateHardLinkfunction allows you to have multiple directory entries for a file, that is, multiple hard links to the same file,which can be different names in the same directory, or the same or different names in different directories.However, all hard links to a file must be on the same volume.

Because hard links are only directory entries for a file, many changes to that file are instantly visible toapplications that access it through the hard links that reference it. However, the directory entry size andattribute information is updated only for the link through which the change was made.

The security descriptor belongs to the file to which a hard link points. The link itself is only a directoryentry, and does not have a security descriptor. Therefore, when you change the security descriptor of a hard link,you a change the security descriptor of the underlying file, and all hard links that point to the file allow thenewly specified access. You cannot give a file different security descriptors on a per-hard-link basis.

This function does not modify the security descriptor of the file to be linked to, even if security descriptorinformation is passed in thelpSecurityAttributes parameter.

UseDeleteFile to delete hard links. You can deletethem in any order regardless of the order in which they are created.

Flags, attributes, access, and sharing that are specified inCreateFile operate on a per-file basis. That is, if youopen a file that does not allow sharing, another application cannot share the file by creating a new hard link tothe file.

When you create a hard link on the NTFS file system, the file attribute information in the directory entry isrefreshed only when the file is opened, or whenGetFileInformationByHandle is called withthe handle of a specific file.

Symbolic link behavior—If the path points to a symbolic link, the function creates a hardlink to the symbolic link.

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

TechnologySupported
Server Message Block (SMB) 3.0 protocolYes
SMB 3.0 Transparent Failover (TFO)No
SMB 3.0 with Scale-out File Shares (SO)No
Cluster Shared Volume File System (CsvFS)Yes
Resilient File System (ReFS)No
 

Note that SMB 3.0 does not support creation of hard links on shares with continuous availability capability.

Examples

The following code snippet shows you how to callCreateHardLink so that it does not modify the securitydescriptor of a file. ThepszExistingFileName parameter can be the original file name,or any existing link to a file. After this code is executed,pszNewLinkName refers tothe file.

  BOOL fCreatedLink = CreateHardLink( pszNewLinkName,                                       pszExistingFileName,                                       NULL ); // reserved, must be NULL  if ( fCreatedLink == FALSE )   {    ;// handle error condition   }

Note

The winbase.h header defines CreateHardLink as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, seeConventions for Function Prototypes.

Requirements

RequirementValue
Minimum supported clientWindows XP [desktop apps only]
Minimum supported serverWindows Server 2003 [desktop apps only]
Target PlatformWindows
Headerwinbase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See also

CreateFile

CreateHardLinkTransacted

DeleteFile

File Management Functions

Hard Links and Junctions

Symbolic Links


Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo