I am trying to get Java Access Bridge (2.02) to work with C # (.NET 3.5). I have work for some functions, but when I call a function that references struct (getAccessibleContextInfo), I get this error message: System.AccessViolationException: attempt to read or write protected memory. This often indicates that another memory is corrupted.
Here is my code:
[DllImport("Windowsaccessbridge.dll", CallingConvention = CallingConvention.Cdecl)] internal extern static void Windows_run(); [DllImport("Windowsaccessbridge.dll", CallingConvention = CallingConvention.Cdecl)] private extern static void releaseJavaObject(long vmID, IntPtr javaObject); [DllImport("Windowsaccessbridge.dll", CallingConvention = CallingConvention.Cdecl)] private extern static bool isJavaWindow(IntPtr window); [DllImport("Windowsaccessbridge.dll", CallingConvention = CallingConvention.Cdecl)] private extern static bool getAccessibleContextInfo(long vmID, IntPtr ac, out AccessibleContextInfo textInfo); [DllImport("Windowsaccessbridge.dll", CallingConvention = CallingConvention.Cdecl)] private static extern bool getAccessibleContextFromHWND(IntPtr hwnd, out long vmID, out IntPtr ac); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct AccessibleContextInfo { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string name; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)] public string description; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string role; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string role_en_US; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string states; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string states_en_US; [MarshalAs(UnmanagedType.I4)] public int indexInParent; [MarshalAs(UnmanagedType.I4)] public int childrenCount; [MarshalAs(UnmanagedType.I4)] public int x; [MarshalAs(UnmanagedType.I4)] public int y; [MarshalAs(UnmanagedType.I4)] public int width; [MarshalAs(UnmanagedType.I4)] public int height; [MarshalAs(UnmanagedType.Bool)] public bool accessibleComponent; [MarshalAs(UnmanagedType.Bool)] public bool accessibleAction; [MarshalAs(UnmanagedType.Bool)] public bool accessibleSelection; [MarshalAs(UnmanagedType.Bool)] public bool accessibleText; [MarshalAs(UnmanagedType.Bool)] public bool accessibleInterfaces; }; private void Form1_Load(object sender, EventArgs e) { Windows_run(); } private void button1_Click(object sender, EventArgs e) { long vmID; IntPtr ac; if (getAccessibleContextFromHWND(mainWindowHwnd, out vmID, out ac)) { MessageBox.Show("Got Context: " + vmID.ToString() + ", " + ac.ToString()); AccessibleContextInfo info; if (getAccessibleContextInfo(vmID, ac, out info))
I donโt think this is a problem with java-dll, as it works fine with the C ++ example program that comes with the API.
I guess from a google search that this is a problem with the way I sort the AccessibleContextInfo structure, but I don't know how to do it correctly.
This is how the structure is declared in the sample program "AccessBridgePackages.h"
#define MAX_STRING_SIZE 1024 #define SHORT_STRING_SIZE 256 typedef struct AccessibleContextInfoTag { wchar_t name[MAX_STRING_SIZE];
Any help is much appreciated!