Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exporting functions from C# .dll for other programs

Exporting functions from C# .dll for other programs

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpjsonhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 1033907
    wrote on last edited by
    #1

    Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.

    C D 2 Replies Last reply
    0
    • M Member 1033907

      Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Member 1033907 wrote:

      If so, how do I make them as regular (global) functions, without being nested in some namespace and class?

      Not possible in .NET. You can't 'export' them, you'd need to write a COM dll and access them via COM.

      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

      M 1 Reply Last reply
      0
      • M Member 1033907

        Hi I need to write a C# .dll library which exports functions accessible from good old win32 API applications. (not the usual other way around, calling win API functions from C#) Is this possible? If so, how do I make them as regular (global) functions, without being nested in some namespace and class? The win32 api application requires for example "void FunctionA()" and "void FunctionBee()" to be exported in my dll, but as far as I know, C# does not allow for functions to be on their own, without being in a namespace and class. Other than that, I do not know how to mark them as "exported", some attribute or something? Please point me in the right direction, any help appreciated. Thanks, H.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Christian is correct. The only "easy" interface you have is to expose your classes, properties, and method is through COM. You could try to host the .NET CLR in your Win32 app, but that's a severe pain in the ass to do. Read[^]

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        M 1 Reply Last reply
        0
        • C Christian Graus

          Member 1033907 wrote:

          If so, how do I make them as regular (global) functions, without being nested in some namespace and class?

          Not possible in .NET. You can't 'export' them, you'd need to write a COM dll and access them via COM.

          Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

          M Offline
          M Offline
          Member 1033907
          wrote on last edited by
          #4

          That is bad news, I'll probably need to reconsider the whole approach. One last shot - if I leave out the requirement that they need to be standalone functions and write something like

          namespace Space
          {
          public class Something
          {
          public static void FunctionA()
          {
          // do something
          }
          }
          }

          Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing). If so, could you give me a little clue how (on the c++ side)? Or another way to make it work with just the C++ app and the C# dll? (I have access to both) Thanks in advance, H.

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Christian is correct. The only "easy" interface you have is to expose your classes, properties, and method is through COM. You could try to host the .NET CLR in your Win32 app, but that's a severe pain in the ass to do. Read[^]

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            M Offline
            M Offline
            Member 1033907
            wrote on last edited by
            #5

            Thanks. Please read my answer above, I'll be glad if you have something to say. Otherwise I'll probably forget about C# altogether. BTW: "through COM" means that I 1) write the C# .dll with the functions (as public static methods) 2) write another library which receives calls from the win32 application and forwards them to the C# dll? Thanks, H.

            1 Reply Last reply
            0
            • M Member 1033907

              That is bad news, I'll probably need to reconsider the whole approach. One last shot - if I leave out the requirement that they need to be standalone functions and write something like

              namespace Space
              {
              public class Something
              {
              public static void FunctionA()
              {
              // do something
              }
              }
              }

              Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing). If so, could you give me a little clue how (on the c++ side)? Or another way to make it work with just the C++ app and the C# dll? (I have access to both) Thanks in advance, H.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Member 1033907 wrote:

              Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing).

              No.

              Member 1033907 wrote:

              Or another way to make it work with just the C++ app and the C# dll? (I have access to both)

              Using C# to generate the .DLL, we've already told you the options you have.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              M 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Member 1033907 wrote:

                Can this FunctionA be somehow accessed from an unmanaged C++ win32 application? (no fancy com, mfc, nothing).

                No.

                Member 1033907 wrote:

                Or another way to make it work with just the C++ app and the C# dll? (I have access to both)

                Using C# to generate the .DLL, we've already told you the options you have.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                M Offline
                M Offline
                Member 1033907
                wrote on last edited by
                #7

                Ok, thanks, no c# then. Out of curiosity - what project type one selects when creating COM dll in Visual Studio? Is it "ATL Project" or something else? I've never done that (and probably won't). H.

                D 1 Reply Last reply
                0
                • M Member 1033907

                  Ok, thanks, no c# then. Out of curiosity - what project type one selects when creating COM dll in Visual Studio? Is it "ATL Project" or something else? I've never done that (and probably won't). H.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Just a class library. The COM exposure is all in how you attribute your code.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups