Go Premium for a chance to win a PS4. Enter to Win


Are system calls more reliable from MFC than .NET?

Posted on 2007-12-05
Medium Priority
Last Modified: 2013-11-20
Are there any known problems with making system calls (such as Desktop calls, WIndowing calls, or ShellExecute or CreateProcess) in .NET? Is it better to write such an application in MFC?
Question by:jobrems
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20412020
As far you specify marshalling properly, it will have the same reliability as MFC. Indeed you don't call from MFC (unless you are using a MFC wrapper class), you call natively the WinAPI applications.
Of course there is a little cost to invoke WinAPI function from .net, since .net makes additional checkings.
LVL 19

Expert Comment

ID: 20412673
Sometimes is more easy to make system calls from native code (MFC) because you can use "ready to use" include files. As jaime_olivares said you have also  little better performance.

Accepted Solution

multithreading earned 1000 total points
ID: 20413991
There are a million ways to get into trouble making calls into the various DLLs from C#, but if you know enough to program in C++, and if you know the CLR well, then you are unlikely to run into them. Given that you can probably avoid the various problem areas in maping functions, the choice of language comes down to what you are gaining and what you are losing, with regard to these system calls, by using .Net.

The more complicated the call, the more likely you are to have problems. If you call a method that takes no arguments and returns nothing, then there isn't much room for trouble. On the other hand, if you call a method that takes a struct, which contains pointers to other structs, and returns a handle, you have several complications. In addition to needing to build a object hierarchy in .net with pointers, you have gotten back a handle (like you would expect to see with some of the calls you mentioned) which (depending on the level of support for that particular RAW handle) may not be directly useable in .Net. Getting back handles etc. can force you to make more system calls, and the next thing you know, you are writing a C++ application, except that you keep having to look up #defines for all kinds of int values etc., and figuring out how you are going to represent things in C#. All of that is before we get into any object lifetime issues...

Look at the overall application. How many different calls are you going to need? What are the arguments and return values? If the calls can be made using regular c# types, and you aren't getting pulled into the vortex, then c# is your answer. If you find that looking up and defining the calls and argument types is taking you more time than you are saving by using c#, then use C++ instead.

Author Closing Comment

ID: 31412863
Thanks for your detailed answer. I also wanted to know: Are there some system calls which, when made from unmanaged code in .NET, execute incorrectly or with unexpected results?

Expert Comment

ID: 20427644
It's kind of complicated. Most calls work fine. Calls that involve callbacks can get you into trouble. I've also seen people frequently get into trouble with calls that take pointers to character buffers. There are other ways to get into trouble, but the more you know the less likely that is to happen. I think the best advice would be only programmers with a solid understanding of the CLR, C/C++, and the Win32 API should be making these mappings. There is a little bit more to it than meets the eye, especially with calls that take pointers to various objects. In the hands of someone with a thorough understanding of the various issues, you will be fine.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question