How do I create a symlink in Windows Vista?

前端 未结 3 1195
执笔经年
执笔经年 2021-01-12 15:43

I am looking to create symlinks (soft links) from Java on a Windows Vista/ 2008 machine. I\'m happy with the idea that I need to call out to the JNI to do this. I am after h

3条回答
  •  执笔经年
    2021-01-12 16:14

    This has been on my list to try, from my notes:

    The API:

    http://msdn.microsoft.com/en-us/library/aa363866(VS.85).aspx

    BOOLEAN WINAPI CreateSymbolicLink(
      __in  LPTSTR lpSymlinkFileName,
      __in  LPTSTR lpTargetFileName,
      __in  DWORD dwFlags
    );
    

    Some C# examples:

    http://community.bartdesmet.net/blogs/bart/archive/2006/10/24/Windows-Vista-2D00-Creating-symbolic-links-with-C_2300_.aspx

    A C++ Example, this is cnp from another article I was reading. I have not tested it so use it with caution.

    typedef BOOL (WINAPI* CreateSymbolicLinkProc) (LPCSTR, LPCSTR, DWORD);
    
    void main(int argc, char *argv[]) 
    {
      HMODULE h;
      CreateSymbolicLinkProc CreateSymbolicLink_func;
      LPCSTR link = argv[1];
      LPCSTR target = argv[2];
      DWORD flags = 0;
    
      h = LoadLibrary("kernel32");
      CreateSymbolicLink_func =
        (CreateSymbolicLinkProc)GetProcAddress(h,
      if (CreateSymbolicLink_func == NULL) 
      {
         fprintf(stderr, "CreateSymbolicLinkA not available\n");
      } else 
      {
         if ((*CreateSymbolicLink_func)(link, target, flags) == 0) 
         {
            fprintf(stderr, "CreateSymbolicLink failed: %d\n",
            GetLastError());
    
      } else 
      {
         printf("Symbolic link created.");
      }
    }
    

    }

    Having said this, I would not use this code :-) I would either be inclined to fork mklink or look at the native library from jruby/jpython (Sorry I cant look it up atm as my network connection is flakey). I seem to recall that jruby has written a library that wraps up various posix apis into java (thinks like chown that are required for ruby compliance but are not cross platform). This library is being used by the jpython folks who seem very pleased with it. I would be surprised if this library does not offer sym link support.

提交回复
热议问题