Search code examples
dllimportvoid-pointersintptrwindows-phone-8

How can I get a COM component which can be used on Windows phone 8 project with C#


Recently,I started working on Windows phone developing.

I got a project which was written in C language,in my windows phone 8 Solution. I created a Dynamic link library(DLL) project and it can be built successfully. In it,I use a runtime component project to provide the DLL’s method to the UI(C#) project. In method,I have some pointer parameters,I use Intptr object to pass a byte[] object to the runtime component project. Unfortunately, this caused the program to crash.

How can I pass a pointer parameter to the runtime component project or DLL project? Perhaps,there is another way which can use [Dllimort] method to import my DLL file?

Any help is appreciated!

PS: My solution's structure:

  1. [Dynamic link library project(C++)].dll_method(void *para);

  2. [Runtime component project(C++)].rc_method(Intptr p)

    {
       dll_method((void*)p);
    }
    
  3. [Windows phone 8 project(C#)].method()

    {
       GCHandle g = GCHandle.Alloc(byte[], GCHandleType.Pinned);
       IntPtr p = g.AddrOfPinnedObject();
       rc_method(p);
    }
    

Solution

  • Unless you're doing remote-process communication, to call a Windows Phone Runtime Component (C++) from a Windows Phone App Project (C#), all you need to do is add the native project as a reference.

    You can pass any WinRT supported type over as arguments. And as for callbacks, you can also use events (if in the same process), or pass in callback types (for RPC).