Page 1 of 1
Re: Read C variable in PB [Resolved]
Posted: Mon May 31, 2021 5:45 pm
by juergenkulow
Code: Select all
; printf under PB_C
CompilerIf #PB_Compiler_ExecutableFormat<>#PB_Compiler_Console
CompilerError "Exec-Format must be Console in Compiler-Option"
CompilerEndIf
Define *string=UTF8("Hello visible universe. ")
Define *string2=UTF8("Value is now: %lld ")
Define *string3=UTF8("CPU-Time: %lld ")
Define value.q=42
OpenConsole()
! printf(p_string);
! printf(p_string2,++v_value);
! long long myrdtsc(void) { asm volatile ("rdtsc\n\t" "shl $32,%rdx\n\t" "or %rdx ,%rax"); }
! printf(p_string3,myrdtsc());
Input()
CloseConsole()
FreeMemory(*string)
FreeMemory(*string2)
FreeMemory(*string3)
; Hello visible universe. Value is now: 43 CPU-Time: 3181639470065
Delete: CompilerIf #PB_Processor_C<>#PB_Compiler_Processor ...
Re: Read C variable in PB [Resolved]
Posted: Mon May 31, 2021 5:56 pm
by Kwai chang caine
Juergenkulow apparently you are a C master
All your codes is always splendids...but always also ununderstandable for my little head

Re: Read C variable in PB [Resolved]
Posted: Tue Jun 01, 2021 8:21 pm
by Olli
It stays a_MyArray and p.v_myVariableInTheProcedure to be tested, and you will have done the complete tour of the links between PB and C !
Re: Read C variable in PB [Resolved]
Posted: Tue Jun 01, 2021 8:29 pm
by Marc56us
Now how to do the opposite: access from C to a variable defined by PB?
Code: Select all
A$ = "Hello World"
! printf("%s", A$ ??? );
Input()
PokeS ? strcpy(v_A[], A$ ?) ?
(My goal is to try to use the
sscanf,
sprintf etc functions afterwards)

Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 2:32 am
by juergenkulow
Code: Select all
; PB_C sscanf
EnableExplicit
Define *a_utf8string=UTF8("4711471274713")
Define value.q
! extern int sscanf(const char *s, const char *template,...);
; First find declaration of sscanf.
! sscanf(p_a_utf8string, "%lld" , &v_value);
; C Scan String function with the parameters
; PureBasic Pointer to a UTF8-String *a_utf8string, p_a_utf8string
; Scanformat for quad, 8 Byte number in c long long %lld
; Adress of PureBasic value, &v_value
SetClipboardText("Value is: "+Str(value))
MessageRequester("Value",Str(value))
FreeMemory(*A_UTF8String)
; Value is: 4711471274713
Please ask your questions.
Delete: CompilerIf #PB_Processor_C<>#PB_Compiler_Processor
Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 6:06 am
by Marc56us
Thank you juergenkulow for this quick answer with the explanations in the comments.
This new version of PB will definitely bring us many new possibilities.

Re: Read C variable in PB [Resolved]
Posted: Wed Jun 02, 2021 4:21 pm
by Kwai chang caine
Re: Read C variable in PB [Resolved]
Posted: Fri Jun 04, 2021 2:35 pm
by Olli
Juergenkulow wrote:! long long myrdtsc(void) { asm volatile ("rdtsc\n\t" "shl $32,%rdx\n\t" "or %rdx ,%rax"); }
I do not find the words to describe how it is beautiful...