Q : How To P/Invoke SHChangeNotifyRegister?

M

Marauderz

The fun never ends with P/Invoke does it? :) Anyway I'm trying to call the
function SHChangeNotifyRegister which is defined as :-

BOOL WINAPI SHChangeNotifyRegister (
HWND hwnd,
SHCHANGENOTIFYENTRY * pshcne );
-----
It needs a pointer to a SHCHANGENOTIFYENTRY structure which looks like this

typedef struct tagSHCHANGENOTIFYENTRY {
DWORD dwEventMask;
LPTSTR pszWatchDir;
BOOL fRecursive;
} SHCHANGENOTIFYENTRY;

-------
This is the definitions I used for the function and structure

<DllImport("coredll.dll")> _
Public Shared Function SHChangeNotifyRegister(ByVal hwnd As IntPtr,
ByVal oNotify As SHChangeNotifyEntry) As Int32
End Function

Public Class SHChangeNotifyEntry
Public EventMask As Int32
Public WatchDir As String
Public Recursize As Int32
End Class
--------

But I'm getting a NotSupported exception when I call it.. can someone tell
me what I'm doing wrong? As MY SHChangeNotifyEntry is a class and all
classes are passed via it's pointer I CAN pass it ByVal to the Native DLL
right?

Thanks for any help guys
Marauderz
 
A

Alex Feinman [MVP]

FOr a class/structure to be marshallable it should contain only "blittable"
types:
http://msdn.microsoft.com/library/d...ide/html/cpconblittablenon-blittabletypes.asp
In your case the probllem is caused by String member. You need to allocate
unmanaged memory using LocalAlloc and then use Encoding.Unicode.GetBytes and
Marshal.Copy to copy the path to that memory block. The structure definition
needs to be modified to use IntPtr instead of String. Don't forget to call
LocalFree when you are done
 
M

Marauderz

*Ack* oh well. We do what we must.. :p I'm trying to detect insert and
removal of the SD card.. If you're familiar with the APIs think it's
possible to do it in .Net along with the MessageWindow class to recieve the
WM_FILECHANGEINFO message?
 
G

Guest

Hi

Would it be too much trouble to ask you to share the snippet of code that employs FileSystemWatcher to detect insert/remove of memory card. Below is my snippet of code which does not seem to detect the event. Thanks

Ge
=============================================

// default to fals
IsFullFramework = false

#region FULL_FRAMEWOR
IsFullFramework = System.Environment.OSVersion.Platform != PlatformID.WinCE
#endregio

if (! IsFullFramework

string[] CFNames = StorageCard.GetStorageCardNames()
// IPaq has internal RAM card, first storage card is the external CF card
if (CFNames.Length < 2
MessageBox.Show("Please insert RAM Card", "Notice")
els
{
DirectoryInfo[] CFDirectoryList = StorageCard.GetStorageCards();
FileWatcher = new FileSystemWatcher(CFDirectoryList[0].FullName)
FileWatcher.Changed += new FileSystemEventHandler(fsw_Changed)
FileWatcher.Created += new FileSystemEventHandler(fsw_Changed)
FileWatcher.Deleted += new FileSystemEventHandler(fsw_Changed)
FileWatcher.EnableRaisingEvents = true
FileWatcher.IncludeSubdirectories = true



private void fsw_Changed(object sender, FileSystemEventArgs e

MessageBox.Show("Please insert RAM Card", "Notice")

=====================================================================
----- Marauderz wrote: ----

seems to be EXACTLY what I need. hehehe ;) thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top