快捷方式如果是中文名,读取其参数就错误!请高手指点一下

zolen 2005-01-18 04:42:08
微软的vb里带的例子,shelllink读取快捷方式是中文名的时候老是读取不了。不是所有中文名的快捷方式都这样。很晕。哪位高手帮忙看看,到底怎么回事情。比如你随便弄个程序创建一个快捷方式,名字叫“梦幻西游.lnk”,那就读取不了快捷方式里的信息了。

微软例子里读取信息的类模块:
Public Enum STGM
STGM_DIRECT = &H0&
STGM_TRANSACTED = &H10000
STGM_SIMPLE = &H8000000
STGM_READ = &H0&
STGM_WRITE = &H1&
STGM_READWRITE = &H2&
STGM_SHARE_DENY_NONE = &H40&
STGM_SHARE_DENY_READ = &H30&
STGM_SHARE_DENY_WRITE = &H20&
STGM_SHARE_EXCLUSIVE = &H10&
STGM_PRIORITY = &H40000
STGM_DELETEONRELEASE = &H4000000
STGM_CREATE = &H1000&
STGM_CONVERT = &H20000
STGM_FAILIFTHERE = &H0&
STGM_NOSCRATCH = &H100000
End Enum

Public Function GetShellLinkInfo(LnkFile As String, ExeFile As String, WorkDir As String, _
ExeArgs As String, IconFile As String, IconIdx As Long, _
ShowCmd As Long) As Long
'---------------------------------------------------------------
Dim pidl As Long ' Item id list
Dim wHotKey As Long ' Hotkey to shortcut...
Dim fd As WIN32_FIND_DATA
Dim Description As String
Dim buffLen As Long
Dim cShellLink As ShellLinkA ' An explorer IShellLink instance
Dim cPersistFile As IPersistFile ' An explorer IPersistFile instance
'---------------------------------------------------------------
If (LnkFile = "") Then Exit Function ' Validate min. input requirements.

Set cShellLink = New ShellLinkA ' Create new IShellLink interface
Set cPersistFile = cShellLink ' Implement cShellLink's IPersistFile interface

' Load Shortcut file...(must do this UNICODE hack!)
'On Error GoTo ErrHandler
cPersistFile.Load StrConv(LnkFile, vbUnicode), STGM_DIRECT

With cShellLink
' Get command line exe name & path of shortcut
ExeFile = Space(MAX_PATH)
buffLen = Len(ExeFile)
.GetPath ExeFile, buffLen, fd, SLGP_UNCPRIORITY
Dim s As String
s = fd.cFileName ' Not returned to calling function

' Get working directory of shortcut
WorkDir = Space(MAX_PATH)
buffLen = Len(WorkDir)
.GetWorkingDirectory WorkDir, buffLen

' Get command line arguments of shortcut
ExeArgs = Space(MAX_PATH)
buffLen = Len(ExeArgs)
.GetArguments ExeArgs, buffLen

' Get description of shortcut
Description = Space(MAX_PATH)
buffLen = Len(Description)
.GetDescription Description, buffLen ' Not returned to calling function

' Get the HotKey for shortcut
.GetHotkey wHotKey ' Not returned to calling function

' Get shortcut icon location & index
IconFile = Space(MAX_PATH)
buffLen = Len(IconFile)
.GetIconLocation IconFile, buffLen, IconIdx

' Get Item ID List...
.GetIDList pidl ' Not returned to calling function

' Set shortcut's startup mode (min,max,normal)
.GetShowCmd ShowCmd
End With

GetShellLinkInfo = True ' Return Success
'---------------------------------------------------------------
ErrHandler:
'---------------------------------------------------------------
Set cPersistFile = Nothing ' Destroy Object
Set cShellLink = Nothing ' Destroy Object
'---------------------------------------------------------------
End Function
'----------------

//////////////////////////////
cPersistFile.Load StrConv(LnkFile, vbUnicode), STGM_DIRECT
这一句遇到某些中文名就出错!!!
...全文
166 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhtzz 2005-07-08
  • 打赏
  • 举报
回复
zolen 2005-01-18
  • 打赏
  • 举报
回复
还到不了长度那里,在cPersistFile.Load StrConv(LnkFile, vbUnicode), STGM_DIRECT这里就直接出错了。
tztz520 2005-01-18
  • 打赏
  • 举报
回复
应该是长度问题.

你将你的程序用到len的改为这样:

比如你有buffLen = Len(Description),就改为这样:buffLen = LenB(StrConv(Description, vbFromUnicode))


全部都改
zolen 2005-01-18
  • 打赏
  • 举报
回复
再顶一下,有不通过WSH那些来解决的方法吗?微软的例子怎么也有问题,晕呀。
zolen 2005-01-18
  • 打赏
  • 举报
回复
谢谢高手指点,这种方法我用过没有问题。但是我这边电脑上的FSO WSH全部被禁用了。所以你的方法用不了。微软的那个是用API搞的,不怕WSH被禁用。
dongge2000 2005-01-18
  • 打赏
  • 举报
回复
Option Explicit

Private Sub Command1_Click()
Shell GetTargetPath("C:\Documents and Settings\MyUserName\桌面\RealOne.lnk")
End Sub

Function GetTargetPath(ByVal FileName As String)
Dim Obj As Object
Dim Shortcut As Object
Set Obj = CreateObject("WScript.Shell")
Set Shortcut = Obj.CreateShortcut(FileName)
GetTargetPath = Shortcut.TargetPath
Shortcut.Save
End Function
zolen 2005-01-18
  • 打赏
  • 举报
回复
自己顶一下。是因为转换vbUnicode才出错的吗?

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