Compiling a native C DLL for use in VC# using DllImport

Hi Group!
I'm a C# newbie (real wet and raw) as well as a complete newbie to Microsoft OS programming. Normally I use native 'C' on Posix complient platforms - which I've been doing for many decades.

Scruitinsed this BB for an answer but can't find anything close. Also burrowed around in Visual C# IDE for clues - none forthcoming.

For reasons of speed and size I want to write a suite of routines in native 'C' and encapsulate them in an external module - presumably a DLL.

Can someone please give me a pointer to how to get started in Microsoft products Can Visual C# IDE compile a native C routine (after all most C++ IDE's can) and what do I have to do to produce an easily incorporated runtime module.

Remember that I'm well used to the vi editor, running a GCC compiler and native object files. Microsoft's architechture is totally new to me.

General directions will help - I don't expect anyone to give me chapter and verse on technique. (eg: What additional development environment should I d/l or purchase etc..).

Thanks in advance - in a small rush on this one.

Gothmordrin



Answer this question

Compiling a native C DLL for use in VC# using DllImport

  • George_egroeG

    You're welcome.

    C# has a lot in common with C++, but a better comparison is Java. One of the differences is how reference types are treated:

    ArrayList foo;

    In C# (and J#), the line above declares a managed pointer to an ArrayList object, which is uninitialized to begin with. If you try to use it before assigning it a value, you'll get an exception. OTOH, in C++/CLI (the extended C++ syntax in VC++), it creates a new ArrayList and lets you pretend it's been allocated on the stack instead of the heap, like you can do with a regular C++ object.

    I personally prefer C# for a few reasons.. 99% of the code I write is managed, and VC++ has more complicated syntax for some of the managed features that don't exactly match up to standard C++ features, like managed arrays (int[] in C# is array<int>^ in C++/CLI). C# has some useful features that C++/CLI lacks, like iterators (methods that use the yield keyword to easily implement IEnumerable) and anonymous methods. Also, I've found that the IDE's IntelliSense works much better for C# code than C++ code.

  • amza

    Thanks for this Mr2001!

    It looks like it'll pay me to switch to Visual C++ instead.

    As I said - the Microsoft way of doing things is a totally new experience. I only chose Visual C# as it seemed to offer the fastest way to develop the GUI end of my app.

    As I understand it - what you are basically saying  is that - Visual C++ will offer a 1-IDE solution to both requirements whereas to use Vis C# I need additional (external) kit.

    I'll make the switch now.  Fortunately I haven't done that much with Vis C# at present.

    Thanks.


  • ark

    Developing the GUI in VC# vs. VC++ will be about the same. It's the same form designer, and if you're using managed classes (which all the Windows Forms components are), your code will look pretty similar either way.

    If you're using Visual Studio, you don't need two separate IDEs, even if you use both languages: you can create one solution (project group) that contains a C# project and a C++ project. But if you're using the express editions, then you'll need to switch between Visual C# Express and Visual C++ Express, which makes your job harder but not impossible.

  • Siostra Andzelika

    No.. the object oriented model of .NET wouldn't really be compatible with straight C.

  • late_nighter

    Visual C# can't compile C modules by itself, but Visual C++ can. Add a new VC++ project to your solution, and it should give you the option to make a DLL, which will have sample code you can look at to see how DLLs are done. To export a function from the DLL, you need to either mark it as exported in your source code, or add its name to your project's .def file.

    Keep the DLL functions' interfaces as simple as possible to avoid having to tweak the marshaling in your C# app.

    Also, think about what you hope to gain from doing this, and whether you might be better off writing the whole thing in VC++ (where you can mix managed code and native code in the same project) or pure C# (using unsafe blocks if you need pointers). There's a performance penalty for switching between managed and native code at runtime, and depending on exactly what you're doing, that might offset the benefits of using native code in the first place.

  • Ultan

    Thanks again Mr2001.

    Glad to get your last post as I was intending to trash my C# IDE tonight.
    I won't now.
    I never got fully round Java (I'm really a native C man through and through and "lovin' it!").
    I could write the GUI in C# and use the C++ IDE to create a native C dynamic link library - which I can then incorporate into the project using dllImport (or whatever the actual name of the function - er.. sorry... method is).

    Just to throw in a yorker - the GUI will actually be part of an NDIS intermediate protocol.  There will be a "firewall-esque" component which will live as close to and right beneath the NIC as I can possibly pursuade it to go. The NDIS entry point will be the native C routine(s) and these will have the GUI "hanging off" of them.

    Is there such a thing as Visual 'C' (with as flashy an IDE as Vis C#) - 'cos as far as I'm concerned - that would be the bee's knees!
    I'd be well at home there.

    Thanks ever so much for your valued help.

    I'll crack this thing yet!

    Regards,

    Gothmordrin



  • Jumpercables

    Thanks again for this.

    I'm using the Express versions of both at the moment but - as I said earlier - I've not got much done in Vis C# and since Vis C++ can compile native C while Vis C# can't I'll just stick with Vis C++.

    Having viewed several of the Vis C# "absoulte newbie" vids from Microsoft - I'd already arrived at the conclusion that C# and C++ are - as near as damn - both the same. In fact I'm intrigued as to what the difference actually is(!).  Why choose C# over C++ for a solution
    Is C# a re-invention of the OOP wheel

    Mind's made up - ditching C# and using C++.

    Thanks Mr2001 and group. You've received me warmly and I appreciate it.

    Gothmordrin



  • Compiling a native C DLL for use in VC# using DllImport