2014-08-29 83 views
0

我必須替換舊項目中的DLL。該項目不能被改變(向下兼容,其他生產商等),並具有以下Methodsignature:在C++應用程序中使用C#dll

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

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

等等... 其中CALLBACK__stdcall

我的C#DLL接口:

[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); 

//... 
} 

及類別,我使用接口:

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

在我的新C#DLL中調用Initialize(它沒有參數),但任何其他方法調用都會在沒有任何信息的情況下崩潰。

當我從C++應用程序中調用它時,如何使用我的新C#dll替換舊的dll時,如何在C#dll中編寫這些方法?

回答

2

您可以使用反向P/Invoke技術概述here,而不是通過COM公開您的功能。

這需要對調用代碼稍作修改。如果您無法對原始項目二進制文件進行任何更改,則可以創建一個與正在替換的DLL具有相同名稱的小型本機DLL,以充當橋接器。

暴露你的所有的原生功能,在此橋DLL,並使用反向的P/Invoke調用託管DLL(他的名字當然需要改變,以不同的東西。)

相關問題