CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2003
    Posts
    7

    How do you use a "C" dll from within a C# app?

    Does anyone know how I can use a dll written in "C" from within a C# application? I have gotten as far as building a C# app and then importing the dll. However, when I call the function, I get an error saying that the dll cannot be loaded.:

    // sample code follows

    [DllImport("MyDll.dll")]
    public static extern int DllFunction();
    :
    :

    public static int Main()
    {
    DllFunction(); // I get a message that the dll cannot be loaded
    }

    Thanks for any help (and sample code)!!
    -Tom

  2. #2
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    plz. check that dll is in path !!!

    Paresh

  3. #3
    Join Date
    Mar 2003
    Posts
    7
    Yes, it's in the path..

  4. #4
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    does it have functions as exported !!! in .def file. which type of dll is that.

    is it a COM dll or a simple MFC/NONMFC dll.

  5. #5
    Join Date
    Mar 2003
    Posts
    7
    It's a non-MFC dll (since it was written in "C")..

  6. #6
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    i think it should have the function exported list, make a NON-MFC dll in VC++ which exposes the function and then compile it to dll.

    maybe pure C dll is conflicting one.

    Paresh

  7. #7
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    If DLL cannot be loaded then dll is not a valid DLL or it is not in current directory or any directory listed in path env. variable...

    Try to write whole path to the dll file in DllImport attribute.
    If the prog fails again then try to make C/C++ prog and use LoadLibrary API function directly to load that DLL.

    If the LoadLibrary loads the DLL successfully, post it here. We will look for problem again.

    If the LoadLibrary fails then post here the error code returned by GetLastError() function call...

    martin

  8. #8
    Join Date
    Mar 2003
    Posts
    7
    Thanks, everyone.
    I'll let you know what I find out.
    Tom

  9. #9
    Join Date
    Nov 2002
    Location
    Singapore
    Posts
    1,890
    sure will wait for your answer.

    thankx
    Paresh

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