[RESOLVED] Does not recognize CreateSymbolicLink function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Does not recognize CreateSymbolicLink function

  1. #1
    Join Date
    Nov 2017
    Posts
    3

    [RESOLVED] Does not recognize CreateSymbolicLink function

    Hello guys,
    i wanted to use the CreateSymbolicLink function of WinAPI.
    Here is my code:
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
     
    int _tmain(int argc, LPTSTR argv[]) {
     
        if (argc != 3) {
            _ftprintf(stderr, _T("Usage: symhard srcfile destfile"));
            return EXIT_FAILURE;
        }
     
        if(!CreateSymbolicLink(argv[2], argv[1], SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) {
            _ftprintf(stdout, _T("Creating symboliclink failed: %lx"), GetLastError());
            return 1;
        }
     
        return 0;
    }
    I'm using MSYS2 and clang version 5.0.0:
    Code:
    clang -Wall -Werror -pedantic -std=c99 -c symbolic_vs_hardlink.c
    I always get the following output:
    Code:
    $ clang -Wall -Werror -pedantic -std=c99 -c symboliclink.c
    symboliclink.c:12:6: error: implicit declaration of function 'CreateSymbolicLink' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
            if(!CreateSymbolicLink(argv[2], argv[1], SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) {
                ^
    symboliclink.c:12:43: error: use of undeclared identifier 'SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE'
            if(!CreateSymbolicLink(argv[2], argv[1], SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) {
                                                     ^
    2 errors generated.
    It seems it does not know of the CreateSymbolicLink function nor the constant.
    When trying the same code with Visual Studio 2017 it works and it also finds the function.

    What am I doing wrong here?
    Last edited by 2kaud; November 4th, 2017 at 06:59 AM.

  2. #2
    Join Date
    Nov 2017
    Posts
    3

    Re: Does not recognize CreateSymbolicLink function

    Why can't I edit my post? Here is the well formated code:
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
     
    int _tmain(int argc, LPTSTR argv[]) {
     
        if (argc != 3) {
            _ftprintf(stderr, _T("Usage: symhard srcfile destfile"));
            return EXIT_FAILURE;
        }
     
        if(!CreateSymbolicLink(argv[2], argv[1], SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) {
            _ftprintf(stdout, _T("Creating symboliclink failed: %lx"), GetLastError());
            return 1;
        }
     
        return 0;
    }

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,837

    Re: Does not recognize CreateSymbolicLink function

    You can't edit the post as you need a certain number of posts (10?) before this facility is enabled. I'd edited your original post for this.

    Sorry I can't help you as I don't use clang, just VS.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

  4. #4
    Join Date
    Nov 2017
    Posts
    3

    Re: Does not recognize CreateSymbolicLink function

    If anybody is interested in the solution:
    I had to modify _WIN32_WINNT. After adding
    Code:
    #define _WIN32_WINNT 0x600
    before including the windows header file, it works!
    The function is only available in Vista+ and VS compiler seems to add this defines automatically.

    https://msdn.microsoft.com/en-us/library/6sehtctf.aspx

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,998

    Re: [RESOLVED] Does not recognize CreateSymbolicLink function

    Glad you have resolved your problem!
    So I mark this thread as resolved!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)