PInvoke from Fortran to C# - pinvoke

I want to import a function from a Fortran compiled library the signature of the function form for that I have access is in c:
typedef void (__stdcall *fp_SUBLTdllTYPE)(double &,double *,double &,long &,char*,long );
My C# code is as follows:
//(long &,char*,char*,char*,long &,char*,long ,long ,long ,long );
[DllImport(#"C:\Program Files\REFPROP\refprop.dll",
CallingConvention=CallingConvention.StdCall,
CharSet = CharSet.Auto,
EntryPoint = "SETUPdll")
]
public static extern void Setup([In] long nc,[In]
[MarshalAs(UnmanagedType.LPStr)] StringBuilder hfiles,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hfmix,
[In] [MarshalAs(UnmanagedType.LPStr)] StringBuilder hrf,
[In,Out] long ierr, [Out] [MarshalAs(UnmanagedType.LPStr)] StringBuilder herr,long l1, long l2, long l3,long l4);
and the fortran definition is:
subroutine SETUP (nc,hfiles,hfmix,hrf,ierr,herr)
implicit double precision (a-h,o-z)
implicit integer (i-k,m,n)
implicit logical (l)
c
cDEC$ ATTRIBUTES DLLEXPORT :: SETUP
c dll_export SETUP
c
parameter (ncmax=20) !max number of components in mixture
parameter (nrefmx=10) !max number of fluids for transport ECS
parameter (n0=-ncmax-nrefmx,nx=ncmax)
parameter (nrf0=n0) !lower limit for transport ref fluid arrays
parameter (nrefluids=4) ! numb
the problem is that I do not have access to a Fortran compiller and my knolege of fortran is almost zero.
When I call the function from C# code:
long ierr=0;
long i = 2;
StringBuilder herr=new StringBuilder("");
Setup(i, new StringBuilder("R410a.mix"), new StringBuilder("hmx.bnc"), new StringBuilder("DEF"), ierr, herr, refpropcharlength * ncmax, refpropcharlength,
lengthofreference, errormessagelength);
I get the following error:
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
Can anyone help me?

It seems incorrect to pass StringBuilder objects when you want a string. As a starting point I would try things like passing herr.ToString() or just build Strings to begin with instead of StringBuilder(s).

Related

What's the minimum code required to make a NativeCall to the md_parse function in the md4c library?

