Note that there are some explanatory texts on larger screens.

plurals
  1. POCallback with PInvoke is very slow
    primarykey
    data
    text
    <p>I'm using a native/unmanaged C++ DLL in my C# application. I call a native function to register a callback method in C# using PInvoke:</p> <pre><code>[DllImport("MyHook.dll", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.U1)] // necessary due to http://dotnet.dzone.com/articles/pinvoke-c-bool-return-values private static extern bool InstallHook(CallbackPrototype callback); private delegate int CallbackPrototype(byte* bytes, int size, int max); // ... var callback = new CallbackPrototype(MyCallback); _callbackHandle = GCHandle.Alloc(callback); var result = InstallHook(callback); // somewhere when I'm removing the hook, I call _callbackHandle.Free(); </code></pre> <p>Then, when an event happens, the native C++ DLL calls my callback.</p> <pre><code>private int MyCallback(byte* bytes, int size, int max) { //var buffer = new byte[size]; // A //Marshal.Copy(new IntPtr(bytes), buffer, 0, size); // B //WrapperInstance.ParseBytes(buffer, 0, size); // C var x = 1 + 1; // D return size; } </code></pre> <p>When everything is commented out, it works well. If I start uncommenting line A in the callback, it gets veeery slow. I notice it because the callback is called in a critical moment. It isn't acceptable that the callback runtime duration is so long that I notice it as a human being.</p> <p>How could this happen? The <em>size</em> parameter is only from one to 100. Thats only 100 bytes at maximum that need to be allocated - very few for todays computers. I need improvements to make the runtime of the callback much faster. Any ideas?</p> <p>To go further in testing, I've added line D. This does not have any impact.</p> <p>Edit: For more detailed testing I've allocated a big buffer when the class is created. So line A is not in the callback anymore. If line B is then commented out, it suddenly starts taking a huge amount of time again. This must have something to do with the managed code / virtual machine. I hope there are possibilities to make it faster.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload