views:

323

answers:

5

Hi,

I m making windows mobile application, it refers some DLL's but i have some problem here. Imagine my application is installed in storage card related DLL's present in Storage card only, if i launch application it refers some of DLL, now i will remove the storage card, still my application will be running,it will not quit only ultimately it leads to restart of my mobile device.. i do get the divice card remove notification but its in DLL and its c++ code.. if card is removed no DLL will be present in storage card, i dont even get the notification also in c# i dont no how to get the card removed notification in c#. how to handle these scenarios please let me know.

Thanks

+1  A: 

Take a look at this article: (link). Search for Detecting the Status of Removable Storage Devices. I think this is what you are looking for.

Shaihi
Hey its OpenNetCF dude...its not free.. we need the source code of it.. we wont get it..for that we need to pay... no i dont want that...
Shadow
You can use the community Dll for free. Why do you care about the source code?
Shaihi
if my app is installed in storage card, then i get problem..ill take out the card, along with the card OpenNetCF dll also come out,who will be there to inform my app that device is removed..how to solve this issue?
Shadow
I have not tried this and am not sure of the outcome, but I think that once you use the Dll it is loaded to RAM until you finish using it and so you should still have the functionality. You can test and see or maybe someone in SO can tell the verdict.
Shaihi
+1  A: 

If you don't want to use OpenNETCF (not sure why you need the source code), the API call you need to use is RequestDeviceNotifications, and you will need to pass it a handle of a msg queue that is created by calling CreateMsqQueue.

Documentation is here

You will need to P/Invoke these calls from your managed code to register for the notification and then wait on an event for messages to arrive in the queue.

Matt
A: 

You can use the function SHChangeNotifyRegister to WM_FILECHANGEINFO message.

After you subscribe, you will be receiving events with ids such as SHCNE_DRIVEREMOVED or SHCNE_MEDIAREMOVED, so you can trigger some actions when storage card is removed.

To use native functions from the compact framework, use P/Invoke.

MannyNS
ya.. thats right... i have used this.. Problem is i get WM_FILECHANGEINFO message to my window-proc which is written in c++ and its present in a DLL, from DLL i perform sendmessage and it will come to windowproc of my form..I am not getting how to take out the form windowproc and recive all the notifications in form only with out using C++ dll code.. please help me if u know
Shadow
I am not aware of a method in which you can avoid native C++ code.
MannyNS
First We need to have window proc for a form, Set the new window proc by using API's "GetWindowLong" and "SetWindowLong" API's,and then have a dummy form in the .exe and register for WM_FILECHANGEINFO message. When card is removed i will get SHCNE_DRIVEREMOVED message,then kill the process.
Shadow
+3  A: 

Since you just absolutely have to have the source (just like you must have it for the Compact Framework itself and the OS too) and since $50 seems to be too steep for you to get the source for the SDF, here's my donation to you. This is right from the SDF code base (for anyone wondering, I own it, so I'm not violating any license):

public static Guid STORE_MOUNT_GUID = new Guid(_STORE_MOUNT_GUID);
internal const string _STORE_MOUNT_GUID = 
        "{C1115848-46FD-4976-BDE9-D79448457004}";

[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr RequestDeviceNotifications(
  byte[] devclass, IntPtr hMsgQ, bool fAll);

[DllImport("coredll.dll", SetLastError = true)]
private static extern bool StopDeviceNotifications(IntPtr h);

The Queue pointer can be retrieved using Daniel Moth's P2P queue implementation (though it has known bugs that are fixed in te SDF). I'm not posting the SDF implementation because it's way too long for a post.

At this point, it becomes as simple as this:

public void StartStatusMonitoring()
{
  if (!Active)
  {
    // Create a point-to-point message queue to get the notifications.
    m_p2pmq = new P2PMessageQueue(true);
    m_p2pmq.DataOnQueueChanged += new EventHandler(p2pmq_DataOnQueueChanged);

    // Ask the system to notify our message queue when devices of
    // the indicated class are added or removed.
    m_requestHandle = RequestDeviceNotifications(m_deviceClass.ToByteArray(),
                      m_p2pmq.Handle, m_fAll);
  }
}

I'm believe that, armed with this much info and the fact that your time has no intrinsic value, that you can string all of this together for a solution.

EDIT1

I should add to this that adding all of this code is not going to fix your issue 100%. If the code that contains this "fix" has not been JITted, or the JITted code has been pitched, and the IL for it resides in a page that is not in RAM when the storage card gets pulled, the device is still going to puke. This is true whether the code is "in" you running application code or in a separate assembly, so your belief that you have to have the source for this is erroneous.

Adding this fix certainly descreases the window of oppotunity for the bug to appear, but since the chance will still exist, and since Murphy's law certainly applies to software rollouts, you can almost guarantee that this is still going to happen in the field.

ctacke
Thank U so much..i will use it.. Thanks Lot
Shadow
A: 

If you have your app installed on a user removable storage card you will just wind up in a world of hurt. Strongly warn or outright forbid installation on removable media.