Prototype, procedure - Return value is wrong?

Just starting out? Need help? Post your questions and find answers here.
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Prototype, procedure - Return value is wrong?

Post by highend »

Hi,

I want to create a symbolic link for a folder...

This will output "Result: 1" (in DebugView), regardless if I execute it (after compiling it) without admin permissions (which will fail, admin permissions are required). Why?

Code: Select all

; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
Prototype.i Prototype_CreateSymbolicLink(lpSymlinkFileName.p-unicode, lpTargetFileName.p-unicode, dwFlags.i = 0)

; Returns #True if creating the symbolic link was successful or #False, if not
Procedure.i CreateSymbolicLink(lpSymlinkFileName.s, lpTargetFileName.s, dwFlags.i = 0)
  Protected.i hDll, result = #True
  Protected CreateSymbolicLink_Proto.Prototype_CreateSymbolicLink

  ; dwFlags
  ; 0x0 = The link target is a file
  ; 0x1 = The link target is a directory

  hDll = OpenLibrary(#PB_Any, "Kernel32.dll")
  If hDll
    CreateSymbolicLink_Proto = GetFunction(hDll, "CreateSymbolicLinkW")

    If Not CreateSymbolicLink_Proto(lpSymlinkFileName, lpTargetFileName, dwFlags)
      result = #False
    EndIf
    CloseLibrary(hDll)
  Else
    result = #False
  EndIf

  ProcedureReturn result
EndProcedure

OutputDebugString_("Result: " + CreateSymbolicLink("C:\Users\username\AppData\Roaming\src_link", "D:\dst_target", 1))

breeze4me
Enthusiast
Enthusiast
Posts: 527
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Prototype, procedure - Return value is wrong?

Post by breeze4me »

The result value seems to be incorrectly explained in the page, or there's something else we don't know.
So, it is necessary to check with the GetLastError_() function.

Code: Select all

; https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsymboliclinkw
Prototype.i Prototype_CreateSymbolicLink(lpSymlinkFileName.s, lpTargetFileName.s, dwFlags.l = 0)

; Returns #True if creating the symbolic link was successful or #False, if not
Procedure.i CreateSymbolicLink(NewSymlinkFileName.s, ExistingFileName.s, dwFlags.l = 0)
  Protected.i hDll, result
  Protected CreateSymbolicLink_Proto.Prototype_CreateSymbolicLink

  ; dwFlags
  ; 0x0 = The link target is a file
  ; 0x1 = The link target is a directory

  hDll = OpenLibrary(#PB_Any, "Kernel32.dll")
  If hDll
    CreateSymbolicLink_Proto = GetFunction(hDll, "CreateSymbolicLinkW")
    
    If CreateSymbolicLink_Proto
      If CreateSymbolicLink_Proto(NewSymlinkFileName, ExistingFileName, dwFlags) And GetLastError_() = #ERROR_SUCCESS
        result = #True
      EndIf
    EndIf
    CloseLibrary(hDll)
  EndIf
  
  ProcedureReturn result
EndProcedure

Debug "Result: " + CreateSymbolicLink("D:\dst_target", "C:\Users\username\AppData\Roaming\src_link", 1)
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Re: Prototype, procedure - Return value is wrong?

Post by highend »

Thanks a lot, breeze4me, it works like a charm with that check!
Post Reply