Re: Native C Dll integration performance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Feb 11, 5:35 am, Frederic Van Belle
<FredericVanBe...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Do you mean writing a small C dll  that will exposed a single function that
deal with a fixed sized array of byte ?

You're saying that you have a long loop that calls the same set of
functions from a C DLL. My suggestion is to move the entire loop to
that same DLL, and export it as a single function with all the
necessary parameters, so that you only need to make a single call.

I have tried your suggestion without success :

For instance , if I rewrite

        [DllImport(
            @".\encode.dll",
            CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall,
            EntryPoint = @" Freeimage” )]
        public static extern int  Freeimage (
                                                [In, Out]ref IntPtr io_image
                                                );

In

[DllImport(
            @".\encode.dll",
            CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall,
            EntryPoint = @" Freeimage” )]
        public static extern int  Freeimage (
                                                [In, Out]byte** io_image
                                                );

I get an error :  Pointers and fixed size buffers may only be used in an
unsafe context

You should mark the method itself as unsafe with the "unsafe"
_modifier_:

public static extern unsafe int FreeImage(byte** io_image);

or the entire class, if you only have P/Invoke declarations in it:

unsafe class MyDllExports { ... }

The unsafe _statement_ ("unsafe { ... }") is only valid where
statements in general are valid - i.e., in method bodies.

By the way, note that [In,Out] is not needed if you work with pointers
directly (either IntPtr or proper pointers), since you deal with all
marshalling yourself there. It's only needed for ref and arrays, so
that P/Invoke knows whether it needs to copy the data back or not.
.



Relevant Pages

  • Re: PATCH? rcu_do_batch: fix a pure theoretical memory ordering race
    ... And I don't know any example of "unsafe" code in that sense. ... rcu_do_batchis just a loop like others in kernel. ... The loop itself is not buggy, but can call a buggy function, you are right. ...
    (Linux-Kernel)
  • Re: Using for loop iterator after the loop
    ... second loop is unsafe but could not say exactly why, ... with the value of n depending on how the loop is exited. ... nothing is "unsafe" about using 'n' here - its range is well defined. ...
    (microsoft.public.vc.language)
  • Re: Using for loop iterator after the loop
    ... second loop is unsafe but could not say exactly why, ... with the value of n depending on how the loop is exited. ... insertion sort will fail because there's no free space for a new element. ...
    (microsoft.public.vc.language)