CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2008
    Posts
    2

    Calling C functions using a C# project

    Hello all.

    I I've been tasked with quite an interesting project right now, but I honestly don't even know where to start. I really need some input from some experienced .net developers.

    The project I was assigned, was that I have to take a certain module of a project written in C for an embedded system, have it compile in a Windows environment, by itself (using some helper files to provide support for any calls to other modules), and create a C# Windows application that can be used to provide input to the C module, and observe its state and outputs afterwards (the module is a state machine).

    Now, where I am right now, is that I've decided to use Visual C++ Express to create a project for the C code that I already have, and compile it using the Visual Studio C Compiler, so I shouldn't have a problem there, and I will be using Visual C# Express to create the windows application.

    The question I have is, how would I call the necessary C functions in the C# project?

    I am an experienced C# and C programmer, but I've never created any sort of project with outside dependencies and calls like this, especially in 2 different languages, so any input on the matter would be helpful. From a little prliminary research on the subject, I've seen that some people use DLLs or the PInvoke method, but I am unaware on how to use either, or if either will even be useful to my needs.

    Once again, any input would be greatly appreciated. Thanks.

  2. #2
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Calling C functions using a C# project

    Maybe you could make a DLL to communicate data between C# project and C project.
    The difficulty is that you have no idea how difficult it is.

    .Net 3.5/VS 2008

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Calling C functions using a C# project

    If you have the C code you can build a managed C++ library using Visual C++ Express. That will give a bonafide .NET assembly. You can add that assembly as a reference in your C# application. Provided you follow the rules (extra syntax) of managed C++ your assembly will look like any other .NET assembly as far as your C# application is concerned. The only tricky bit with the managed C++ is that it has extensive syntax to be able express managed and unmanaged data types within one assembly. The other alternative is the platform invoke which I think is nastier. You would only go for the platform invoke if you're given a compiled Win32 dll.

  4. #4
    Join Date
    Mar 2008
    Posts
    2

    Re: Calling C functions using a C# project

    hmmm... is it possible to make a c++ library using C code, though? Do I have to make a lot of changes to get it to be recognized as managed c++?

    Right now, the project is about 10k lines of code, and all is written to MISRA C Coding standards.

  5. #5
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Calling C functions using a C# project

    If you already have C code in a library (dll) then I would use platform invoke. It is possible to write C code against a C++ compiler. A c++ compiler would recognise classes and structs but you don't have to take advantage of C++. In fact in early days people still wrote C style (procedural as opposed to object oriented) code on C++ environments just out of habit...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured