Declare Problem

Hello,

I keep getting a bad crash when I try to access this dll. It returns a StringBuilder object in Parameter 3 (Results) of ReadRegion but I dont know how to handle it:

C++ Code:

public static extern int ReadRegion(IntPtr hwnd, string name, ref StringBuilder resulst, int offsetX, int offsetY);

Xojo Declare:

Declare Function ReadRegion lib "OpenScrape.dll" (hWND as integer, Region as Cstring, Results as Cstring, offsetX as int32, offsetY as int32) as integer Dim Result2 as CString value = ReadRegion(WindowHandle,"p0cardface0rank",Result2, 0,0)

How do I handle the return value datatype of: ref StringBuilder resuls ??

I have tried CString, ptr etc and all fail. Any help would be appreciated.

Thanks

?

  • Try byref Results.
  • Also, resultst appears to be a structure of type StringBuilder. You should define the structure in a module and dimension Results as the structure. I suppose that you have the technical documentation describing the structure details.

If the dll you are using is based onthis or this code then you will probably need to do one of the following:

  1. Allocate space in a CString before you make the call then pass in a reference to the CString so its contents can be updated by the dll. i.e.

Dim Result2 As CString = " (256 spaces in here) " Declare Function ReadRegion lib "OpenScrape.dll" (hWND as integer, Region as Cstring, ByRef Results as CString, offsetX as int32, offsetY as int32) as Int32 value = ReadRegion(WindowHandle,"p0cardface0rank",Result2, 0,0)

Or you could use some code to pad Result2 before the call, its up to you.

  1. Allocate space using a memoryblock and pass that into the dll i.e.

Dim Result2 As New MemoryBlock(256) Dim p as Ptr = Result2 Declare Function ReadRegion lib "OpenScrape.dll" (hWND as integer, Region as Cstring, Results as Ptr, offsetX as int32, offsetY as int32) as Int32 value = ReadRegion(WindowHandle,"p0cardface0rank",p, 0,0) system.debuglog(Result2.CString(0))

I used 256 everywhere because:

strcpy_s(result,256,text);

or 2) might be:

Dim Result2 As New MemoryBlock(256) Declare Function ReadRegion lib "OpenScrape.dll" (hWND as integer, Region as Cstring, Results as Ptr, offsetX as int32, offsetY as int32) as Int32 value = ReadRegion(WindowHandle,"p0cardface0rank", Result2, 0,0) system.debuglog(Result2.CString(0))

I can never remember off the top of my head :slight_smile:

Hey thanks for looking at this. I tried every combination of what you suggested and it still crashes. Very frustrating so far. I think Louis is right and I have to do some kind of structure. Of course I have no clue on how to do this…

Do you have a link to the site/docs you got the dll from?

Looking at the code on the GITHub link provided by @ , I am not so sure that Result should be a structure. A string of some flavour or a memoryblock as suggested by Julian would be the more likely possibilities.

You will find a topic about structures on developer.xojo.com. I like to use structures, even where I could probably use classes instead. I often create structures and then single dimension arrays declared with my structure type, to create internal tables easy to manipulate, sort, etc. An array of classes could probably achieve the same result. It is recommended to use classes, but I have not seen hard facts yet to justify that structures are less efficient or more problematic.

That code on GitHub is probably correct. Somebody compiled the dll for me so it could have been modified a bit but doubtful. I wonder if the window handle is the problem? I noticed that the handles are 6 digits and perhaps this one takes a pointer to the handle? I tried:

Dim WindowHandle as ptr
WindowHandle = ptr(app.WindowHandle)

in which app.WindowHandle = 460238 but that returns all zeros

Window handles are window handles, they could be any number, quite hard to get one of those wrong :slight_smile:

Do you have working non-xojo code that uses the dll that I can look at?

I’ve just debugged the dll and its failing inside the first line of ReadRegion, like its not initialised or something. Do you need to load something before calling ReadRegion?

YEUX_V2_API int OpenTablemap(char* filename)

Oh, this is the exposed function by the way:

YEUX_V2_API int ReadRegion(HWND hwnd, char* name, char* &result, int offset)

Where did you get the C++ code in the original post from, the somebody else, did they wrap it up in another layer for you?

Hey Louis,

Yes you have to load the “table map” file first which works ok:

[code]//load TM
value = OpenTablemap(“IGN9M.tm”)

//Success
if value = 1 then
value = ReadRegion(WindowHandle,“p0cardface0rank”,p, 0,0)
end[/code]

the “p0cardface0rank” is a string located in the table map file which is basically an index that gets the screen scraping coordinates of a particular item:

r$p0cardface0rank 481 63 491 73 ffffffff -50 T3

I was told the “r$” portion of that was not needed when passing the region name to find.

Yeah a friend of mine sent me the dll and how to access it. It might be easier to redo it all and try to compile it myself but I do not know anything about C or even what IDE to use. I hate everything but Xojo :slight_smile:

Ahh, can you send me the table map so I can try and get past the part that is erroring when I debug the dll here?

Or just try this:

Declare Function ReadRegion Lib "OpenScrape.dll" (hWND As Integer, Region As CString, ByRef Results As Ptr, offsetX As Int32, offsetY As Int32) As Int32 Dim Result2 As New MemoryBlock(256) Dim p As Ptr = Result2 value = ReadRegion(window1.Handle, name, p, 0, 0)

or this

Declare Function ReadRegion Lib "OpenScrape.dll" (hWND As Integer, Region As CString, ByRef Results As Ptr, offset As Int32) As Int32 Dim Result2 As New MemoryBlock(256) Dim p As Ptr = Result2 value = ReadRegion(window1.Handle, name, p, 0)

The bottom one works here for me, the string gets through to the dll ok, but if your exported function is as you originally posted the the first one should be ok for you.

Hey,

It still crashes for me so your dll is probably better then the one I have.

You can get the sample table map and the 64bit dll I am using here:

https://drive.google.com/open?id=1j_Axalo4SskraStVzpnlzdlVFHY__VXQ

thanks for all your help on this! I think using a 32bit dll would be better but I do not have one. Can I try yours?

I got a value of 8 back, do you know if that is correct?

Hey. It will not be correct as its for a specific screen shot but any return is GOOD! Is this with your dll or ?

Yeah, my dll, one sec, I’ll package it all up and give you a link.

Here you go, I’ll remove the link within the hour.

[link removed, let me know if you didn’t manage to download it]

I’ve included the 32 and 64 bit dll’s and a sample project from xojo, you’ll need to change the paths.

The source was just taken from https://github.com/TheHighFish/open-scrape-hopper-dll/ and compiled in VS 2017.

Hey Julian. Wow…thanks for this! I will give it a try tonight and let you know how it works. I was getting so frustrated with this so you saved my life…

Interesting, its not working 2018r1 and above for me, in 2017r3 and below its working fine, could you let me know if you find the same problem?