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. Create C# dll that acts like a C++ dll

Create C# dll that acts like a C++ dll

Scheduled Pinned Locked Moved C#
c++csharphtmloraclecom
8 Posts 5 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.
  • P Offline
    P Offline
    Preston McCormick
    wrote on last edited by
    #1

    There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.

    N T V 3 Replies Last reply
    0
    • P Preston McCormick

      There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      AFAIK that's not possible. C# is managed code, while C++ is unmanaged code. Data and object marshaling in both is very different -not to mention the structure of the dll file, and the method of function exporting-.

      Regards:rose:

      S 1 Reply Last reply
      0
      • N Nader Elshehabi

        AFAIK that's not possible. C# is managed code, while C++ is unmanaged code. Data and object marshaling in both is very different -not to mention the structure of the dll file, and the method of function exporting-.

        Regards:rose:

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.

        Steve

        N 1 Reply Last reply
        0
        • S Stephen Hewitt

          While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.

          Steve

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Stephen Hewitt wrote:

          I’d be willing to bet it is possible.

          If you do, then why didn't you tell us how to do it! Yes, it's possible somehow, but not in his scenario. You may mean the COM wrapper of DotNet that enables you to make your objects COM visible. He wants to make a C# dll for a program that references a C++ dll without COM, and my bet is that the program doesn't expect a managed code either. Also note that the function doesn't belong to a class which is not allowed in C#.

          Regards:rose:

          S 1 Reply Last reply
          0
          • N Nader Elshehabi

            Stephen Hewitt wrote:

            I’d be willing to bet it is possible.

            If you do, then why didn't you tell us how to do it! Yes, it's possible somehow, but not in his scenario. You may mean the COM wrapper of DotNet that enables you to make your objects COM visible. He wants to make a C# dll for a program that references a C++ dll without COM, and my bet is that the program doesn't expect a managed code either. Also note that the function doesn't belong to a class which is not allowed in C#.

            Regards:rose:

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Here's what I said:

            Stephen Hewitt wrote:

            While I don't know much about C# I am aware that MS went to some effort to make managed code and native code interoperate. I’d be willing to bet it is possible.

            Nader Elshehabi wrote:

            If you do, then why didn't you tell us how to do it!

            Isn't the answer to this question obvious? "I don't know much about C#". I don't know how, it's just my opinion that it’s possible. What’s more, I’d wager it’s not that complex.

            Steve

            1 Reply Last reply
            0
            • P Preston McCormick

              There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.

              T Offline
              T Offline
              Tim Paaschen
              wrote on last edited by
              #6

              As already explained by others, C# can only produce managed code, while your sample is an unmanaged DLL. There are several ways to implement managed-unmanaged interoperability, but there is no simple solution for what you want to do. One idea that come to my mind requires the creation of two DLLs: One unmanaged C++ DLL that exports your required method and transforms it into a COM method call. And a COM server DLL that provides the method consumed by the first DLL. The second DLL can be written in C# as managed DLLs can be designed to be callable by COM clients (CCW).

              Regards, Tim

              P 1 Reply Last reply
              0
              • P Preston McCormick

                There is a program calling a C++ dll named "ffisamp.dll". The C++ code is as follows: ***** //Simple C function that just puts something in a string // and returns the length of that string both as the RC and as a param // Keeping the C interface as simple as possible. __declspec( dllexport ) int PopulateString ( char *FormsBuffer, int *BuffLen) { int LocalLength; strcpy(FormsBuffer,"A Fixed string from within the C program"); *BuffLen = strlen(FormsBuffer); LocalLength = strlen(FormsBuffer); return LocalLength; } ***** I need C# code that will compile to a dll that mimics the sample dll such that no modifications will need to be made to the calling program. I need to be able to just replace the old dll with the new one. I am most perplexed that the C++ method is not in a class. That might be that the source code is just a snippet of the whole dll's source code... since it comes from a sample, but I have the impression from the documentation that is all of it. Here is a link to the article I am working with. I am trying to communicate with an Oracle 10g form using methods to call C functions. http://www.oracle.com/technology/products/forms/htdocs/webutil/howto_ffi.html[^] I've tried a few things, but so far the calling application doesn't even find the function. :(( I am not familiar with C++, MFC etc... so I am sure I am missing some fundamentals.

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                I'm not sure if this is what you want, but it might be helpful: Interop[^]

                V. I found a living worth working for, but haven't found work worth living for.

                1 Reply Last reply
                0
                • T Tim Paaschen

                  As already explained by others, C# can only produce managed code, while your sample is an unmanaged DLL. There are several ways to implement managed-unmanaged interoperability, but there is no simple solution for what you want to do. One idea that come to my mind requires the creation of two DLLs: One unmanaged C++ DLL that exports your required method and transforms it into a COM method call. And a COM server DLL that provides the method consumed by the first DLL. The second DLL can be written in C# as managed DLLs can be designed to be callable by COM clients (CCW).

                  Regards, Tim

                  P Offline
                  P Offline
                  Preston McCormick
                  wrote on last edited by
                  #8

                  Well, I actually expected it was the case that I could not fully duplicate the external behavior of a C++ dll, but I wanted to make sure. The C++ wrapper dll occurred to me and would be possible since we have C++ developers, but it is not a good solution. Ultimately the problem was solve in another manner. We are communicating through he standard output stream and it works great. Thanks for everyones comments.

                  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