CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    Monte Carlo
    Posts
    129

    [RESOLVED] Burning in DLL Hell

    I have a very simple C# dll with 1 function called "ReturnSomething". All this function does is return a number.

    I want to use this dll, without adding a reference to it in the source code. (There is a reason for this I will explain later.)

    So, in my application I add the following:

    Code:
            [DllImport("MyDll.dll", EntryPoint="ReturnSomething")]
            public static extern int ReturnSomething();
    In in my method, I try:

    Code:
    int xyz = ReturnSomething();
    Only to get this error:
    Can't find an Entry Point 'ReturnSomething' in a PInvoke DLL 'MyDll.dll'.

    The platform I'm currently on is Windows Mobile 6.5. Please keep in mind that when I add a reference to the DLL and use it normally, everything works fine.

    The reason that I don't want to add a reference is that I need to use a C++ dll compiled for the ARM v6 processor and MS will not let me add that as a reference. (If there is a way to do this I would appreciate hearing it.)

    What do I need to do in order to use a DLL in this manner?

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Burning in DLL Hell

    C++ mangles the names of functions when they're compiled. If you want to call into the dll like that you will need to declare the functions like so:

    Code:
    extern "C" __declspec(dllexport) void Foo();
    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    As an aside, you can use dumpbin.exe to view the exports of a library.
    Last edited by BigEd781; February 16th, 2011 at 12:41 PM.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: Burning in DLL Hell

    DllImport (a.k.a. P/Invoke) can *only* be used with native libraries. i.e. C/C++/obj-c/whatever. It cannot be used to invoke functions on normal .NET libraries.

    If you want to test the DllImport functionality you'll have to create a normal C library (or C++ library where you export the symbols with extern "C") and then place that in the same directory as your application. Also, as was said before, P/Invoking C++ directly is awkward as every C++ compiler modifies the names of the functions in a complicated way. The name you see in the source code is not what you see in the compiled code. The simplest thing to do to get around this is create a little C library which wraps the native C++ library and exposes normal C functions which can trivially be P/Invoked as they will not be renamed.
    Last edited by Mutant_Fruit; February 12th, 2011 at 07:45 AM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Aug 2000
    Posts
    48

    Re: Burning in DLL Hell

    This is how you do the function in DLL

    extern "C" _declspec(dllexport) int ReturnSomething(int x)

  5. #5
    Join Date
    Jun 2002
    Location
    Monte Carlo
    Posts
    129

    Re: Burning in DLL Hell

    Hi,

    Thanks for the replies everybody. Even though the phone states it has an ARM processor, once the DLL was compiled for thumb processor (and not ARM) everything worked.

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