Are system calls more reliable from MFC than .NET?

Posted on 2007-12-05
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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Infragistics Ultragrid 3 85
EvenOdd challenge 10 88
Unix Command -- Challenging  question 7 86
Trying to run powershell  in a batch file. How do I do this? 8 63
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…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now