Sign in to follow this  
Followers 0
JohnOne

PixelSearch C#

26 posts in this topic

Anyone help me out here?

I'm trying for a return from the function but getting error during debug

int[] cords = new int[2];
AX3.AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, cords);
recieving error:

An unhandled exception of type 'System.AccessViolationException' 
occurred in csharpsearchtest.exe

Additional information: Attempted to read or write protected memory. 
This is often an indication that other memory is corrupt.

The last argument I pass "cords" array, I think the function wants a pointer, but C# claims not to need/use pointers as far as I know.

I dont think its the rest of my code as the other functions I have tried work ok.

Any pointers? :blink:


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

According to the autoitx help the parameters are: "left, top, right, bottom, color [, shade-variation] [, step]]". It looks like you'll need to assign pixelsearch's return value into a variable. I've never used autoitx before however.

int cords;
cords = AX3.AU3_PixelSearch(0, 0, 800, 600, 15333544);

btw: what tag do you use to post c# code?

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites

The extra param, as far as I can tell, should be a pointer to an array to recieve the return fron the function.

Maybe I have not read far enough but C# reckons it dosent need pointers so I'm wondering if this function can be used in it.

code='C#'


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

According to this website (http://www.c-sharpcorner.com/UploadFile/rajeshvs/PointersInCSharp11112005051624AM/PointersInCSharp.aspx):

"C# also supports pointers in a limited extent. A pointer is nothing but a variable that holds the memory address of another type. But in C# pointer can only be declared to hold the memory address of value types and arrays."

edit: oh ok, I see, I must looking at either a help file for something else or an outdated helpfile.

I just read in this post (http://www.autoitscript.com/forum/index.php?showtopic=90733&st=0&p=655133&hl=au3_pixelsearch&fromsearch=1&#entry655133) that you'll need to do this...

; make cords point to an array of integers...
int[] *cords = new int[2];
AX3.AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, cords);

code='C#'

int ; doesn't work for me, odd

Edit: oh so NOW it works, oh ok

Edited by jaberwocky6669
closing tag is just [/code]

Share this post


Link to post
Share on other sites

C# supports pointers just fine. Instead, use ref int[] to keep it type safe.

Share this post


Link to post
Share on other sites

Still struggling here.

public unsafe void Func()
{
   int[] cords = new int[2];
   fixed (int* ptr = cords);
   AX3.AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, ptr);
}
Now it cries about the ptr argument being passed.

I should also mention that my original error was runtime, with this code here its compiletime.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

int[] cords = new int[2];
AX3.AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, ref cords);

And make the last parameter a ref int[] in the declaration.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

After trying everything I could find via searching and in this thread I still cannot get this to work.

@richard

I tried using the ref in the call and altering the definition, I also tried the out keyword without success, thanks for the suggestion.

Has anyone actually used this function successfulky?

I'd love to know how.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

From the help file

AU3_API void WINAPI AU3_PixelSearch(long nLeft, long nTop, long nRight, long nBottom, long nCol, /*default 0*/long nVar, /*default 1*/long nStep, LPPOINT pPointResult);

This says it's a point, not an array of int. This is the reason it's failing.

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public POINT(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public static implicit operator System.Drawing.Point(POINT p)
    {
        return new System.Drawing.Point(p.X, p.Y);
    }

    public static implicit operator POINT(System.Drawing.Point p)
    {
        return new POINT(p.X, p.Y);
    }
}

public class AU3
{
    [DllImport("autoitx.dll")]
    public static extern void AU3_PixelSearch(int nLeft, int nTop, int nRight, int nBottom, int nCol, int nVar, int nStep, ref POINT nPointResult);
}
Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Seems you cannot use that namespace in a console app.

Thanks for your code Richard, unfortunately its still causing me woes.

Argument 8: cannot convert from 'ref int[]' to 'ref CsharpConsolePixelsearch1.POINT'

