Unsafe methods and managed code

Dear all,

I am re-designing an image/video processing package that I wrote a while ago to use C# for the GUI part such that development on that end is quicker. However, I want to keep the performance by having the processing run on unmanaged code.

There are of course many ways to do this but the simplest that I have found was to have my interfaces declare an "unsafe" method that processing classes will override.

However, I am not certain and couldn't find any info on it either, if "unsafe" also means that the code will run unmanaged.

Brief code listing:

C#
public interface Processor {
 unsafe void doStuff(void*);
}

C++
class MyProcessor : public Processor {
 unsafe void doStuff(void*) {
 ........
 .........
 }
};

So to sum up, will MyProcessor::doStuff run like or near-like a pure C++ library would

Thanks in advance.


Answer this question

Unsafe methods and managed code

  • SpaceDog

    Hi Georgios

    This is not my area of expertise, so I invite anyone else to comment on this thread.

    For objects, Managed C++ (C++/CLI) is probably your best bet.  Unless they are COM objects, in which case interop should suffice.  In either case, the runtime does the marshalling for you.

    Hope that helps

    -Chris


  • Colin Osbaldeston

    I recommend P/Invoking into your unmanaged DLLs.  As long as there isn't a managed thread running at the same time in the same process, and as long as the machine doesn't run low on memory, the GC shouldn't cause a collection, and your performance should be good.

    Hope that helps

    -Chris


  • boss nunya

    Hi Georgios

    No, the unsafe keyword does not make a method run unmanaged.  All unsafe does is allow the use of pointers.

    See this MSDN article for more information:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/csref/html/vclrfunsafe.asp

     

    -Chris


  • Inray

    Thanks Chris.

    A few questions on that:

    - I was hoping the plugin DLLs to contain objects rather than C functions. Will P/Invoke work ok with that

    - I will need to pass an unsafe pointer to the DLL, will the data pointed to by the pointer be transfered without marshalling

    I hope MS improves the documentation on how unsafe contexts work in managed apps. Unless I'm just making it more complex in my own head.


  • psygone13

    OK, that makes sense.

    What do you think would be the best way to interface native plugins with managed code then

    Thanks,

    Georgios

  • Unsafe methods and managed code