Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Would you people stop playing these stupid games?!?!?!!!!


devel / comp.lang.tcl / Re: cffi and ffidl

SubjectAuthor
* cffi and ffidlJacob
`* cffi and ffidlJacob
 `* cffi and ffidlJacob
  `* cffi and ffidlAshok
   `- cffi and ffidlJacob

1
cffi and ffidl

<tso52m$3lfg3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10758&group=comp.lang.tcl#10758

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: JacobLam...@clevelandgolf.com (Jacob)
Newsgroups: comp.lang.tcl
Subject: cffi and ffidl
Date: Fri, 17 Feb 2023 07:04:37 -0800
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <tso52m$3lfg3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 17 Feb 2023 15:04:54 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="ec82e7bf486182c733c1b2270c240f9c";
logging-data="3849731"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9s9MoAoZAfoaAD+PubOhDVJGT0E6iJo4="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Cancel-Lock: sha1:y7aTKMYRMCYPJAkFKxw6xna7NGo=
Content-Language: en-US
 by: Jacob - Fri, 17 Feb 2023 15:04 UTC

Hello,

I have perhaps a trivial question, but it's been giving me trouble as I
don't work with C code or dll files often. I'd like to call a function
from a dll file and have successfully done this using the cffi package.
Here is an example of what that looks like:

