Solved

Unable to create shortcut with VB6 on an iPAQ using ActiveSync CeSHCreateShortcut api call

Posted on 2006-11-30
15
384 Views
Last Modified: 2012-08-14
I've written an app that copies some files to an iPAQ. I just need to create a shortcut on the iPAQ that points to the executable I've installed. The rapi.dll that's part of ActiveSync includes a function called CeSHCreateShortcut. Microsoft's description: "This function creates a new shortcut on a remote Windows Embedded CE–based device." Just what I want to do. From the object browser:

Function CeSHCreateShortcut(lpszShortcut As String, lpszTarget As String) As Long
    Member of RemoteCETypeLibrary.CERAPI
    The CeSHCreateShortcut function creates a shortcut.

So I've got this declaration:

Public Declare Function CeSHCreateShortcut& Lib "rapi.dll" ( lpShortcut$, lpTarget$)

Used in this function:

Public Function RapiCreateShortcut(sShortcut$, sTarget$) As Boolean
  Dim lResult&
  lResult& = CeSHCreateShortcut(sShortcut$, sTarget$)
  RapiCreateShortcut = (lResult& <> 0)
End Function

And I call it something like this:

RapiCreateShortcut "ShortcutName", "\Program Files\Unichip\Flux\Application.exe"

It says it's successful, but I get a file with unprintable characters in its name in root of the PDA. It's not a valid shortcut.

Could someone please point out where I'm going wrong? Thanks.


0
Comment
Question by:ketchukf
  • 8
  • 7
15 Comments
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18051267
Whenever your passing a string in an API decleration you always pass the string Byval, in your decleration your passing it Byref, I also looked at the microsoft documentation and that function is asking for a string pointer so what you are seeing is most likely unicode characters.

From microsoft :

This function creates a shortcut. CeSHCreateShortcut is a remote application interface (RAPI), which enables an application running on a desktop computer to make function calls on a Windows CE–based device.


BOOL CeSHCreateShortcut(
LPWSTR lpszShortcut,
LPWSTR lpszTarget);


Although I can't test this myself you can try the following


Option Explicit

Private Declare Function CeSHCreateShortcut Lib "rapi.dll" ( _
    ByVal lpszShortcut As Long, _
    ByVal lpszTarget As Long) As Boolean

Public Function CreateShortcuts( _
    ByVal Shortcutname As String, _
    ByVal ShortCutTarget As String) As Boolean
   
        CreateShortcuts = CeSHCreateShortcut( _
            StrPtr(Shortcutname), _
            StrPtr(ShortCutTarget))
       
End Function


0
 

Author Comment

by:ketchukf
ID: 18053134
Thanks for the comment. I think what you're saying is usually true, but not in this case. From AllAPI.net:

Some types begin with LP. It is an abbreviation of Long Pointer to. So LPWORD is actually a memory location where the data is stored. No, you won't have to call a function to get this address. When you pass your argument ByRef (the default) you actually pass its address. The thing to remember here is that, if you parameter type begins with LP - you should pass it ByRef.

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18053685
hi, ketchukf,

The conversion I posted should be correct in Visual Basic 6 and if I am wrong pherhaps another expert could point this out.


So I take it what I have posted is not working correctly for you? Are you getting any type of errors?
0
 

Author Comment

by:ketchukf
ID: 18055116
I tried ByVal and I think CeGetLastError returned 'Invalid function call' or something like that. I'll try it again this evening and post the exact error.
0
 

Author Comment

by:ketchukf
ID: 18057982
eql1044,

I had a chance to test your code and CeSHCreateShortcut returns success, like it does with the code in my original question, but I don't have a shortcut. I've passed CeSHCreateShortcut  the name of the shortcut I want to create, the path and the name, the path and the name with a .lnk extension, and the result is always the same. No shortcut. Not that I can find, anyway.

I've been working with computers for a long time. I'll feel funny calling Microsoft and asking for help creating a shortcut.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18058162
Could you post how you are specifying the Shortcut and Target that you are using in regards to using the function I provided.

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 250 total points
ID: 18058220
What about something like this? I dont know how your device works exactly but I do know that on some devices you might need to power off the device or restart the device so the changes go into effect.

