PBv621b4 x86 - Error with Prototype and libusb-1

Just starting out? Need help? Post your questions and find answers here.
PeDe
Enthusiast
Enthusiast
Posts: 287
Joined: Sun Nov 26, 2017 3:13 pm

PBv621b4 x86 - Error with Prototype and libusb-1

Post by PeDe »

PB v6.12/v6.20/v6.21b4 x86, LMDE 6

I use the libusb-1 with Windows 32/64-bit, and with Linux 64-bit and Raspberry PI 64-bit without any problems. When compiling, I can switch between prototypes with OpenLibrary() and ImportC. The assembler and C backend work without errors.

Under Linux 32-bit my program does not work with the prototypes and OpenLibrary() with the assembler or c backend.
There is a memory error 'Invalid memory access' at the first call of a function from the libusb-1. The ImportC version works with both backends.

Here in the example code, the error occurs when the 'Init()' function is ended.

You may need to install the developer package if there is an error importing "-lusb-1.0".

Code: Select all

sudo apt install libusb-1.0-0-dev
There is a code with the libusb-1 from infratec:
https://www.purebasic.fr/english/viewtopic.php?p=633916
With the file 'libusb.pbi' there is exactly the same error in the 'libusb_init(*ctx)' procedure.

Peter

Code: Select all

EnableExplicit

#Prototype = 1 ; 1 for Prototype, 0 = for ImportC

CompilerIf #Prototype
	Prototype.l ptLibusb_init(*Context)
	Global libusb_init.ptLibusb_init
CompilerElse
	ImportC "-lusb-1.0"
	libusb_init.l(*Context)
	EndImport
CompilerEndIf


CompilerIf #Prototype
	Procedure.i LoadLibrary()
		Protected fReturn.i, iLib.i
		iLib = OpenLibrary(#PB_Any, "libusb-1.0.so.0")
		If iLib
			libusb_init = GetFunction(iLib, "libusb_init")
			fReturn = #True
		EndIf
		ProcedureReturn fReturn
	EndProcedure
CompilerEndIf


Procedure.i Init()
  Protected iError.l
  iError = libusb_init(#Null)
  ProcedureReturn iError
EndProcedure


Define iError.i

CompilerIf #Prototype
	If LoadLibrary()
		iError = Init()
		Debug iError
	EndIf
CompilerElse
	iError = Init()
	Debug iError
CompilerEndIf
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PBv621b4 x86 - Error with Prototype and libusb-1

Post by Fred »

You will need to use PrototypeC instead, as x86 is stdcall by default (not cdecl). Other processor (arm64, x64) doesn't have this distinction. Also for callbacks you will need to use ProcedureC.
PeDe
Enthusiast
Enthusiast
Posts: 287
Joined: Sun Nov 26, 2017 3:13 pm

Re: PBv621b4 x86 - Error with Prototype and libusb-1

Post by PeDe »

Thanks for the answer. With PrototypeC it now works under Linux 32-bit.

With Windows 32/64-bit I have to use Prototype, otherwise I get the memory error with PrototypeC.
I have to use the libusb-0 with PrototypeC again.
I don't understand the rules.

But the problem is solved, it is not a PB bug.

Peter
Fred
Administrator
Administrator
Posts: 18247
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PBv621b4 x86 - Error with Prototype and libusb-1

Post by Fred »

That's wierd you get error on Windows using PrototypeC unless the libusb lib is compiled with stdcall on Windows. Anyway, these messy calling conventions should be history once the x86 will be out of business :)
Post Reply