Click here to Skip to main content
15,897,518 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to get a Shell32 PDIL from a filename Pin
Chris Richner3-Jan-05 19:57
Chris Richner3-Jan-05 19:57 
GeneralRe: How to get a Shell32 PDIL from a filename Pin
Nick Parker4-Jan-05 5:42
protectorNick Parker4-Jan-05 5:42 
GeneralFieldTypes Pin
TyronX3-Jan-05 9:32
TyronX3-Jan-05 9:32 
GeneralRe: FieldTypes Pin
TyronX3-Jan-05 9:50
TyronX3-Jan-05 9:50 
GeneralSingleton with a twist Pin
bigals3-Jan-05 9:24
bigals3-Jan-05 9:24 
GeneralRe: Singleton with a twist Pin
Matt Gerrans4-Jan-05 8:08
Matt Gerrans4-Jan-05 8:08 
GeneralRe: Singleton with a twist Pin
bigals4-Jan-05 9:59
bigals4-Jan-05 9:59 
GeneralRe: Singleton with a twist Pin
Matt Gerrans7-Jan-05 20:42
Matt Gerrans7-Jan-05 20:42 
Sorry about the lengthy response time, but I got ravaged by some kind of stomach virus over the last few days. Dead | X|

Anyway, there is a Code Project article How to send a user-defined message with SendMessage, PostMessage or PostThreadMessage[^], by Georg Bedenk that I haven't read, so I'll give you a very brief description and assume that the article contains more detailed explanations, if you are interested in reading more.

Essentially, you need to define your own message, which is an integer value that won't conflict with existing Windows messages. You'll need to be able to call PostMessage() or SendMessages() Windows APIs (there may be a .NET library call for this -- Heath?) to send that message to another instance of your app. Your app will have to watch for that message and do whatever you want when it gets it (note that you could have multiple messages, or make use of lparam and wparam to get lots of different behaviors).

Here are all the chunks of code you'll need in your main form's class (which I called FormMain below), with some comments:

const int WM_APP = 0x8000;
const int WM_DO_SOMETHING_MAN = WM_APP + 1; // Your custom message ID.

// Get the PostMessage() API function:
[DllImport("User32.Dll")]
private static extern System.Int32 PostMessage(int hwnd, int msg, int lparam, int wparam);

// Your mutex:
private static Mutex AppMutex = new Mutex(true, "TestApp");

// The form's constructor:
public FormMain()
{
   InitializeComponent();

   // This is important; without it, OnNotifyMessage() is inert:
   SetStyle( ControlStyles.EnableNotifyMessage, true );
}

// The app's entrypoint:
[STAThread]
static void Main() 
{
   if(AppMutex.WaitOne(0, false))
   {
      Application.Run(new FormMain());
      AppMutex.ReleaseMutex();
   }
   else
   {
      // Look through the processes to find "the other one" that
      // is running and make sure this instance doesn't accidentally
      // find itself!
      Process thisProcess = Process.GetCurrentProcess();
      Process [] allProcesses = System.Diagnostics.Process.GetProcesses();
      foreach( Process process in allProcesses )
      {
         if( process.MainModule.FileName == Application.ExecutablePath )
         {
            if(process.Id != thisProcess.Id) // Not me?
            {
               int hwnd = process.MainWindowHandle.ToInt32();
               PostMessage( hwnd, WM_DO_SOMETHING_MAN, 0, 0 );
               break;
               // There is an itsy bitsy corner case bug here, if somehow
               // three or more instances started virtually simultaneously...
            }
         }
      }
   }
}

// The handler for the custom message:
protected override void OnNotifyMessage(Message m)
{
   if( m.Msg == WM_DO_SOMETHING_MAN )
      DoSomethingClever();  // Pop your window to the top, or whatever.
}


About the corner case bug mentioned above, there are a number of things you could do to make it more ironclad, if that is necessary. ...Actually, come to think of it, only "the one" instance that should get the messages from the stubs (all the Johnny-come-latelies that couldn't get the mutex) should have a non-zero MainWindowHandle, so adding that check may do the trick.

Anyway, you've got the gist of it and can proceed with the fine-tuning as you like.

(By the way, I had done this in C/C++ before, but not in C# or .NET before this).

Matt Gerrans
GeneralRe: Singleton with a twist Pin
bigals8-Jan-05 11:25
bigals8-Jan-05 11:25 
GeneralRe: Singleton with a twist Pin
Matt Gerrans9-Jan-05 19:51
Matt Gerrans9-Jan-05 19:51 
GeneralRe: Singleton with a twist Pin
bigals9-Jan-05 19:57
bigals9-Jan-05 19:57 
Question"How Get Type of File ? Pin
WDI3-Jan-05 6:14
WDI3-Jan-05 6:14 
AnswerRe: "How Get Type of File ? Pin
Bahadir Cambel3-Jan-05 9:05
Bahadir Cambel3-Jan-05 9:05 
AnswerRe: "How Get Type of File ? Pin
DavidNohejl3-Jan-05 10:11
DavidNohejl3-Jan-05 10:11 
GeneralRe: "How Get Type of File ? Pin
Bahadir Cambel3-Jan-05 10:22
Bahadir Cambel3-Jan-05 10:22 
GeneralRe: "How Get Type of File ? Pin
DavidNohejl3-Jan-05 10:51
DavidNohejl3-Jan-05 10:51 
GeneralRe: "How Get Type of File ? Pin
Bahadir Cambel3-Jan-05 11:42
Bahadir Cambel3-Jan-05 11:42 
General.NET Remote - Activator.GetObject Pin
Wender Oliveira3-Jan-05 5:21
Wender Oliveira3-Jan-05 5:21 
GeneralRe: .NET Remote - Activator.GetObject Pin
Adam Goossens9-Jan-05 23:48
Adam Goossens9-Jan-05 23:48 
GeneralFile to byte[ ] Pin
WDI3-Jan-05 4:43
WDI3-Jan-05 4:43 
GeneralRe: File to byte[ ] Pin
Wender Oliveira3-Jan-05 5:15
Wender Oliveira3-Jan-05 5:15 
GeneralRe: File to byte[ ] Pin
Dennis C. Dietrich3-Jan-05 7:40
Dennis C. Dietrich3-Jan-05 7:40 
QuestionHow to build patch Pin
Newbie_Toy3-Jan-05 3:34
Newbie_Toy3-Jan-05 3:34 
GeneralSaving Text Versions Pin
Darren Pruitt3-Jan-05 3:19
Darren Pruitt3-Jan-05 3:19 
GeneralRe: Saving Text Versions Pin
leppie3-Jan-05 5:55
leppie3-Jan-05 5:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.