2013-03-13 29 views
0

我剛剛開始從C#編寫託管dll。我正在嘗試從msdn這example。該示例演示了我們如何通過本機C++調用簡單方法。然而,我擴展了這個例子並添加了一個名爲CheckCar的簡單方法。我的問題是如何將我使用CheckCar方法在C++從Native C++使用受管dll中的對象

這是C#代碼,我有

public class car 
{ 
    public string CarMake { get; set; } 
    public int CarModel { get; set; } 
} 

public interface ICalculator 
{ 
    int Add(int Number1, int Number2); 
    int Multiply(int a, int b); 
    car CheckCar(car c); 
}; 

public class ManagedClass : ICalculator 
{ 
    public int Add(int Number1, int Number2) 
    { 
     return Number1 + Number2; 
    } 

    public int Multiply(int a, int b) 
    { 
     return a * b; 
    } 
    public car CheckCar(car c) 
    { 
     if(c.CarMake.Equals("honda")) 
     { 
      car _c = new car(); 
      _c.CarMake = "Honda"; 
      _c.CarModel = 1232121; 
      return _c; 
     } 
     else 
     { 
      return null; 
     } 
    } 
} 

這是C++代碼

// Initialize COM. 
    HRESULT hr = CoInitialize(NULL); 

    // Create the interface pointer. 
    ICalculatorPtr pICalc(__uuidof(ManagedClass)); 

    long lResult = 0; 

    // Call the Add method. 
    pICalc->Add(5, 10, &lResult); 
    wprintf(L"The result is %d", lResult); 

    // Uninitialize COM. 
    CoUninitialize(); 

這是我的AssemblyInfo看起來像

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information 
// associated with an assembly. 
[assembly: AssemblyTitle("sManagedDLL")] 
[assembly: AssemblyDescription("")] 
[assembly: AssemblyConfiguration("")] 
[assembly: AssemblyCompany("")] 
[assembly: AssemblyProduct("sManagedDLL")] 
[assembly: AssemblyCopyright("Copyright © 2013")] 
[assembly: AssemblyTrademark("")] 
[assembly: AssemblyCulture("")] 

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type. 
//[assembly: ComVisible(false)] 
[assembly: ComVisible(true)] 
[assembly: AssemblyDelaySign(false)] 
[assembly: AssemblyKeyFile("..\\..\\..\\MyKeyFile.SNK")] 

// The following GUID is for the ID of the typelib if this project is exposed to COM 
[assembly: Guid("41fc209d-a359-45d7-bf05-b1d93690824d")] 

// Version information for an assembly consists of the following four values: 
// 
//  Major Version 
//  Minor Version 
//  Build Number 
//  Revision 
// 
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below: 
// [assembly: AssemblyVersion("1.0.*")] 
[assembly: AssemblyVersion("1.0.0.0")] 
[assembly: AssemblyFileVersion("1.0.0.0")] 

我的問題是如何在C++中調用car CheckCar(car c);方法。我如何訪問C++中的對象車及其屬性?

更新:

更新我的代碼無法訪問IcarPtr在我的C++

這是我更新的C#代碼

namespace sManagedDLL 
{ 
    [ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")] 
    public interface Icar 
    { 
     string GetCarMake(); 
     void SetCarMake(string rx); 

     int GetCarModel(); 
     void SetCarModel(int rx); 
    } 

    [ComVisible(true), ClassInterface(ClassInterfaceType.None), Guid("13FE32AD-4BF8-495f-AB4D-6C61BD463EA5")] 
    public class CarImpl : Icar 
    { 
     private string carmake; 
     private int carmodel; 

     public string GetCarMake(){return carmake;} 
     public void SetCarMake(string rx) { this.carmake = rx;} 

     public int GetCarModel() {return carmodel ;} 
     public void SetCarModel(int rx){this.carmodel = rx;} 
    } 

    public interface ICalculator 
    { 
     int Add(int Number1, int Number2); 
     int Multiply(int a, int b); 
     Icar CheckCar(CarImpl c); 
    } 

    public class ManagedClass : ICalculator 
    { 
     public int Add(int Number1, int Number2) 
     { 
      return Number1 + Number2; 
     } 

     public int Multiply(int a, int b) 
     { 
      return a * b; 
     } 

     public Icar CheckCar(CarImpl c) 
     { 
      if (c.GetCarModel().Equals("honda")) 
      { 
       CarImpl _c = new CarImpl(); 
       _c.SetCarMake("Honda"); 
       _c.SetCarModel(1212132); 
       return _c; 
      } 
      else 
      { 
       return null; 
      } 
     }//end method 
    } 
} 
+0

**完全**與ManagedClass相同。通過以相同的方式進入成功之地,宣佈一個ICar界面。 – 2013-03-13 17:33:09

+0

這怎麼可能。我相信接口不能包含字段 – Rajeshwar 2013-03-13 18:05:35

+0

這是一件好事*,COM不支持字段。屬性不是問題。 – 2013-03-13 18:29:30

回答

2

您必須公開Car作爲COM對象(後與爲ManagedClassICalculator所做的相同),然後將其實例化並將其發送到CheckCar方法。

有一個類似的例子here,你可以使用它作爲指導(當然,它是更復雜的,然後你需要什麼,但服務器作爲插圖)。

+0

感謝您的回覆。我目前在AssemblyInfo.CS中擁有所有信息(如上所述),是否需要將這些信息移動到類聲明的頂部,如在您發佈的鏈接中。我試着在班車頂部使用'ComVisible(true)]',但它表示它無法將其解析爲類型。 – Rajeshwar 2013-03-13 17:32:47

+0

你應該在COM公開方法中使用接口而不是類。 .NET可以爲類生成接口,但這最終導致更令人困惑,IMO。請參閱http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM或http://stackoverflow.com/questions/1863128/c-sharp-exposing-to-com-interface-inheritance – 2013-03-13 17:47:57

+0

那麼這是否意味着我們無法將對象傳遞給這些對象?我的目標是在Native C++中使用C#託管的dll中的對象。接口不能包含字段。我在這裏有點困惑 – Rajeshwar 2013-03-13 18:07:17

相關問題