gpt4 book ai didi

c# - 为什么适配器不工作?

转载 作者:太空狗 更新时间:2023-10-30 01:35:04 25 4
gpt4 key购买 nike

我有两个应用程序分别命名为 appanotherapp 以及一个类库 myadp.dll

app 包含:

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;
}
}
}

另一个应用包含:

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;
}
}
}

myadp.dll 包含:

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

我正在尝试使用 myadp 作为适配器将值从 anotherapp 传递到 app。但它不起作用。我假设它不起作用,因为每个应用程序都在 myadp 中创建类 Class1 的新实例。我对吗?如何测试并修复它?

最佳答案

终于成功了(这里适配器的 readfromnotepad() 返回记事本编辑字段上的字符串)

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();


}


}
}

关于c# - 为什么适配器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com