Note: This post is similar, but not quite the same as a more open-ended questions asked on Reddit: https://www.reddit.com/r/rakulang/comments/vvpikh/looking_for_guidance_on_getting_nativecall/
I'm trying to use the md4c c library to process a markdown file with its md_parse function. I'm having no success, and the program just quietly dies. I don't think I'm calling it with the right arguments.
Documentation for the function is here: https://github.com/mity/md4c/wiki/Embedding-Parser%3A-Calling-MD4C
I'd like to at least figure out the minimum amount of code needed to do this without error. This is my latest attempt, though I've tried many:
use v6.d;
use NativeCall;
sub md_parse(str, int32, Pointer is rw ) is native('md4c') returns int32 { * }
md_parse('hello', 5, Pointer.new());
say 'hi'; # this never gets printed
md4c is a SAX-like streaming parser that calls your functions when it encounters markdown elements. If you call it with an uninitialised Pointer, or with an uninitialised CStruct then the code will SEGV when the md4c library tries to call a null function pointer.
The README says:
The main provided function is md_parse(). It takes a text in the
Markdown syntax and a pointer to a structure which provides pointers
to several callback functions.
As md_parse() processes the input, it calls the callbacks (when
entering or leaving any Markdown block or span; and when outputting
any textual content of the document), allowing application to convert
it into another format or render it onto the screen.
The function signature of md_parse is:
int md_parse(const MD_CHAR* text, MD_SIZE size, const MD_PARSER* parser, void* userdata);
In order for md_parse() to work, you will need to:
define a native CStruct that matches the MD_PARSER type definition
create an instance of this CStruct
initialise all the function pointers with Raku functions that have the right function signature
call md_parse() with the initialised CStruct instance as the third parameter
The 4th parameter to md_parse() is void* userdata which is a pointer that you provide which gets passed back to you as the last parameter of each of the callback functions. My guess is that it's optional and if you pass a null value then you'll get called back with a null userdata parameter in each callback.
Followup
This turned into an interesting rabbit hole to fall down.
The code that makes it possible to pass a Raku sub as a callback parameter to a native function is quite complex and relies on MoarVM ops to build and cache the FFI callback trampoline. This is a piece of code that marshals the C calling convention parameters into a call that MoarVM can dispatch to a Raku sub.
It will be a sizeable task to implement equivalent functionality to provide some kind of nativecast that will generate the required callback trampoline and return a Pointer that can be assigned into a CStruct.
But we can cheat
We can use a simple C function to return the pointer to a generated callback trampoline as if it was for a normal callback sub. We can then store this pointer in our CStruct and our problem is solved. The generated trampoline is specific to the function signature of the Raku sub we want to call, so we need to generate a different NativeCall binding for each function signature we need.
The C function:
void* get_pointer(void* p)
{
return p;
}
Binding a NativeCall sub for the function signature we need:
sub get_enter_leave_fn(&func (uint32, Pointer, Pointer))
is native('./getpointer') is symbol('get_pointer') returns Pointer { * }
Initialising a CStruct attribute:
$!enter_block := get_enter_leave_fn(&enter_block);
Putting it all together:
use NativeCall;
enum BlockType < DOC QUOTE UL OL LI HR H CODE HTML P TABLE THEAD TBODY TR TH TD >;
enum SpanType < EM STRONG A IMG SPAN_CODE DEL SPAN_LATEXMATH LATEXMATH_DISPLAY WIKILINK SPAN_U >;
enum TextType < NORMAL NULLCHAR BR SOFTBR ENTITY TEXT_CODE TEXT_HTML TEXT_LATEXMATH >;
sub enter_block(uint32 $type, Pointer $detail, Pointer $userdata --> int32) {
say "enter block { BlockType($type) }";
}
sub leave_block(uint32 $type, Pointer $detail, Pointer $userdata --> int32) {
say "leave block { BlockType($type) }";
}
sub enter_span(uint32 $type, Pointer $detail, Pointer $userdata --> int32) {
say "enter span { SpanType($type) }";
}
sub leave_span(uint32 $type, Pointer $detail, Pointer $userdata --> int32) {
say "leave span { SpanType($type) }";
}
sub text(uint32 $type, str $text, uint32 $size, Pointer $userdata --> int32) {
say "text '{$text.substr(0..^$size)}'";
}
sub debug_log(str $msg, Pointer $userdata --> int32) {
note $msg;
}
#
# Cast functions that are specific to the required function signature.
#
# Makes use of a utility C function that returns its `void*` parameter, compiled
# into a shared library called libgetpointer.dylib (on MacOS)
#
# gcc -shared -o libgetpointer.dylib get_pointer.c
#
# void* get_pointer(void* p)
# {
# return p;
# }
#
# Each cast function uses NativeCall to build an FFI callback trampoline that gets
# cached in an MVMThreadContext. The generated callback code is specific to the
# function signature of the Raku function that will be called.
#
sub get_enter_leave_fn(&func (uint32, Pointer, Pointer))
is native('./getpointer') is symbol('get_pointer') returns Pointer { * }
sub get_text_fn(&func (uint32, str, uint32, Pointer))
is native('./getpointer') is symbol('get_pointer') returns Pointer { * }
sub get_debug_fn(&func (str, Pointer))
is native('./getpointer') is symbol('get_pointer') returns Pointer { * }
class MD_PARSER is repr('CStruct') {
has uint32 $!abi_version; # unsigned int abi_version
has uint32 $!flags; # unsigned int flags
has Pointer $!enter_block; # F:int ( )* enter_block
has Pointer $!leave_block; # F:int ( )* leave_block
has Pointer $!enter_span; # F:int ( )* enter_span
has Pointer $!leave_span; # F:int ( )* leave_span
has Pointer $!text; # F:int ( )* text
has Pointer $!debug_log; # F:void ( )* debug_log
has Pointer $!syntax; # F:void ( )* syntax
submethod TWEAK() {
$!abi_version = 0;
$!flags = 0;
$!enter_block := get_enter_leave_fn(&enter_block);
$!leave_block := get_enter_leave_fn(&leave_block);
$!enter_span := get_enter_leave_fn(&enter_span);
$!leave_span := get_enter_leave_fn(&leave_span);
$!text := get_text_fn(&text);
$!debug_log := get_debug_fn(&debug_log);
}
}
sub md_parse(str, uint32, MD_PARSER, Pointer is rw) is native('md4c') returns int { * }
my $parser = MD_PARSER.new;
my $md = '
# Heading
## Sub Heading
hello *world*
';
md_parse($md, $md.chars, $parser, Pointer.new);
The output:
./md4c.raku
enter block DOC
enter block H
text 'Heading'
leave block H
enter block H
text 'Sub Heading'
leave block H
enter block P
text 'hello '
enter span EM
text 'world'
leave span EM
leave block P
leave block DOC
In summary, it's possible. I'm not sure if I'm proud of this or horrified by it. I think a long-term solution will require refactoring the callback trampoline generator into a separate nqp op that can be exposed to Raku as a nativewrap style operation.

