Frage

Now I have several C++ DLLs and their source code. These are managed DLLs and I can directly add them to c# project as reference. The issue is Now I want to use them in a windows phone application (c#).

I don't know how to achieve this, please help

I know merely nothing about these C++ projects, and they contains a lot of header files. Do I have to rewrite these codes to fit for the windows phone? That will be a sad story..

Or I just need to recompile them as different type of projects?

Thanks for your patient

War es hilfreich?

Lösung

Not sure how Windows Phone C# does it, but normal C# can:

a) Load symbols directly from C library, for example:

using System;
using System.Runtime.InteropServices;
static class win32
{
    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

    [DllImport("kernel32.dll")]
    public static extern bool FreeLibrary(IntPtr hModule);   
}

(this is taken from: http://www.andyrushton.co.uk/csharp-dynamic-loading-of-a-c-dll-at-run-time/ after brief googling)

You need to export the C++ interface methods as "C" for that, e.g.:

extern "C" __declspec( dllexport ) int MyFunc(long parm1);

(from MSDN: http://msdn.microsoft.com/en-us/library/wf2w9f6x.aspx)

b) Use a wrapper in C++/CLI to connect unmanaged C++ to managed C#:

here's a good example: http://www.codeproject.com/Articles/19354/Quick-C-CLI-Learn-C-CLI-in-less-than-10-minutes

Do note that the syntax is somewhat weird at first look, and not eveything can be used. However, what you gain is ability to use the C++ classes - something that exporting "C" prohibits you from doing.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top