AVIK DUTTA
AVIK DUTTA

Reputation: 746

Why is the adapter not working?

I have two applications named app and anotherapp respectively along with one class library myadp.dll

app contains:

using myadp;
namespace app
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void bt_Click(object sender, RoutedEventArgs e)
        {
            Class1 c = new Class1();
            tbinapp.Text = c.st;
        }
    }
}

anotherapp contains:

using myadp;
namespace anotherapp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void anotherbt_Click(object sender, RoutedEventArgs e)
        {
            Class1 ad = new Class1();
            ad.st = anothertb.Text;
        }
    }
}

And myadp.dll contains:

namespace myadp
{
    public class Class1
    {
      public  string st = "this is from adapter ";
    }
}

I am trying to pass a value from anotherapp to app using myadp as an adapter. But it is not working. I am assuming that it is not working because each application is creating a new instance of the class Class1 in myadp. Am I right? How do test this and fix it?

Upvotes: 4

Views: 192

Answers (2)

AVIK DUTTA
AVIK DUTTA

Reputation: 746

Finally it worked (here the adapter's readfromnotepad() is returning string that was on the edit fielt in the notepad )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using System.Threading.Tasks;
//using System.ServiceModel.Dispatcher;

namespace finally_adapter
{
    public class adapt_
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd);
        enum GetWindow_Cmd : uint {
   GW_HWNDFIRST = 0,
   GW_HWNDLAST = 1,
   GW_HWNDNEXT = 2,
   GW_HWNDPREV = 3,
   GW_OWNER = 4,
   GW_CHILD = 5,
   GW_ENABLEDPOPUP = 6
}

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

        [DllImport("User32.dll")]
        public static extern Int32 FindWindow(String lpClassName, String lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);




        int len;
        public string strin;
        const int WM_GETTEXT = 0x000D;
        StringBuilder sb = new StringBuilder(100);
        IntPtr hwnd;
        public adapt_()
        {

        }
        public string readfromnotepad()
        {
            string lpclassname = "Edit";
            string lpwindowname = "Notepad";
            int temp = FindWindow(lpwindowname,null);
            hwnd = (IntPtr)temp;
            StringBuilder temps = new StringBuilder(100);
            IntPtr edit_hwnd = GetWindow(hwnd, GetWindow_Cmd.GW_CHILD);
            IntPtr msg_hwnd = SendMessage(edit_hwnd, WM_GETTEXT, (IntPtr)100, temps);

            return temps.ToString();


        }


    }
}

Upvotes: 2

roryok
roryok

Reputation: 9645

You are correct in that each app is creating a new instance of Class1, however it is deeper than that. You seem to be misunderstanding a basic part of how classes work in code

Two apps which share the namespace myadp can both create the class Class1 in that namespace, however the instances of those classes are not shared between them.

Think of it as two kids who each have a set of instructions for building a lego house. Using the blocks they have, they can both build the same kind of house, but they're not the same house. If kid A builds a house, kid B does not have that house to play with, he has to build his own.

To fix your problem, you need somewhere to store the data, like a database, or a public method in one app which can be accessed by another.

Upvotes: 1

Related Questions