Any chance of an example of how to call the function using POINT ?

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

You can use whatever namespace you want from any .Net app as long as you add the appropriate references. Just remove the two functions that contain the implicit conversions so you have the two fields and the constructor in POINT. It should work then.

Also, you don't use ref int[] anymore, you use a POINT which has a .X and .Y.

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public POINT(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

public class AU3
{
    [DllImport("autoitx.dll")]
    public static extern void AU3_PixelSearch(int nLeft, int nTop, int nRight, int nBottom, int nCol, int nVar, int nStep, ref POINT nPointResult);

    public static void Main()
    {
        POINT mypoint;
        AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, ref mypoint);
        System.Console.Writeline("Found at ({0}, {1})", mypoint.X, mypoint.Y);
    }
}

Are you sure you should be doing platform invoke when you don't even entirely understand C#?

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

Thanks once again Richard.

I know it can be annoying when someone is asking questions about a language they dont understand, and for that I apologise, but I'm not trying to understand C#, my brain is not wired that way, just trying to know how to use the AutoitX functions with it. Understanding it will come to me as I go along.

Does that example work for you? because I get an error...

Use of unassigned local variable 'mypoint'


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Seems like the function still doesn't work. It wasn't working in the past...

#include <windows.h>
#include <iostream>
#include "AutoIt3.h"

int filter(UINT, PEXCEPTION_POINTERS);

int main()
{
    LPPOINT p = new POINT();

    __try
    {
        __try
        {
            AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, p);
            std::cout << "x:" << p->x << ", Y:" << p->y << std::endl;
        }
        __except(filter(GetExceptionCode(), GetExceptionInformation()))
        {
            std::cout << "Exception caught!!!\n";
        }
    }
    __finally { delete p;}

    return 0;
}

int filter(UINT code, PEXCEPTION_POINTERS ep)
{
    if (code == EXCEPTION_ACCESS_VIOLATION)
    {
        std::cout << "Caught AV as expected.\n";
        return EXCEPTION_EXECUTE_HANDLER;
    }
    else
    {
        std::cout << "Didn't catch AV, unexpected.\n";
        return EXCEPTION_CONTINUE_SEARCH;
    }
}

It doesn't matter which language you use.

Share this post


Link to post
Share on other sites

Doh!

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

namespace CsharpConsolePixelsearch1
{
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int X;
        public int Y;

        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }

        public static implicit operator System.Drawing.Point(POINT p)
        {
            return new System.Drawing.Point(p.X, p.Y);
        }

        public static implicit operator POINT(System.Drawing.Point p)
        {
            return new POINT(p.X, p.Y);
        }
    }

    public class AU3
    {
        [DllImport("AutoItX3.dll")]
        public static extern void AU3_PixelSearch(int nLeft, int nTop, int nRight, int nBottom, int nCol, int nVar, int nStep, ref POINT nPointResult);
        public static void Main()
        {
            POINT mypoint = new POINT();
            AU3_PixelSearch(0, 0, 800, 600, 15333544, 0, 1, ref mypoint);
            //Console.Writeline("Found at ({0}, {1})", mypoint.X, mypoint.Y);
        }

    }
    
}

Runtime error

An unhandled exception of type 'System.AccessViolationException' occurred in CSformPixelsearch1.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Yeah sorry I forgot to initialize it. I don't often write code in such small amounts.

As Authenticity pointed out though, the function just seems to be broken.

Share this post


Link to post
Share on other sites

Is this C++ code terribly wrong?

It compiles and runs without error, except it moves the mouse somewhere unexpected.

#include <Windows.h>
#include "AutoIt3.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
long xx = 0; 
long yy = 0;

LPPOINT ptr = new POINT();

AU3_PixelSearch(0,0,1024,768,0xFFFFFF,0,1,ptr);

xx = ptr->x;
yy = ptr->y;

AU3_MouseMove(xx,yy,5);

return 0;
}

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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