Is pointer offset to a cython pointer?

Is there a way that I can move a pointer to be n bytes later in Cython?
For instance, if I have (pseudo code):
cdef void *n_ary
cdef void *eightbytes
cdef void *n_ary_plus8
ary = cnp.PyArray_DATA(input_array)
eightbytes = 8
n_ary_plus8 = ary + eightbytes
I get the error Invalid operand types for '+' (void *; void *) for the last line telling me that it does not know how to add the pointer address ary and the pointer eightbytes. It seems like this ought to be obvious but I can't find anything in the manual or this august reference.
void* arithmetic is not allowed by the C and C++ standards.
If you want to do it, you need to convert the pointer to a char* type.
For more information, please look at this and this. Be very careful of the strict aliasing rule with such pointer casts (padding and alignment too).
I was able to do what I needed to do with
cdef void *n_ary
cdef int eightbytes
cdef void *n_ary_plus8
ary = cnp.PyArray_DATA(input_array)
eightbytes = 8
n_ary_plus8 = &ary[eightbytes]

Xamarin binding C library params not working (variadic functions)

I need to use a C library and I got it to work on the emulator easily, but on an arm64 device only with some strange trickery. The issue is that C functions with … (variadic functions) do not pass values correctly from C# to the library.
This is the C function, with ...
cmd_ln_t *
cmd_ln_init(cmd_ln_t *inout_cmdln, const arg_t *defn, int32 strict, ...)
{
va_list args;
const char *arg, *val;
char **f_argv;
int32 f_argc;
va_start(args, strict);
f_argc = 0;
while ((arg = va_arg(args, const char *))) {
++f_argc;
E_INFO("name: %s ", arg);
E_INFO(" retrieving value...");
val = va_arg(args, const char*);
E_INFO("value retrieved. \n");
E_INFO("value: %s \n", val);
if (val == NULL) {
E_ERROR("Number of arguments must be even!\n");
return NULL;
}
++f_argc;
}
va_end(args);
.....................................
I check if the values are correct with the E_INFO()
Approach 1 - The default PARAMS doesn't work:
When I use the following default params expression approuch for c bindings, the ‘arg’ printed in the function shows unknown characters and when ‘val’ is used the function crashes.
[DllImport("__Internal")] public static extern unsafe cmd_ln_t*
cmd_ln_init(cmd_ln_t* inout_cmdln, arg_t* defn, int strict, params string[] arguments);
Approach 2 - a more elaborate approach works:
When I use the a more elaborate approach everything works, on x86_64 architecture normally but for arm64 with a strange work-around.
the binding expression in a more elaborate approach.
[DllImport("__Internal")]
public static extern unsafe cmd_ln_t* cmd_ln_init(cmd_ln_t* inout_cmdln, arg_t* defn, int strict, string arg1, string arg2);
[DllImport("__Internal")]
public static extern unsafe cmd_ln_t* cmd_ln_init(cmd_ln_t* inout_cmdln, arg_t* defn, int strict, string arg1, string arg2, string arg3);
[DllImport("__Internal")]
public static extern unsafe cmd_ln_t* cmd_ln_init(cmd_ln_t* inout_cmdln, arg_t* defn, int strict, string arg1, string arg2, string arg3, string arg4);
//etc etc… for x numbers of arguments
The binding works works the following code
// works for x86_64
var cmdPointer = MyBindingLib.cmd_ln_init(null, psArgsPointer, 1,
"-hmm", hmmFolder,
"-dict", dictFile,
"-mmap", "no",
"-kws_threshold", "1e-80",
"-lw", "2.0",
null);
// works for arm64
var cmdPointer = MyBindingLib.cmd_ln_init(null, psArgsPointer, 1,
null, null,
null, null, null,
"-hmm", hmmFolder,
"-dict", dictFile,
"-mmap", "no",
"-kws_threshold", "1e-80",
"-lw", "2.0",
null);
As you see, the x86_64 works normally to get the values to the C library.
But the arm64 version needs to have 5 null values, others half of the values won't make it to the C library (I can check that with the E_INFO function in the C function).
Anyone any idea how to get this Xamarin C binding correct with params or without the 5 prefix null values?
Source is on github
uses c library at sphinxbase
It seems to be expected behavior for arm64 architecture, because of the way arm64 functions are invoked.
Invoking functions in a arm64 library that use ...) at the end, you have to take into account that the first 8 argument spots are for 'normal' arguments, then optionally the variable/params can start.
So, in my example I used 5 NULL values to fill the first 8 argument spots, then start the values for the ...)
See full answer:
https://github.com/xamarin/xamarin-macios/issues/10285

