-1

I have to develop a testing application to test some of the features of the graphics driver. By making use of the standards exposed by Microsoft, like DXVAHD.h,DXVA2Api.h (Directshow) and other Microsoft APis. As coding in C# is very easy compared to C++, why cant we use C# instead. Is it because, we can't use header files in C# (As there is a concept of packages)?

Can anyone kindly let me know why do one prefers C,C++ over C#, when it comes to system level programming. ? Any links or learning materials W.R.T. these would be greatly appreciated.

Thanks in advance.

codeLover
  • 3,720
  • 10
  • 65
  • 121
  • Smells like off-topic. – Soner Gönül Mar 12 '13 at 08:41
  • Pointers ... you pass objects around by reference to memory rather than copying objects – Sayse Mar 12 '13 at 08:42
  • @Sayse in c# the objects are passed by reference by default – VladL Mar 12 '13 at 08:46
  • Off-topic.!! It's a question related to programming. I just wanted to get clear understanding on this,hence raised this question. Not sure why it was closed. – codeLover Mar 12 '13 at 08:46
  • 6
    Short answer: C# is compiled to managed bytecode, which runs on top of byte code running "virtual machine". Drivers need to manipulate stuff directly on the real CPU and have unmanaged memory access for the things they need to do. – hyde Mar 12 '13 at 08:47
  • 4
    The answer is [abstraction](http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29). Abstraction makes things easier to do but at the same time makes it harder to access lower level stuff. – Jesse Good Mar 12 '13 at 08:52
  • Yes, but we can achieve the something using C# right? Provided there is a way to access the definitions of the Microsoft header files, like dxvahd.h,dxva2api.h etc.. from C#? – codeLover Mar 12 '13 at 08:56
  • No, you cannot succeed some stuff without ability to call really low level. And in C# even `untrusted` isn't low enough to achieve some things. – Hauleth Mar 12 '13 at 09:06
  • @VladL - Oh right thanks for that! this is what happens when you learn c++ and c# at the same time lol – Sayse Mar 12 '13 at 09:25
  • Can you kindly explina the meaning of words "low level" here. For eg:: memcpy() is a low level operation,where you copy contents (bytes) of memory from 1 address to another.You can achieve samething in C#. I am not trying to prove myself correct, but I just trying hard to CLEARLY UNDERSTAND the term low "LOW LEVEL" and what is that C/C++ can do but not C#. – codeLover Mar 12 '13 at 09:30
  • theoretically you can call every c++ code in the unsafe block in c#, don't know if it's sufficient. +1 and voted to reopen as I would love to hear the opinion of experienced members – VladL Mar 12 '13 at 09:31
  • Yes very eagerto CLEARLY and COMPLETELY understand this & expert opinion/inputs on this. – codeLover Mar 12 '13 at 09:39
  • Some people use C++ because they know that, and that makes it easy. I don't know C#, so using that would be very hard. One reason for the preference is that I have used C++ since before C# was designed. – Bo Persson Mar 12 '13 at 09:57
  • @hyde it's entirely possible to write lowlevel code using C# though you would need either another compiler than csc/mono or a IL to machine code compiler http://channel9.msdn.com/Shows/Going+Deep/Singularity-A-research-OS-written-in-C – Rune FS Mar 12 '13 at 10:19
  • @VladL SO is not the place to share opinions, it's a place to share expertise and fact. This question would have to be a lot more specific to afford that. – millimoose Mar 12 '13 at 11:15
  • @millimoose true, so as an answer I'm expecting a fact for why should I use/not use C# for driver programming. SO is full of such questions and they are very useful, for example this one http://stackoverflow.com/questions/33746/xml-attribute-vs-xml-element – VladL Mar 12 '13 at 11:22
  • 1
    @VladL Questions from 2008 are not necessarily representative of current moderation policy. Part of SO's "charter" is to be the opposite of a bulletin board / forum, which is why I feel inherently subjective questions should work *harder* to justify their existence. This one really doesn't, at its core it's "C++ vs. C# for systems-level programming, discuss". It doesn't help that "systems-level programming" is a nebulous term to begin with, and this OP hasn't really cleared it up. – millimoose Mar 12 '13 at 11:25
  • @VladL An example of what I (but not necessarily other people with close privileges) feel *might* salvage this question would be the OP outlining his use case in more detail than "testing some features of the graphics driver", so as to let the people answering really judge *why* C++ or C# is or isn't suitable for him. Otherwise you just get waffling around like "I think C# can't do this", "I think C#" can surrounded by the pretense of factuality. (E.g. "C++ can set registers" when the OP might not really need that except we have nothing to go on to tell whether he does.) – millimoose Mar 12 '13 at 11:29
  • @VladL tl;dr: I personally believe subjective questions should be at least as (if not *more*) specific than objective ones to fly on SO. This one really isn't very specific at all, and in fact betrays a lack of certain fundamental knowledge on the OP's part which makes it a bad foundation for even a fruitful discussion. (See: the weird comment about header files, which are certainly not a package / modularity mechanism, and it's not like .NET doesn't have that.) – millimoose Mar 12 '13 at 11:30