Option Explicit

Private Declare Function CeSHCreateShortcut Lib "rapi.dll" ( _
    ByVal lpszShortcut As Long, _
    ByVal lpszTarget As Long) As Boolean

Public Function CreateShortcuts( _
    ByVal ShortcutName As String, _
    ByVal ShortcutTarget As String) As Boolean
   
        CreateShortcuts = CeSHCreateShortcut( _
            StrPtr(ShortcutName), _
            StrPtr(ShortcutTarget))
       
End Function

Private Sub Form_Load()
Debug.Print CreateShortcuts("\windows\desktop\Name.lnk", """\Program Files\Folder\Name.exe\""")
End Sub
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ketchukf
ID: 18058448
If Not CreateShortcuts("Company Gadget", "\Program Files\Company\Gadget\Company Gadget.exe") Then ...

Like I said earlier, I've tried path and shortcut name, path and name and .lnk extension, same thing.

The files that I copy to the PDA show up as they're being copied. I can refresh the directory listing and see the size of the biggest file increase as it's being copied. I'm sure the shortcut would appear if it were there.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18061219
Have you tried with the double quotes?
0
 

Author Comment

by:ketchukf
ID: 18061273
Yes. Same result. CeSHCreateShortcut returns 0, but no shortcut. I tried deleting a shortcut to some other application and recreating it. Same result.
0
 

Author Comment

by:ketchukf
ID: 18090217
eql1044, thanks for your time and effort trying to help me with this problem.

Here's the answer I received on Usenet. It worked like a charm.

When you declare a function like this, VB does some work behind the
scenes to make it work. It converts the string as it's stored internally
to a null terminated ASCII string and passes it to the function (and
does the reverse conversion afterwards).

This normally works because you can call into the ASCII version of any
random Windows API. The RAPI functions, however, don't have an ASCII
version and expect Unicode strings. Here's an example of how you can do
this:

Option Explicit

' Using As Any declarations to avoid VB's string handling
Public Declare Function CeSHCreateShortcut Lib "rapi.dll" _
    (lpShortcut As Any, lpTarget As Any) As Long
Public Declare Function CeRapiInit Lib "rapi.dll" () As Long
Public Declare Function CeRapiUninit Lib "rapi.dll" () As Long

Public Function RapiCreateShortcut(sShortcut As String, _
    sTarget As String) As Boolean

    Dim lResult As Long
    Dim bShortcut() As Byte
    Dim bTarget() As Byte

    ' Use the byte array to get a Unicode version of
    '  the strings, ensuring a null character on the end
    bShortcut = sShortcut & vbNullChar
    bTarget = sTarget & vbNullChar

    lResult = CeSHCreateShortcut(bShortcut(0), bTarget(0))

    RapiCreateShortcut = (lResult <> 0)

End Function

Sub Example()
    CeRapiInit

    ' Note:  Quotes appear needed for the target name
    '  if there are embedded spaces.
    RapiCreateShortcut "\ShortcutName.lnk", _
        """\Program Files\Application.exe"""

    CeRapiInit
End Sub

--
Scott Seligman [MSFT]
This posting is provided AS IS with no warranties, and confers
no rights.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18095621
Great to hear its working, Have you by any chance tested my code with the proper formatting?
Adding Vbnullchar to the end? I still belive what I have posted is correct.

"\ShortcutName.lnk", _
        """\Program Files\Application.exe"""
0
 

Author Comment

by:ketchukf
ID: 18099067
Yes egl1044, I believe I tested every suggestion you offered. I tried appending vbNullChar to each string before I posted on EE, without any luck.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 18099073
I will have to admit, I left you on the wrong path and I must be wrong. Good luck
0
 

Author Comment

by:ketchukf
ID: 18110864
egl1044, I must have missed something because your earlier suggestion is essentially what I'm using now, and it works. I don't know if I had a typo or what.

Thanks for your help.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now