package require cffi
cffi::Wrapper create myFunc {C:\myFunc.dll}
::myFunc function myFunc long {a {long[30] inout} b {double[30] inout} c
{long inout} d {long[30] inout} e {double[30] inout} f {double[30]
inout} g {long inout} h {long inout} i {string}}
set a {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set b {7 0.00018 -45.2107 0.39324 32.5714 -4.82 0.536 207.8 99.6 \
-20.7 12 10 100 3 0 0 -2 -3 1 17 0.00018181 -10.1909 -3.3898 29.2007 \
0.26492 0.29827 0.4567 0.00428 0.0077969 -0.039173}
set c 30
set d {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set e {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set f {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set g 1
set h 0
set i U
myFunc a b c d e f g h i
set resp [lindex $e 0]
puts "Response: $resp"

Unfortunately, it turns out I need to do this on Tcl 8.5.9, which
doesn't appear to work with cffi. So I believe I need to do this with
Fifdl instead. My understanding is that all the arrays are treated as
pointers, but I haven't been able to get it to work. Any help would be
appreciated.

Thanks,

Jacob

Re: cffi and ffidl

<tt2pjh$163pk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10777&group=comp.lang.tcl#10777

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: JacobLam...@clevelandgolf.com (Jacob)
Newsgroups: comp.lang.tcl
Subject: Re: cffi and ffidl
Date: Tue, 21 Feb 2023 07:56:34 -0800
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <tt2pjh$163pk$1@dont-email.me>
References: <tso52m$3lfg3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 21 Feb 2023 15:56:33 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="58e8224577d591a198ef9ca0ad9f264a";
logging-data="1249076"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wvKZVsTgmEPbBhH6W1M4sz6PF9nlYdRk="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Cancel-Lock: sha1:MIrlMHwxnCejj6AOGMWjLIn6TzQ=
In-Reply-To: <tso52m$3lfg3$1@dont-email.me>
Content-Language: en-US
 by: Jacob - Tue, 21 Feb 2023 15:56 UTC

On 2/17/2023 7:04 AM, Jacob wrote:
> Hello,
>
> I have perhaps a trivial question, but it's been giving me trouble as I
> don't work with C code or dll files often. I'd like to call a function
> from a dll file and have successfully done this using the cffi package.
> Here is an example of what that looks like:
>
> package require cffi
> cffi::Wrapper create myFunc {C:\myFunc.dll}
> ::myFunc function myFunc long {a {long[30] inout} b {double[30] inout} c
> {long inout} d {long[30] inout} e {double[30] inout} f {double[30]
> inout} g {long inout} h {long inout} i {string}}
> set a {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
> set b {7 0.00018 -45.2107 0.39324 32.5714 -4.82 0.536 207.8 99.6 \
> -20.7 12 10 100 3 0 0 -2 -3 1 17 0.00018181 -10.1909 -3.3898 29.2007 \
> 0.26492 0.29827 0.4567 0.00428 0.0077969 -0.039173}
> set c 30
> set d {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
> set e {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
> set f {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
> set g 1
> set h 0
> set i U
> myFunc a b c d e f g h i
> set resp [lindex $e 0]
> puts "Response: $resp"
>
> Unfortunately, it turns out I need to do this on Tcl 8.5.9, which
> doesn't appear to work with cffi. So I believe I need to do this with
> Fifdl instead. My understanding is that all the arrays are treated as
> pointers, but I haven't been able to get it to work. Any help would be
> appreciated.
>
> Thanks,
>
> Jacob

Sorry to bump this message, but perhaps a simple example could get me
moving in the right direction. If I have the following c function held
in dPlus.dll, how would I interface with FFidl?

void dPlus(const double a[2], const double b[2], double c[2])
{ c[0] = a[0] + b[0];
c[1] = a[1] + b[1];
}

Thanks,

Jacob

Re: cffi and ffidl

<tt30io$16s48$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10778&group=comp.lang.tcl#10778

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: JacobLam...@clevelandgolf.com (Jacob)
Newsgroups: comp.lang.tcl
Subject: Re: cffi and ffidl
Date: Tue, 21 Feb 2023 09:55:32 -0800
Organization: A noiseless patient Spider
Lines: 81
Message-ID: <tt30io$16s48$1@dont-email.me>
References: <tso52m$3lfg3$1@dont-email.me> <tt2pjh$163pk$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 21 Feb 2023 17:55:37 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="ddb1cebd3fee9d0c8a2603919684d5be";
logging-data="1273992"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/vN53yEH1Pv/1zSYWPuo2f6xqxugKZdgM="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Cancel-Lock: sha1:Skn3xaGey5ROpQaTkycAV9f0lbw=
In-Reply-To: <tt2pjh$163pk$1@dont-email.me>
Content-Language: en-US
 by: Jacob - Tue, 21 Feb 2023 17:55 UTC

On 2/21/2023 7:56 AM, Jacob wrote:
> On 2/17/2023 7:04 AM, Jacob wrote:
>> Hello,
>>
>> I have perhaps a trivial question, but it's been giving me trouble as
>> I don't work with C code or dll files often. I'd like to call a
>> function from a dll file and have successfully done this using the
>> cffi package. Here is an example of what that looks like:
>>
>> package require cffi
>> cffi::Wrapper create myFunc {C:\myFunc.dll}
>> ::myFunc function myFunc long {a {long[30] inout} b {double[30] inout}
>> c {long inout} d {long[30] inout} e {double[30] inout} f {double[30]
>> inout} g {long inout} h {long inout} i {string}}
>> set a {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
>> set b {7 0.00018 -45.2107 0.39324 32.5714 -4.82 0.536 207.8 99.6 \
>> -20.7 12 10 100 3 0 0 -2 -3 1 17 0.00018181 -10.1909 -3.3898 29.2007 \
>> 0.26492 0.29827 0.4567 0.00428 0.0077969 -0.039173}
>> set c 30
>> set d {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
>> set e {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
>> set f {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
>> set g 1
>> set h 0
>> set i U
>> myFunc a b c d e f g h i
>> set resp [lindex $e 0]
>> puts "Response: $resp"
>>
>> Unfortunately, it turns out I need to do this on Tcl 8.5.9, which
>> doesn't appear to work with cffi. So I believe I need to do this with
>> Fifdl instead. My understanding is that all the arrays are treated as
>> pointers, but I haven't been able to get it to work. Any help would be
>> appreciated.
>>
>> Thanks,
>>
>> Jacob
>
> Sorry to bump this message, but perhaps a simple example could get me
> moving in the right direction. If I have the following c function held
> in dPlus.dll, how would I interface with FFidl?
>
> void dPlus(const double a[2], const double b[2], double c[2])
> {
>   c[0] = a[0] + b[0];
>   c[1] = a[1] + b[1];
> }
>
>
> Thanks,
>
> Jacob
>

I figured it out. I needed to convert to and from binary. I used the
following functions from Arjen:

proc Binarize {varType args} {
foreach var $args {
upvar $var x
if [regexp {[ac]} $varType] {
set x [binary format a* $x]
} else {
set x [binary format $varType[llength $x] $x]
}
}
};#End proc Binarize

proc deBinarize {varType args} {
foreach var $args {
upvar $var x
switch $varType {
i {binary scan $x i[expr {[string length $x]/4}] x}
f {binary scan $x f[expr {[string length $x]/4}] x}
d {binary scan $x d[expr {[string length $x]/8}] x}
default {binary scan $x a* x}
}
}
};#End proc deBinarize

Re: cffi and ffidl

<tt5co4$1i2g3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10781&group=comp.lang.tcl#10781

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: apnmbx-p...@yahoo.com (Ashok)
Newsgroups: comp.lang.tcl
Subject: Re: cffi and ffidl
Date: Wed, 22 Feb 2023 21:05:32 +0530
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <tt5co4$1i2g3$1@dont-email.me>
References: <tso52m$3lfg3$1@dont-email.me> <tt2pjh$163pk$1@dont-email.me>
<tt30io$16s48$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 22 Feb 2023 15:35:32 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="241cb1702529cc178f46a3b72cf708ff";
logging-data="1640963"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MtzEtQjpmqkCLKKSYqD26"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:SfugyXNenvRWnlnQYtvFKIROgtc=
In-Reply-To: <tt30io$16s48$1@dont-email.me>
Content-Language: en-US
 by: Ashok - Wed, 22 Feb 2023 15:35 UTC

On 2/21/2023 11:25 PM, Jacob wrote:
> I figured it out. I needed to convert to and from binary. I used the
> following functions from Arjen:
>

Jacob,

You haven't shown what you do with those generated binaries but be aware
that if your function is in fact treating those array parameters as
in-out (as you have shown in your CFFI definition), then it will write
to the storage allocated by the Tcl_Obj. This is generally a no-no. If
they are input parameters only then that should be ok.

For output and inout parameters, you probably have to explicitly
allocate memory in ffidl. I'm not sufficiently familiar with ffidl to
know how to do that.

/Ashok

Re: cffi and ffidl

<tt66vp$1ktr6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=10789&group=comp.lang.tcl#10789

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: JacobLam...@clevelandgolf.com (Jacob)
Newsgroups: comp.lang.tcl
Subject: Re: cffi and ffidl
Date: Wed, 22 Feb 2023 15:03:21 -0800
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <tt66vp$1ktr6$1@dont-email.me>
References: <tso52m$3lfg3$1@dont-email.me> <tt2pjh$163pk$1@dont-email.me>
<tt30io$16s48$1@dont-email.me> <tt5co4$1i2g3$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 22 Feb 2023 23:03:21 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="937ce07875af6312dd1165b99933602c";
logging-data="1734502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+uA6VgM1RIut9V0kjKKSZSn4DU4PkQbeg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Cancel-Lock: sha1:6rRjnSdTVEFerQ9qimUvNW19rgU=
In-Reply-To: <tt5co4$1i2g3$1@dont-email.me>
Content-Language: en-US
 by: Jacob - Wed, 22 Feb 2023 23:03 UTC

On 2/22/2023 7:35 AM, Ashok wrote:
> On 2/21/2023 11:25 PM, Jacob wrote:
>> I figured it out. I needed to convert to and from binary. I used the
>> following functions from Arjen:
>>
>
>
> Jacob,
>
> You haven't shown what you do with those generated binaries but be aware
> that if your function is in fact treating those array parameters as
> in-out (as you have shown in your CFFI definition), then it will write
> to the storage allocated by the Tcl_Obj. This is generally a no-no. If
> they are input parameters only then that should be ok.
>
> For output and inout parameters, you probably have to explicitly
> allocate memory in ffidl. I'm not sufficiently familiar with ffidl to
> know how to do that.
>
> /Ashok

Ashok,

Thank you for your feedback. As far as I can tell things are working
correctly, but I would not be surprised if there is something dangerous
I'm doing. Here is the code for completeness. This function in the dll
file is a little strange in that it does have many in-out parameters,
but only one is actually relevant to me (rresp). Considering the script
basically ends here and tcl closes, hopefully whatever risky behavior
going on behind the scenes can be ignored.

# current script location
set scriptFolder [file dirname [info script]]
# load ffid package
load [file join $scriptFolder Ffidl07t.dll]
# setup dll call
set DLL [file join $scriptFolder myFunc.dll]
set typeList {pointer-var pointer-var pointer-var pointer-var
pointer-var pointer-var pointer-var pointer-var pointer-utf8}
ffidl::callout myFunc $typeList long [ffidl::symbol $DLL myFunc]
# initialize inputs
set iparam {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set nparam 30
set iresp {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set rresp {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set dresp {1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1}
set nresp 1
set isens 0
set userdata U
Binarize i iparam nparam iresp nresp isens
Binarize d dresp rresp rparam
set rparam {7 0.00018 -45.2107 0.39324 32.5714 -4.82 0.536 207.8 99.6 \
-20.7 12 10 100 3 0 0 -2 -3 1 17 0.00018181 -10.1909 -3.3898 29.2007 \
0.26492 0.29827 0.4567 0.00428 0.0077969 -0.039173}
myFunc iparam rparam nparam iresp rresp dresp nresp isens userdata
deBinarize d rresp
set resp [lindex $rresp 0]
puts "Response: $resp"

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor