making a .dll file with gcc to use in C# (MSVS)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zman77
    New Member
    • Sep 2007
    • 17

    making a .dll file with gcc to use in C# (MSVS)

    Hi.
    How do I use gcc to compile C code into a .dll file that can be used by my C# project?

    The problem I am faced with is this: I have C code compiled with gcc. I tried making a new C++ project in visual studio (so that I can create a dll) and just copying the C code, but language extensions and void pointers prevented me from compiling. I tried compiling as C code and turning off Microsoft Language Extensions in my project properties, but that did not work.

    So... I need to be able to access some functions from the C code (that compiles with gcc) in my C# project (MSVS). I think creating a .dll file out of the C code and then using that in my C# project would be easiest. How can I do this? If there is a better way to do it, what is it?

    Thanks for any help.
  • zman77
    New Member
    • Sep 2007
    • 17

    #2
    I just found out about MinGW and have been using that. I can make dll files from C code. However, when I use them in my C# code, compile, and then run the executable, it hangs. I've followed a bunch of tutorials (most recently, this one: http://www.adp-gmbh.ch/win/misc/mingw/dll.html ), but they all give the same result - C# executable hangs. If I use the dlls in C code, the C executable runs fine.

    What am I missing here?

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      No, you can't directly use C libraries with C#. It's even problematic using libraries across different compilers. You want to use them with C#, that expects managed assemblies?

      There is significantly more complexity to this. I suggest you open up Google and type in the question C DLL from C#, or hte like, because you'll find results like this. Until you have a specific question about the material you can find through 5 seconds of Google search, we can't do much more than point you in this direction.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I assume your gcc-compiled library was done on a WIndows machine.

        Otherwise, the system calls will be for the wrong operating system.

        The OP wasn't clear on this point.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Originally posted by weaknessforcats
          I assume your gcc-compiled library was done on a WIndows machine. Otherwise, the system calls will be for the wrong operating system.The OP wasn't clear on this point.
          Yeah, you would have to read carefully and know that MinGW was on Windows (he did mention MinGW in his second post). The point is, you either have a COM DLL or a regular DLL. COM is easier to work with. Regular means you have marshalling and all that fun stuff.

          Actually, this question may have to be moved to the .NET forums. Looks like our OP has no issue with his C code. He just can't figure out how to work with regular DLLs in .NET. But then again, they'll just tell him to read the results from Google.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Let's move this thread to the .NET forum and see if help arrives.

            Comment

            • zman77
              New Member
              • Sep 2007
              • 17

              #7
              Ok I guess starting with the example I did, and the one you linked me to, I can't create a wrapper class.

              This is my code:

              ztest.c:
              [PHP]#if defined(WIN32)
              # define DLL_EXPORT __declspec(dlle xport)
              #else
              # define DLL_EXPORT /**/
              #endif

              #include <stdio.h>
              #include <string.h>

              DLL_EXPORT void dll_fct (char *par)
              {
              printf("executi ng dll_fct with parameter:%s\n" ,par);
              }

              DLL_EXPORT int dll_fct2 (int a, int b)
              {
              return(a+b);
              }[/PHP]

              Zwrap.cs:
              using System;
              using System.Runtime. InteropServices ;

              [PHP] public class Zwrap
              {

              [DllImport("zo.d ll")]
              private static extern void dll_fct(string par);

              [DllImport("zo.d ll")]
              private static extern int dll_fct2(int a, int b);

              static void Main(string[] args)
              {
              Console.WriteLi ne("before call dll_fct");
              Zwrap.dll_fct(" hello");
              Console.WriteLi ne("after call dll_fct");

              Console.WriteLi ne("calling other function...");
              int a = 5, b = 9, c = 0;
              c = Zwrap.dll_fct2( a, b);
              Console.WriteLi ne("the result is: " + c);
              }
              }[/PHP]

              The above works fine. However, when I take out the DllImports from Zwrap.cs, and put them in a proper wrapper class, like so:
              MyWrapper.cs
              [PHP]namespace MyWrapper
              {
              public class MyWrap
              {
              [DllImport("zo.d ll")]
              private static extern void dll_fct(string par);

              [DllImport("zo.d ll")]
              private static extern int dll_fct2(int a, int b);
              }
              }[/PHP]

              I am unable to compile Zwrap.cs. I compile MyWrapper with "csc /t:library /out:MyWrapper.d ll MyWrapper.cs" In Zwrap.cs, I do "using MyWrapper;" and when I try to access a function in main, I do "MyWrap.dll_fct 2(a, b);" however, I get an error: 'MyWrapper.MyWr ap' does not contain a definition for 'dll_fct2'

              So if I make a C# wrapper class, and make a DLL out of it, I can't get it to work. However, if I don't make a C# wrapper class, and just do DllImport in my C# code, and compile into an executable, it works fine. What am I doing wrong?

              Thanks for your help so far, that link cleared up some things.


              EDIT: Yes, I am using MinGW on a Windows machine. I think my problem is more because I have to turn C code into a DLL that is usable in C# - if I had to make a C# dll that was usable in C#, it wouldn't be this much hassle, I think. I foresee having to do many other things to get the C code to work properly, this was just a simple example to get me started.
              Last edited by zman77; Nov 14 '07, 07:35 PM. Reason: names, replies

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                You set your functions to private.
                Make them public so you can access them outside the class.

                Out of curiosity, why did you end up making a C# dll for a C DLL?
                If you build the C DLL in the format that makes it a COM dll, you can just use DLLImports in your code?

                Comment

                • zman77
                  New Member
                  • Sep 2007
                  • 17

                  #9
                  Wow, thank you. That was dumb of me.

                  Originally posted by Plater
                  If you build the C DLL in the format that makes it a COM dll, you can just use DLLImports in your code?
                  Well, I don't know what format is a COM dll. I could have included DllImports wherever I needed to use the certain function from the dll, but I thought it would look "cleaner" if I just put all the imports in one file. That and I wanted to try it. At any rate, I guess this is solved (thank you), and now I'm going to see what happens when I have to send in and return structs from the C dll.

                  Again, thanks everyone for the help

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Well I had thought that all C DLL's were COM DLLs, but it seemed like a previous response had said no.
                    I would say a simple class file in your project to handle all the DLLImports would suffice, no need to make ANOTHER project just to make them into a dll again.

                    Comment

                    Working...