Sign in to follow this  
Followers 0
DickWms

AU3_WinGetHandle in C# Program

9 posts in this topic

I need help using AU3_WinGetHandle in a C# Program. My test program is shown below. It runs as expected with the last two lines commented out. But when the with AU3_WinGetHandle is active I get an error message saying "A problem caused the program to stop working correctly." and the only option is to close the program. I would appreciate if if someone points out my mistake.

Thank you - Dick Williams

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TryAutoitDLL
{
public partial class Form1 : Form
{
[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern int AU3_AutoItSetOption([MarshalAs(UnmanagedType.LPWStr)] string Option, int Value);

[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern void AU3_WinActivate([MarshalAs(UnmanagedType.LPWStr)]string Title, [MarshalAs(UnmanagedType.LPWStr)]string Text);

[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern void AU3_WinGetHandle([MarshalAs(UnmanagedType.LPWStr)]string Title, [MarshalAs(UnmanagedType.LPWStr)]string Text, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder RetText, int BufSize);

[DllImport("AutoItX3.dll", SetLastError = true, CharSet = CharSet.Auto)]
static public extern int AU3_ControlSend([MarshalAs(UnmanagedType.LPWStr)] string Title, [MarshalAs(UnmanagedType.LPWStr)] string Text, [MarshalAs(UnmanagedType.LPWStr)] string Control, [MarshalAs(UnmanagedType.LPWStr)] string SendText, int Mode);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
AU3_AutoItSetOption("WinTitleMatchMode", 2);
AU3_WinActivate("Notepad", "");
StringBuilder handle = new StringBuilder();

//AU3_WinGetHandle("Untitled - Notepad", "", handle, 200);
//int i = AU3_ControlSend("", "", handle.ToString(), "aaaa", 0);
}
}
}

Share this post


Link to post
Share on other sites



Logically, it would have to take three. One for the input, and two describing the output buffer. First being the buffer and the second being the length.

Share this post


Link to post
Share on other sites

You can't return a string in C.That is just asking for trouble.

Share this post


Link to post
Share on other sites

It's actually just easier to pass a char[] and to make a string out of it just call new string(buffer).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0