3 Answers3

4

When you write system software or drivers, you want tight control over your environment: memory usage, time constraints etc.

With C# it is not possible. Even if you ignore the fact that you run on top of a VM which introduces significant overhead, you still have no control over memory allocation/deallocation policy, you are never sure when GC thread will kick in and block your worker threads, etc.

Besides that, there are many low level operations that you just can't do in C#: starting with reading/writing the value of device registers or direct access to the memory and moving onto more specific tasks.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
3

There's no real reason why you couldn't do it in C# other than some of the APIs being C++ APIs. Unlike what some people have said in the comments, you are not creating a driver, you are creating (quote) "a testing application to test some of the features of the graphics driver". (And why would anyone create a driver unless they're hardware manufacturers?)

Popular APIs like DirectShow have C# bindings. For those that don't, you can easily create your own by wrapping the APIs in C++/CLI assemblies that the C# code references.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • 2
    It is no problem to call C++ APIs from a C# application. The real problem with using C# are the inherent limitations of a JIT-compiled language that runs on top of a runtime environment like the .NET Framework. If it's just an in-house test app, that's probably irrelevant. If you're a driver manufacturer shipping this app to large numbers of customers, you might want to rethink this. Remember ATI's debacle where their graphics card configuration app, written on top of the .NET Framework, took several *minutes* to open? Super embarrassing. – Cody Gray - on strike Mar 12 '13 at 10:24
  • Again, he/she is not creating a driver, but a normal application that tests a driver. And .NET apps by no means take several minutes to open. – user1610015 Mar 12 '13 at 10:28
  • They do the first time the framework is initialized on a cold start. At least they did a year or so ago. Hardware has improved slightly, but software bloat tends to keep up pretty well. – Cody Gray - on strike Mar 12 '13 at 10:38
  • .NET does have a higher load time than C++, but that's maybe 0.2 seconds. But the ATI app that you describe sounds like one of those that have a heavy UI and a bunch of other things to initialize, and those exist in C++ too (think games). – user1610015 Mar 12 '13 at 10:44
  • @user1610015 Seeing as the ATI configuration app was skinned heavily and hideously, the fact that it was written in .NET likely alone didn't explain the slowdown. (Anecdotally, MonoDevelop takes <10s to open on my macbook, and that's certainly a very complex application that loads up a binding to Gtk, itself not a lightweight library.) – millimoose Mar 12 '13 at 11:18
0

C is called middle-level language because it is actually binding the gap between a machine level language and more conventional high-level languages. User can use c language to do System Programming (for writing operating system) as well as Application Programming (for generate menu driven customer billing system ). So that it is called middle level language.

High level - Java- Ada , Modula-2 , Pascal, COBOL, FORTRAN, BASIC

Middle level - Java(through C interface), C++, C, FORTH, Macro-assemble

Low level - Assembler

Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76
  • I would give Java and C# to high level if you are writeing that Ada nad Pascal are high level, because it is higher than them, it use JVM nota bene. – Hauleth Mar 12 '13 at 08:49
  • Sure Lukaz Niemier ..But Here I am Speaking About Which Language is Middle(binding the gap between a machine level language )so Java Can Be In High Level And Middle Level.. – Mohamad Mahmoud Darwish Mar 12 '13 at 08:58
  • 1
    So can be Ada or Pascal. Java can do this only through C interface, by calling native functions through JNI. Pascal can do this directly, can even be linked with pure Asm code. – Hauleth Mar 12 '13 at 09:03
  • @Nicol Bolas .. Me too Never heard this term before ..but when reading about it ..i accept it ..http://cprogrammingcodes.blogspot.com/2011/12/middle-level-language.html ... – Mohamad Mahmoud Darwish Mar 12 '13 at 09:58
  • @Mohamad I'm not sure a random low-content blog written in slightly broken English is a reputable authority on what technical terms mean. Personally, I do consider C a low-level language, and assembly as "barely a programming language", seeing as it doesn't really offer much in terms of language constructs. (As opposed to mnemonics for instructions.) – millimoose Mar 12 '13 at 11:19