Search code examples
vb.netvisual-studio-2008windows-cesmart-device

Trouble passing string to lpctstr in WinCE5 API call


I am trying to make a call to the Windows CE 5 API call, "FindFirstChangeNotification" in a VS2008 Smart Device project using:

Private Declare Function FindFirstChangeNotification Lib "coredll.dll" _
(ByVal lpPathName As String, ByVal bWatchSubtree As Long, _
ByVal dwNotifyFilter As Long) As Long

Dim strFolderPath As String = "\My Documents\My App Files\"
Dim ptrHandle as IntPtr = FindFirstChangeNotification(strFolderPath, 0, 1)

Attempting this method results in a "System.NotSupportedException" which I assume to be an incompatibility in string types. Despite attempting different marshaling behaviors I'm still stuck after several days.


Solution

  • String types in Windows CE are Unicode, so declaring as String should be correct.

    Coredll actually exports the function as FindFirstChangeNotificationW (note trailing 'W'), so that is likely the reason you're getting an exception.

    'W' indicates a Wide, as in wide-character or Unicode, implementation of the function. Generally you can use the dumpbin tool in a Visual Studio command prompt to identify the names of function exports, in this case I used dumpbin /exports coredll.dll to check.

    Also, as far as I know, in VB.Net Long is a 64-bit type, and FindFirstChangeNotification expects 32-bit arguments.

    So try this:

    Private Declare Function FindFirstChangeNotificationW Lib "coredll.dll" _
    (ByVal lpPathName As String, ByVal bWatchSubtree As Integer, _
    ByVal dwNotifyFilter As Integer) As Integer