Page 1 of 1

What is a byte array and how to call this dll file?

Posted: 13 Oct 2019, 11:23
by afe
Hello,

Converting a file to a byte array means reading the file into memory, then assigning the first byte in memory to the first element of the array, and so on?

If so, I want to pass it to a ocr.dll. I don't know much about this dll file, and the documentation is very bad.
This is an example of calling this dll with C#. But there is an error in line 3, that string cannot be used here. The final result should be obtained from memory. But I don't quite understand what it means.

Code: Select all

[DllImport("ocr.dll")]
public static extern int init();
[DllImport("ocr.dll")]
public static extern int ocr(byte[] bin,int binlength);
The document does not specify what the parameter "ok" is. It only writes as an integer.


Re-edit

Code: Select all

r := ocr("TEST.jpg")
msgbox % r
return

ocr(FilePath)
{
	File := FileOpen(FilePath, "r")
	size := File.Length
	File.RawRead(bin, size)
	File.Close()

	hModule := DllCall("LoadLibrary", "Str", "ocr.dll", "Ptr")
	DllCall("ocr\init")
	t := DllCall("ocr.dll\ocr", "ptr", &bin, "int", size)
	DllCall("FreeLibrary", "Ptr", hModule)

	r := StrGet(&t)
	return, r
}

dll Description
.DLL command init, integer, "ocr.dll", "init", , 'Initialize once in the subroutine startup

.DLL command ocr, text type, "ocr.dll", "ocr", , ‘recognition submission
     .parameter z_bin, byte set, , submit picture byte set
     . parameter ok, integer type, ,


dll file
http://s000.tinyupload.com/?file_id=11129310907512837481

Re: What is a byte array and how to call this dll file?

Posted: 13 Oct 2019, 17:00
by lexikos
What you have created is an associative array containing a sequence of values of variant type, which all happen to be 64-bit integers in the range 0 to 255. This is not an array of bytes.

In C, an array of bytes is just a pointer to a location in memory. If an array has n items, this information isn't stored in the array; there are just n items at that memory location. (This is probably what the second parameter is for.) For an array of bytes with n items at address p, the first byte is at address p and the last byte is at address p+n-1.

Inside your function, the variable bytes contains what you need already, but you cannot return it as strings cannot reliably contain binary zero in v1. One alternative is to use ByRef instead. Another alternative is to use an object. You can read into an object like this:

Code: Select all

; Untested
...
size := File.Length
bytes := {}
bytes.SetCapacity("buf", size)
bytes.ptr := bytes.GetAddress("buf")
bytes.size := size
file.RawRead(bytes.ptr, size)
return bytes
Then you would pass bytes.ptr to ocr.dll.
v2
There is no reason to accept z_bin ByRef if you are passing it as "ptr", z_bin, as it must be an integer. If you pass a variable containing binary data, you must use "ptr", &z_bin.

Re: What is a byte array and how to call this dll file?

Posted: 15 Oct 2019, 13:00
by afe
Thank you for your guidance, I have to think about it.

Re: What is a byte array and how to call this dll file?

Posted: 21 Oct 2019, 13:56
by afe
I updated the code. The parameter ok here should be the size of the byte array. But I don't know how to get its return value. It is a strange type called text type. I have to read from the memory address, but I don't know how long it is.

Re: What is a byte array and how to call this dll file?

Posted: 21 Oct 2019, 16:08
by swagfag
what dll? what's it called? where is the documentation? code samples?
why arent u providing any of these? u do seem to have at least some of them, yet u expect of us to piece it all together based off of code signatures and generic names

read again what lexikos wrote last. u have to make a choice: either u pass ur function the pointer(read: number) to ur buffer and leave the body as is, or u pass the buffer by reference and change the body to accept a reference

the function's return value(if we can trust u havent changed it too for whatever reason) is an int, probably a status code.
"text type" sounds a lot like a string type, a pointer type. either u supply some function a pointer and it fills it up for u, or perhaps the ocr engine allocates it itself and gives u back a handle. but again, why guess? show the docs/code

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 04:54
by afe
This dll file was previously available for download at the end of the post.
To be exact, there is no documentation. However, I still attach a description of the dll.
As for the example, I have also provided an example of C# before. If you want the ahk example, I have already given it, but it is not complete and does not work. This is exactly what I want to ask.

It should be noted that this dll will return a text type data, end with '\0'. The final result must be obtained from its memory address.

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 08:10
by swagfag
no, i meant the C# code besides the imports, so we can see how its used

as for documentation, whats this?
http://www.dywt.com.cn/index.htm
http://www.dywt.com.cn/first/index.htm
unfortunately, i cant read any of it

if the signature is indeed what it is(no, im not gonna dissassemble the dll to find out if it really is, ill just assume so), then there are 2 possibilities:
  • either the function mutates the buffer u passed in and stores its result there
  • or the result is returned
now, i dont know how common it is in C# to return pointer types as plain ints(probably not particularly),
but if the return value indeed contains an address at which a null-terminated string awaits u,
then use StrGet(theAddress+0, [Encoding]) to retrieve it
mind the string's encoding

and again:
"ptr", bin, if bin is a variable that contains an address(number, 12341131515)
"ptr", &bin, if bin is a variable that contains binary data

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 08:49
by afe
This is not the documentation page for this dll.

Thanks for your reminder, I have updated the code. But if you use StrGet, how to determine the length of the return value of the dll?Currently only know that the return value has been null ending.

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 09:38
by swagfag
if its a null-terminated string and u know how its encoded, u dont need the length. strget will retrieve everything up to the first null byte and return it to u, interpreted as a string

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 09:56
by Helgef

Code: Select all

DllCall("ocr.dll\init")
I assume this is supposed to do some kind of initialisation for the dll and is required to be called before any other functions in the library are used. If that is the case, see this :arrow: weird script crash if LoadLibrary isnt used.

More guessing, I find it unlikely that the function wants the pointer to the data of a entire jpg file, you are probably required to load the image and then pass a pointer to the actual pixel data.

If you don't have any documentation, why even bother?

Also, you have edited your original post to the point where some of the answers doesn't make sense :thumbdown: .

Cheers.

Re: What is a byte array and how to call this dll file?

Posted: 22 Oct 2019, 11:50
by afe
For re-editing posts, I just removed the parts that might be misleading. I don't think there is anything wrong with this.

The script still crashes.