Quantcast
Viewing all articles
Browse latest Browse all 1449

Reason why you not shall use CreateSymbolicLinkW....

In this example I have used both the ole32 CreateSymbolicLinkW API which creates a copy of it self as a 0-bytes link.

Code:

Private Sub Command10_Click()
  Dim pISLW As IShellLinkW
  Dim pIPF As IPersistFile
  Dim pidl As Long
  Dim hr As Long
  Dim lRet As Long
 
  If CreateSymbolicLinkW(StrPtr("C:\PowerVB\New Link Test.lnk"), StrPtr("C:\Test.txt"), 0&) > 0 Then
    hr = m_cShell32.GetIShellLinkW(pISLW)
    If hr <> S_OK Then
      MsgBox "Error #" & Hex(hr), vbExclamation
      Exit Sub
    End If
    Set pIPF = pISLW
    pISLW.SetDescription "New Link Test"
    pISLW.SetPath "C:\Test.txt"
    pISLW.SetRelativePath "C:\PowerVB\New Link Test.lnk", 0
    pISLW.SetHotkey vbKeyEscape
    pISLW.SetWorkingDirectory "C:\PowerVB\"
    pISLW.SetShowCmd SW_SHOWNORMAL
    pISLW.SetArguments "Some New Aruments..."
    pISLW.SetIconLocation "shell32.dll", 22
    'pIPF.Save "C:\PowerVB\New Link Test.lnk", True
   
    SHParseDisplayName StrPtr("C:\PowerVB\New Link Test.lnk"), Nothing, pidl, 0, 0
   
    pISLW.SetIDList pidl
  Else
    MsgBox ErrorMessage(GetLastError()) & " Do you wanna delete this file?", vbExclamation Or vbYesNo
    If vbYes Then
      lRet = DeleteFile("C:\PowerVB\New Link Test.lnk")
      If lRet > 0 Then
        MsgBox "C:\PowerVB\New Link Test.lnk is now deleted!", vbInformation
      End If
    End If
  End If
 
End Sub

But as you see you are not able to do anyting with this IShellLinkW Interface AT ALL.
IShellLinkW becomes totally overflow.
Once a copy there is a copy and it is 0-bytes!!
Which not a file saved from IPersistFile will be!

And here is a link version with IPersistFile instead of CreateSymbolicLinkW

Both are so called "soft links"....

Code:

Private Sub Command10_Click()
  Dim pISLW As IShellLinkW
  Dim pIPF As IPersistFile
  Dim pidl As Long
  Dim hr As Long
  Dim lRet As Long
 
  'If CreateSymbolicLinkW(StrPtr("C:\PowerVB\New Link Test.lnk"), StrPtr("C:\Test.txt"), 0&) > 0 Then
    hr = m_cShell32.GetIShellLinkW(pISLW)
    If hr <> S_OK Then
      MsgBox "Error #" & Hex(hr), vbExclamation
      Exit Sub
    End If
    Set pIPF = pISLW
    pISLW.SetDescription "New Link Test"
    pISLW.SetPath "C:\Test.txt"
    pISLW.SetRelativePath "C:\PowerVB\New Link Test.lnk", 0
    pISLW.SetHotkey vbKeyEscape
    pISLW.SetWorkingDirectory "C:\PowerVB\"
    pISLW.SetShowCmd SW_SHOWNORMAL
    pISLW.SetArguments "Some New Aruments..."
    pISLW.SetIconLocation "shell32.dll", 22
    pIPF.Save "C:\PowerVB\New Link Test.lnk", True
   
    SHParseDisplayName StrPtr("C:\PowerVB\New Link Test.lnk"), Nothing, pidl, 0, 0
   
    pISLW.SetIDList pidl
'<--  I need to comment out this because IPeristFile doesn't care if file already exist.
 ' Else
 '  MsgBox ErrorMessage(GetLastError()) & " Do you wanna delete this file?", vbExclamation Or vbYesNo
 '  If vbYes Then
 '    lRet = DeleteFile("C:\PowerVB\New Link Test.lnk")
 '    If lRet > 0 Then
 '      MsgBox "C:\PowerVB\New Link Test.lnk is now deleted!", vbInformation
 '    End If
'    End If
'  End If
 
End Sub

I can the least say that I'm confused about both these two aproaches.
With CreateSymbolicLinkW I can't change the properties.
but with the IShellLinkW and the IPersistFile I can change and overwrite the file.

And for the third which did me most confused was the CreateHardLinkW API

Code:

Private Sub Command10_Click()
  Dim pISLW As IShellLinkW
  Dim pIPF As IPersistFile
  Dim pidl As Long
  Dim hr As Long
  Dim lRet As Long
 
  If CreateHardLinkW(StrPtr("C:\PowerVB\New Link Test.lnk"), StrPtr("C:\Test.txt"), 0) > 0 Then
    hr = m_cShell32.GetIShellLinkW(pISLW)
    If hr <> S_OK Then
      MsgBox "Error #" & Hex(hr), vbExclamation
      Exit Sub
    End If
    Set pIPF = pISLW
    pISLW.SetDescription "New Link Test"
    pISLW.SetPath "C:\Test.txt"
    pISLW.SetRelativePath "C:\PowerVB\New Link Test.lnk", 0
    pISLW.SetHotkey vbKeyEscape
    pISLW.SetWorkingDirectory "C:\PowerVB"
    pISLW.SetShowCmd SW_SHOWNORMAL
    pISLW.SetArguments "Some New Aruments..."
    pISLW.SetIconLocation "shell32.dll", 22
    'pIPF.Save "C:\PowerVB\New Link Test.lnk", pIPF.IsDirty
   
    SHParseDisplayName StrPtr("C:\PowerVB\New Link Test.lnk"), Nothing, pidl, 0, 0
   
    pISLW.SetIDList pidl
  Else
    MsgBox ErrorMessage(GetLastError()) & " Do you wanna delete this file?", vbExclamation Or vbYesNo
    If vbYes Then
      lRet = DeleteFile("C:\PowerVB\New Link Test.lnk")
      If lRet > 0 Then
        MsgBox "C:\PowerVB\New Link Test.lnk is now deleted!", vbInformation
      End If
    End If
  End If
 
End Sub

This created a link with same bytes as softlink but it could not be used at all.
It was just a static link object. The created object could not be used. And it can't be used for Folders as CreateSymbolicLink can.

Now I have showed three types of links that Win32 can create but only two of them make any sences?

There is a fourth actually but this is so confusing so I just mention its API CreateSymbolicLinkTransactedW.

I should be happy if any one could sort this out for me.

And I guess there is only one friend who can do this for me I guess ;):p

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>