How to I declare "LPTSTR" for Raku NativeCall?

Raku/Perl6
Windows
I am trying to code a Raku Native call to a C functions that uses a "LPTSTR". How do I declare that? Something like constant DWORD := int32; but for LPTSTR?
If it helps, I found this description: "LPTSTR is a [long] pointer to a (non-const) TCHAR string" and "LPTSTR: null-terminated string of TCHAR (Long Pointer)"
"LPTSTR" comes from "LPWSTR lpBuffer,"
LP = (long) pointer, TSTR = either a wide string if UNICODE is defined, or an 8-bit string if not. So it's either a wchar_t * or unsigned char *. Determining which of these is in effect for the library you're using is probably quite difficult from Raku-space.

Artnet Packet Structure

Even though I have found so many Libraries that allows programmers to use the Artnet/ArtnetDMX protocol I haven't found the correct structure to send out Artnet in code (To a socket).
The structure is given but, I just can't figure out in what order bytes should be send.
Do i send a byte to a universe One by One, or do I need to send a Byte array with all the values?
Doesn anyone have experience in sending Artnet over UDP?
This the structure given on wikipedia: http://i.stack.imgur.com/wUjzd.png
I use the following struct for Art-Net v2
typedef struct {
char ID[8]; //"Art-Net"
UInt16 OpCode; // See Doc. Table 1 - OpCodes eg. 0x5000 OpOutput / OpDmx
UInt16 version; // 0x0e00 (aka 14)
UInt8 seq; // monotonic counter
UInt8 physical; // 0x00
UInt8 subUni; // low universe (0-255)
UInt8 net; // high universe (not used)
UInt16 length; // data length (2 - 512)
uint8_t data[512]; // universe data
} ArtnetDmx;
If you want to write an Art-Net application you should really read the documentation located on Artistic Licence.
I would follow the version 2 (14) specification as that version 3 is quite rare in the wild.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Artnet
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
public string id;
public short opCode;
public byte protocolHi;
public byte protocolLo;
public byte sequence;
public byte physical;
public short universe;
public short length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public byte[] data;
}