none
How to load a unmanaged c++ dll from c# project

    Question

  • hi,
    i have create a simple c++ dll,then i try to load it through this way:Right click Reference -->Add reference
    Seems this method can't work, i am fresh about c#, i will appreciate for any suggesiton about fix this

    Thanks
    Thursday, February 21, 2008 8:31 AM

Answers

  • Hi Terax,

     

    if your DLL is unmanaged, you cannot add a reference to it. You have to use p/invoke to invoke your DLL:

     

    Code Snippet

    public classFoo

    {

    [DllImport("myunmanaged.dll", CharSet = CharSet.Ansi)]

    private extern static int UnmanagedFunction(int type, int dest);

    }

     

     

    Foreach function in your unmanaged dll, that you want to use from C#, you have to define a extern static method, and decorate it with the "DllImport" attribute.
    DllImport attribute allows you to specify in which native DLL is the method (and some other parameters).

     

    You have to do the translation between unmanaged C++ types (like char*) to managed types (like string): the types specified in the C# declaration of the UnmanagedFunction are managed types.

     

    This page can help you about type conversions between unmanaged and managed:

    http://msdn2.microsoft.com/en-us/library/aa446538.aspx

     

    And this other can help you about more options of DllImport attribute and p/invoke in general:

    http://msdn2.microsoft.com/en-us/library/aa288468.aspx

     

    Hope this helps ;-)

    edu

    Thursday, February 21, 2008 9:04 AM

All replies

  • Hi Terax,

     

    if your DLL is unmanaged, you cannot add a reference to it. You have to use p/invoke to invoke your DLL:

     

    Code Snippet

    public classFoo

    {

    [DllImport("myunmanaged.dll", CharSet = CharSet.Ansi)]

    private extern static int UnmanagedFunction(int type, int dest);

    }

     

     

    Foreach function in your unmanaged dll, that you want to use from C#, you have to define a extern static method, and decorate it with the "DllImport" attribute.
    DllImport attribute allows you to specify in which native DLL is the method (and some other parameters).

     

    You have to do the translation between unmanaged C++ types (like char*) to managed types (like string): the types specified in the C# declaration of the UnmanagedFunction are managed types.

     

    This page can help you about type conversions between unmanaged and managed:

    http://msdn2.microsoft.com/en-us/library/aa446538.aspx

     

    And this other can help you about more options of DllImport attribute and p/invoke in general:

    http://msdn2.microsoft.com/en-us/library/aa288468.aspx

     

    Hope this helps ;-)

    edu

    Thursday, February 21, 2008 9:04 AM
  • edu, i have to say you are a good man, thanks for your help for thounds times.
    Thursday, February 21, 2008 9:10 AM
  • Hi Edu,

    Thanks for the nice answer. However I have questions:

    Can't we use   tlbimp.exe tool to convert native (unmanaged) c++ dll   to managed dll?

    If not, can you please explain how to create Runtime callable wrapper (RCW) for the dll generated in Native win32 c++?

    I understand that we can directly add a dll created in managed C++. Then What is so called C++/CLI wrapper class?How to create C++/CLI wrapper class? Where to use it?
    Please explain this last point with example. Its urgent for me.

    Thanks in advance.


    Aditya N B
    Friday, May 13, 2011 7:44 PM
  • tlbimp only works for COM enabled type libraries. Is your C++ dll COM?
    Friday, May 13, 2011 7:48 PM
  • Hi Ekoostik,

    I am just practicing for illustration purpose.

      No. My dll is Native win32 Type. I created it in vc++ 2008 >Win32> dll.

    If I create CLR DLL it works fine by adding reference as I can understand it has managed code.

    But when I create Unmanaged vc++ DLL, then it won't let me add the reference which is obvious.

    So how to create Wrapper to this unmanaged DLL so that it can be consumed by managed code? I don't want to use PInvoke/dllimport.

    Is there any method to generate C++/CLI Wrapper ?

    Here I do not understand why at all is it called  C++/CLI wrapper? Because C++/CLI can be directly added as reference. Why is't it called native c++ wrapper?
    Please help.

    Its urgent.

     


    Aditya N B
    Saturday, May 14, 2011 1:58 AM
  • To be simple and clear,

    My question is,

    - What is C++/CLI wrapper

    - How to create C++/CLI wrapper.(The way we create CCW and RCW)

    Thanks in advance.


    Aditya N B
    Saturday, May 14, 2011 2:05 AM