Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how difficult does getting an instance by handle have to be?

how difficult does getting an instance by handle have to be?

Scheduled Pinned Locked Moved C#
tutorialregexquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spazzman
    wrote on last edited by
    #1

    I want one instance of an application (let's call it instance2) to get another instance (instance1) of the same application. Instance2 already has the handle of instance1 - I've checked, and the handle's match. But in instance2, if I do : IntPtr hdlInstance1 = [whatever I do to get the correct handle]; Control myInstance1 = Control.FromChildHandle( hdlInstance1 ); then myInstance1 is always null. I can use Control.GetHandle(hdlInstance1) as well - that doesn't work either. Is there some special way I'm supposed to get instances by handles? I can't find any useful info online. And has anyone noticed how often code sites have the easiest, most trivial examples of a subject, like in this case, how to stop there being more than one instance of an application, but none of the sites will even dare doing something a bit more advanced like actually doing something with the second instance instead of just blocking it? It's almost like code sites just rip eachother off - one site puts up a simple example, then everyone else just copies it. Sigh .... rantmode off. Thanks in advance.

    H M 2 Replies Last reply
    0
    • S spazzman

      I want one instance of an application (let's call it instance2) to get another instance (instance1) of the same application. Instance2 already has the handle of instance1 - I've checked, and the handle's match. But in instance2, if I do : IntPtr hdlInstance1 = [whatever I do to get the correct handle]; Control myInstance1 = Control.FromChildHandle( hdlInstance1 ); then myInstance1 is always null. I can use Control.GetHandle(hdlInstance1) as well - that doesn't work either. Is there some special way I'm supposed to get instances by handles? I can't find any useful info online. And has anyone noticed how often code sites have the easiest, most trivial examples of a subject, like in this case, how to stop there being more than one instance of an application, but none of the sites will even dare doing something a bit more advanced like actually doing something with the second instance instead of just blocking it? It's almost like code sites just rip eachother off - one site puts up a simple example, then everyone else just copies it. Sigh .... rantmode off. Thanks in advance.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You can't create object references using handles from other processes. You need to P/Invoke SendMessage and send window messages to the other handle. This is documented in the .NET Framework SDK. You could, instead, use .NET Remoting or some other remote procedure calling (RPC) convention, which may be easier depending on what you need to do.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • S spazzman

        I want one instance of an application (let's call it instance2) to get another instance (instance1) of the same application. Instance2 already has the handle of instance1 - I've checked, and the handle's match. But in instance2, if I do : IntPtr hdlInstance1 = [whatever I do to get the correct handle]; Control myInstance1 = Control.FromChildHandle( hdlInstance1 ); then myInstance1 is always null. I can use Control.GetHandle(hdlInstance1) as well - that doesn't work either. Is there some special way I'm supposed to get instances by handles? I can't find any useful info online. And has anyone noticed how often code sites have the easiest, most trivial examples of a subject, like in this case, how to stop there being more than one instance of an application, but none of the sites will even dare doing something a bit more advanced like actually doing something with the second instance instead of just blocking it? It's almost like code sites just rip eachother off - one site puts up a simple example, then everyone else just copies it. Sigh .... rantmode off. Thanks in advance.

        M Offline
        M Offline
        MilesAhead
        wrote on last edited by
        #3

        What's the desired interraction between the app instances? One instance "getting" another isn't very specific. Give a bit more detail and maybe someone can get you started.

        S 1 Reply Last reply
        0
        • M MilesAhead

          What's the desired interraction between the app instances? One instance "getting" another isn't very specific. Give a bit more detail and maybe someone can get you started.

          S Offline
          S Offline
          spazzman
          wrote on last edited by
          #4

          The exact use is I need to send a string message from one instance to another (the string message being the file name the one instance wants the other one to open). This has to be done from the static Main() method. The simplified code could be : // this is the main "starting" class of my app class MainForm() { public static void Main(string[] args) { // gets instance of this app already running MainForm otherAppStance = objCleverWidget.GetOtherInstance(); // sends message to other instance and quits if (otherAppStance != null) { otherAppStance.TransferMessage(args[0]); return; } } public static void TransferMessage(string strMessage) { MessageBox.Show( "The other instance says" + strMessage ); } } So what I really need to know is how to make objCleverWidget.GetOtherInstance() This will return the instance of the main Window Form from the other app instance, and all my problems will be solved. That's the ideal solution. I haven't had any luck coding it, so I came up with a small but crude workaround - use P/Invoke's SendMessage. SendMessage is limited though - you can't send strings with it, just a message id in the form of an uint, and two pointers which I can't figure out properly. So I converted my string message into chars, converted each char to its uint equivalent, and sent those uints as message id's from one instance to another (obviously using some start- and end-message flags). It works and uses very little code, but I'm not too sure how well it will stand up to scrutiny. Oh, and it kinda destroys the recipient app's main form - you have to redraw after receiving messages. Doesn't sound too healthy ... I've seen two other solutions online, one in VB (on codeproject infact) which I dont understand too well (VB + interop = brainmelt), and the other using Remoting, which raises questions of its own (assumes user pc will have network support). I was surprised by how much code was used in both solutions - my workaround was pretty light. Anyway, associating an application with a file type is a pretty standard requirement, so I am a little puzzled by the lack of built-in functionality in the .Net framework for this kind of thing. If you can point out a practical solution that doesn't use networking, please let me know. I have reason to believe P/Invoke's SendMessage will do the trick, but I need to find out how to use it's two pointer parameters. Th

          M 1 Reply Last reply
          0
          • S spazzman

            The exact use is I need to send a string message from one instance to another (the string message being the file name the one instance wants the other one to open). This has to be done from the static Main() method. The simplified code could be : // this is the main "starting" class of my app class MainForm() { public static void Main(string[] args) { // gets instance of this app already running MainForm otherAppStance = objCleverWidget.GetOtherInstance(); // sends message to other instance and quits if (otherAppStance != null) { otherAppStance.TransferMessage(args[0]); return; } } public static void TransferMessage(string strMessage) { MessageBox.Show( "The other instance says" + strMessage ); } } So what I really need to know is how to make objCleverWidget.GetOtherInstance() This will return the instance of the main Window Form from the other app instance, and all my problems will be solved. That's the ideal solution. I haven't had any luck coding it, so I came up with a small but crude workaround - use P/Invoke's SendMessage. SendMessage is limited though - you can't send strings with it, just a message id in the form of an uint, and two pointers which I can't figure out properly. So I converted my string message into chars, converted each char to its uint equivalent, and sent those uints as message id's from one instance to another (obviously using some start- and end-message flags). It works and uses very little code, but I'm not too sure how well it will stand up to scrutiny. Oh, and it kinda destroys the recipient app's main form - you have to redraw after receiving messages. Doesn't sound too healthy ... I've seen two other solutions online, one in VB (on codeproject infact) which I dont understand too well (VB + interop = brainmelt), and the other using Remoting, which raises questions of its own (assumes user pc will have network support). I was surprised by how much code was used in both solutions - my workaround was pretty light. Anyway, associating an application with a file type is a pretty standard requirement, so I am a little puzzled by the lack of built-in functionality in the .Net framework for this kind of thing. If you can point out a practical solution that doesn't use networking, please let me know. I have reason to believe P/Invoke's SendMessage will do the trick, but I need to find out how to use it's two pointer parameters. Th

            M Offline
            M Offline
            MilesAhead
            wrote on last edited by
            #5

            Okay, I think a more straight-forward solution to what you want to do, provided both applications are on the same machine, is to use shared memory via Memory Mapped Files. C# is a bit of a pain in that it only gives the PInvoke mechanism for doing Win32 stuff, but using a page of swap file in a memory mapped file for shared memory isn't that difficult. There are a couple articles showing how to get at memory mapped files from C# on Code Project. Also you can use the tool at http://pinvoke.net to pull the prototypes adjusted for C# right into the editor for you. The main advantage of using a named memory mapped file with app instances is that you don't need to worry about handles. Using a GUID string in the memory mapped filename makes sure it's unique. So in your application you try to create the Memory Mapped File with CreateFileMapping(). If you get a handle but Marshal.GetLastWin32Error() returns ERROR_ALREADY_EXISTS then you're in the secondary instance. Otherwise if the creation succeeded you're in the primary. The secondary instance copies info it wants to send to the primary in the shared memory. The primary instance reads it out. To avoid mangling the data you can use a mutex to control access. Before it quits, the secondary instance gets the mutex, copies the info to the shared memory, releases the mutex, then exits(if that's what you want it to do.) The primary gets the mutex, checks for new data in the shared memory, if it finds it then it copies the data out, then releases the mutex. It sounds a bit more complicated than it is. I would look for examples of Memory Mapped Files. Using one page of system swap file is the simplest case with fewest system quirks to worry about. As I suggested, the TellTail component on www.torry.net does what you're trying to do. Delphi is pretty Enlish-like so even if you don't program in it often you can read the source and get the gist. Besides, Win32 calls are going to be pretty much the same no matter what language you call them with.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups