How to use C++ dll from C#

I’m trying include c++ library (DLL) in my c# project but every time I do that I get following error message in VS2012

A reference to 'C:UsersHiepDangDesktopmydll.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

When I’m trying add Com+ Component, I get following error message in window

One or more files do not contain components or type libraries. These files cannot be installed.

Error 80110425 occurred.

An unknown error occured. You should check all documentation for a solution. If no further information is available, please contract technical support

I’m following thread here and here but my problem not solved.

In C++, I can use dll like:

    extern "C" __declspec( dllimport ) int mymethod(const char *key, char *data, size_t buflen);

    //Code test
    const char* key = "test";

    char* data = new char[1024];
    int buflen = 1024;
    int result = mymethod(key,data, buflen);

In C#, I use dll as:

    //define dll
    [DllImport("mydll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern int mymethod([MarshalAs(UnmanagedType.LPTStr)]string key, [MarshalAs(UnmanagedType.LPTStr)]string data, uint buflen);

    //method test
    private static int testdll() 
        string key = "123456789";
        string buf = string.Empty;
        mymethod(key, buf, 1024);
        return 0;

Can you tell me any solutions to solve it.

P.s: My English is not good. I’m sorry if something inconvenient

Edited: I explain the variable in method in dll. “key” as string input has 8-13 characters, mymethod will be encrypted to generate “buf”. I need variable of “buf”.

Source: dll

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.