0

I have to replace a DLL in an old project. This project can't be changed (downward compatibility, other producer etc) and has the following Methodsignature:

typedef UINT(*CALLBACK Initialise)();
Initialise fp_initialise;

typedef UINT(*CALLBACK ReadPosition)(int *ptr_ncolours, float *ptr_pderrs, float *ptr_cderrs);
ReadPosition fp_readposition;

and so on... where CALLBACK is __stdcall

My C# DLL Interface:

[Guid("00005D2D-ABCD-3214-5694-A4EC0005B4D9")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    public interface IMainInterface
    {
        int Initialise();
        unsafe int ReadPosition(ref int ptr_ncolours, float* ptr_pderrs, float* ptr_cderrs);

//...
}

and the Class where I use the Interface:

    [ClassInterface(ClassInterfaceType.None)]
    [Guid("04991247-ABCD-ABCD-A4B8-3533D2F264D0")]
    [ComVisible(true)]
    [ProgId("MyDLL.Main")]
    public class Main : IMainInterface
    {
//...
}

The call of Initialize (it has no parameters) in my new C# dll works, but any other methodcalls crashs without any information.

How I have to write those Methods in my C# dll, when I call it from a C++ application, when I replace the old dll with my new C# dll?

Mitja
  • 863
  • 5
  • 22

1 Answers1

2

Rather than expose your functionality through COM, you can use the Reverse P/Invoke technique outlined here.

This requires a slight change to the calling code. If you are unable to make any changes to the original project binary, you can create a small native DLL with the same name as the DLL you are replacing, to act as a bridge.

Expose all your native functions in this bridge DLL, and use reverse P/Invoke to call your managed DLL (whose name will of course need to change to something different.)

bright
  • 4,700
  • 1
  • 34
  • 59