CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2004
    Posts
    27

    Wrapping C++ functions

    I'm trying to call a C++ function in a dll from a GUI I made with Winforms in VS.NET. I'm doing it something like this:

    [DllImport("Data.dll")]
    private static extern bool IsOpen();
    ...
    static void Main(){
    IsOpen();
    //do something
    }

    The error I keep getting is
    "Unable to find an entry point named IsOpen in DLL Data.dll"

    Since the function is actually CDataApp::IsOpen() in the .dll I tried to make a wrapper function like this:

    BOOL IsOpen(){
    return theApp.IsOpen();
    }

    but I get the same error...Any suggestions?

  2. #2
    Join Date
    Jan 2004
    Posts
    43

    Re: Wrapping C++ functions

    be sure, when you exporting your function in C++
    you do this inside

    extern "C"
    {
    .....
    ......
    }



    Have a look at your DLL with "DependecyWalker", you will see that the function name looks strange.

  3. #3
    Join Date
    Jul 2004
    Posts
    27

    Re: Wrapping C++ functions

    Quote Originally Posted by MikeNovy
    be sure, when you exporting your function in C++
    you do this inside

    extern "C"
    {
    .....
    ......
    }



    Have a look at your DLL with "DependecyWalker", you will see that the function name looks strange.
    1. Why 'extern "C" '?? I'm not using C anywhere...
    2. DependencyWalker?

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Wrapping C++ functions

    Why don't you just create a .NET wrapper assembly written in managed C++ for your dll. In fact if you've got access to your dll's functionality why not transfer it to a managed C++ assembly ?

    If you know C++ and C#, it's really not difficult. It's what I do all the time to overcome problems like this.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Jul 2004
    Posts
    27

    Re: Wrapping C++ functions

    Quote Originally Posted by darwen
    Why don't you just create a .NET wrapper assembly written in managed C++ for your dll. In fact if you've got access to your dll's functionality why not transfer it to a managed C++ assembly ?

    If you know C++ and C#, it's really not difficult. It's what I do all the time to overcome problems like this.

    Darwen.
    Maybe I should point out that I'm VERY new to C++ and C#. Anyone know of any helpful tutorials b/c I'm really not understanding anything that anyone is saying ???

  6. #6
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: Wrapping C++ functions

    Quote Originally Posted by el_fuego83
    I'm trying to call a C++ function in a dll from a GUI I made with Winforms in VS.NET. I'm doing it something like this:

    [DllImport("Data.dll")]
    private static extern bool IsOpen();
    ...
    static void Main(){
    IsOpen();
    //do something
    }

    The error I keep getting is
    "Unable to find an entry point named IsOpen in DLL Data.dll"...?
    Yes, try:
    Code:
    using System.Runtime.InteropServices;
    
    [DllImport("Data.dll")]
    private static extern bool CDataApp.IsOpen();
    When looking for more information on this search for "PInvoke".

  7. #7
    Join Date
    Jul 2004
    Posts
    27

    Re: Wrapping C++ functions

    when I try that I get these errors:

    'The modifier 'private' is not valid for this item' and

    'The modifier 'static' is not valid for this item'

  8. #8
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: Wrapping C++ functions

    Sorry, cut-n-paste error, change the modifier to public and remove static. Accessing native DLLs requires that the method is public and it may NOT be static.

    If you need anything else, create a wrapper class and have the class methods call the DLL using PInvoke. The PInvoke methods will still need to be public (and not static).

  9. #9
    Join Date
    Jul 2004
    Posts
    27

    Re: Wrapping C++ functions

    I still get the

    'The modifier 'public' is not valid for this item' error when I try to compile...

    If you need anything else, create a wrapper class and have the class methods call the DLL using PInvoke. The PInvoke methods will still need to be public (and not static).

  10. #10
    Join Date
    Jan 2004
    Posts
    43

    Re: Wrapping C++ functions

    Quote Originally Posted by el_fuego83
    1. Why 'extern "C" '?? I'm not using C anywhere...
    2. DependencyWalker?


    1. Don't think about it... do it! You have to do does always
    2. Is a tool coming up with Visual Studio to check, which functions in a DLL exist.

  11. #11
    Join Date
    Dec 2003
    Location
    Montreal
    Posts
    58

    Re: Wrapping C++ functions

    I'm sorry I was in error on the static part, it IS required (that's what you get when you answer from memory only).

    I have only used this in one project which used a dll written in C. From what I understand, the PInvoke code would be something along the lines of:
    Code:
    using System.Runtime.InteropServices;
    
    [DllImport("Data.dll")]
    public static extern bool CDataApp.IsOpen();
    As for wrapping, this would be a way to encapsulate the DLL methods into a class. Write your class and write the methods of your class so that they would call the methods in your DLL.

    In either case you will still need the above code to call your DLL.

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