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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
source code to scan multiple images 6 96
noX challenge 17 130
How to convert MFC::CString to UTF8 wchar_t* 10 294
Detect file exist or not 3 181
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
The viewer will learn how to use and create keystrokes 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.

821 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