在C#中实现ImageSearchDLL.dll [英] Implement ImageSearchDLL.dll in C#
问题描述
我正在尝试在Visual Studio C#中使用ImageSearchDLL.dll.我曾经在AutoIt中使用它.
I am trying to use ImageSearchDLL.dll in Visual Studio C#. I used to use it in AutoIt.
以下是我想出的使用 Imagesearch 函数.但是,一旦它尝试调用ImageSearch,程序将毫无例外地崩溃. 我的项目文件夹中包含dll文件.可能是因为这样,尽管我能够在AutoIt中使用dll,但这并不意味着它也可以在C#中工作? 注意:我尝试了32位和64位.dll
Following is the code I came up with to use the Imagesearch Function. However, as soon as it tried to call ImageSearch, the program crashes without any exception. I have the dll file included in my project folder. Could it be because of that although I was able to use the dll in AutoIt, it doesn't mean it would work in C# as well? Note: I tried both 32 and 64 bits of .dll
[DllImport("ImageSearchDLL.dll")]
static extern string ImageSearch(int aLeft, int aTop, int aRight, int aBottom, string aImageFile);
public static int Search(String FilePath, int X1, int Y1, int X2, int Y2, ref int X, ref int Y, int tolerance, int resultPosition)
{
if (tolerance > 0)
{
FilePath = "*" + tolerance.ToString() + " " + FilePath;
}
string result = ImageSearch(X1, Y1, X2, Y2, "test.png"); **<- crash here**
string[] result_array = result.Split('|');
if (result_array[0] == "0" )
return 0;
X = Convert.ToInt32(result_array[2]);
Y = Convert.ToInt32(result_array[3]);
if (resultPosition == 1)
{
X = X + (Convert.ToInt32(result_array[4]))/2;
Y = Y + (Convert.ToInt32(result_array[5]))/2;
}
return 1;
}
推荐答案
这与使用C#无关.如果使用相同的参数(类型和值)调用该方法,则DLL必须以相同的方式工作.
This shouldn't have to do with using C#. If the method is called with the same parameters (type and value) the DLL has to work the same way.
我使用的方法签名与您使用的方法签名略有不同,并且对我有用:
I used a bit different method signature than yours and it worked for me:
[DllImport("ImageSearchDLL.dll")]
private static extern IntPtr ImageSearch(int x, int y, int right, int bottom, [MarshalAs(UnmanagedType.LPStr)]string imagePath);
除此之外,您应该验证是否找到了DLL并可以将其导入(考虑使用SetDLLDirectory
)
Other than that, you should verify that the DLL is found and can be imported (consider using SetDLLDirectory
)
[DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi)]
public static extern bool SetDllDirectory([MarshalAs(UnmanagedType.LPStr)] String pathName);
这是我使用ImageSearch
的方式,也许会对您有所帮助:
This is how I used the ImageSearch
maybe it'll help you:
public static String[] UseImageSearch(string imgPath)
{
int right = Screen.PrimaryScreen.WorkingArea.Right;
int bottom = Screen.PrimaryScreen.WorkingArea.Bottom;
IntPtr result = ImageSearch(0, 0, right, bottom, imgPath);
String res = Marshal.PtrToStringAnsi(result);
if (res[0] == '0') return null;//not found
String[] data = res.Split('|');
//0->found, 1->x, 2->y, 3->image width, 4->image height;
// Then, you can parse it to get x and y:
int x; int y;
int.TryParse(data[1], out x);
int.TryParse(data[2], out y);
return data;
}
这篇关于在C#中实现ImageSearchDLL.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!