Thread: Creating wrapper for C++ DLL

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Creating wrapper for C++ DLL

    Hello,

    VS 2008

    I have a .Net application that P/Invokes with a DLL written in C++. Everything works, and now I have to create a wrapper, but not sure how to do this.

    Does the wrapper need to be created in C#.Net or C++?

    In the C++ code is is just some getters and settings that I need information from.

    Most Internet sites I have looked at seem too complicated to understand.

    I am just looking for a very simple example to get started.

    Many thanks,

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Do the same you did with your normal application. Best chance of success.
    If not, either language should work.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Your wrapper has to be written in C. The C functions essentially wrap access to the underlying C++ class. Unfortunately there is no other easy way to do this except creating an ActiveX DLL via ATL.

    The problem comes into play because C# does not use 'this' call and therefore has no way of calling into a C++ class and resolving the function address. The method shown to import functions into C# are all importing C style functions. AFAIK you cannot import C++ functions into any C# code without using either ATL or a C wrapper acting as a front end to the class.

    Write your C wrapper to access the C++ class and then import the C wrapper functions into your C# project.

    So the calls will go like this:

    C# -> C wrapper -> C++ class
    C# <- C wrapper <- C++ class

    The other method is to use the ATL wizards which will define an interface which uses the Microsoft interface definition language to describe your class. Then you simply use the interface as a front end to your actual C++ class. Once you wrap this up into a DLL you can simply add it to your C# references and then just call into it as normal. The one drawback to this method is that COM objects cannot return values from functions. The only way to return values is via the stack. This is sort of a pain but you get used to it. The benefit in this is that since you are creating a COM DLL you can use this component from ANY language that operates on the Windows platform and the code will work. Another nice feature is that when you add this to your references in C# you can browse the object just like all the other references which is very handy at times.
    Last edited by VirtualAce; 06-10-2008 at 07:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating a simple DLL
    By Dark_Phoenix in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2009, 09:41 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. DLL Wrapper
    By vampirekiss in forum C Programming
    Replies: 4
    Last Post: 01-08-2008, 10:24 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. Creating and using a Dll in Dev-C++
    By Aidman in forum C++ Programming
    Replies: 2
    Last Post: 01-17-2003, 03:07 PM