2013-03-09 58 views
0

我有一個C++項目,將調用一個C#函數,然後,C#將調用一個C++函數(都帶有返回值)。我堅持在COM接口,有人在這裏在stackoverflow給我(我沒有太多經驗的C + +或C#,只有Java),但由於某種原因,它給了我一個錯誤,當我嘗試編譯C++代碼,致命錯誤C1196 :'FaceTrackingBasics-WPF':在類型庫中找到的標識符c:\ FaceTrackingBasics-WPF/bin/x86/Debug/FaceTrackingBasics-WPF.tlb'不是有效的C++標識符。使用COM接口C#從C++訪問會產生錯誤

你能發現一些錯誤嗎?也許我不應該在一個文件中有兩個類?順便說一句,C#代碼與窗口相關聯,我不知道它是否會導致問題。 由於

C#代碼

// --------------------------------------------------------------------------------    ------------------------------------ 
    // <copyright file="FaceTrackingViewer.xaml.cs" company="Microsoft"> 
    //  Copyright (c) Microsoft Corporation. All rights reserved. 
    // </copyright> 
    // --------------------------------------------------------------------------------  ------------------------------------ 

    namespace FaceTrackingBasics 
    { 
using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 
using Microsoft.Kinect; 
using Microsoft.Kinect.Toolkit.FaceTracking; 

using System.Globalization; 

///// 
using System.Runtime.InteropServices; 


using Point = System.Windows.Point; 

/// <summary> 
/// Class that uses the Face Tracking SDK to display a face mask for 
/// tracked skeletons 
/// </summary> 
public partial class FaceTrackingViewer : UserControl, IDisposable 
{ 
    public static readonly DependencyProperty KinectProperty = DependencyProperty.Register(
     "Kinect", 
     typeof(KinectSensor), 
     typeof(FaceTrackingViewer), 
     new PropertyMetadata(
      null, (o, args) => ((FaceTrackingViewer)o).OnSensorChanged((KinectSensor)args.OldValue, (KinectSensor)args.NewValue))); 

    private const uint MaxMissedFrames = 100; 



    private Boolean drawFlat = false; 

    public FaceTrackingViewer() 
    { 
     this.InitializeComponent(); 
    } 

    ~FaceTrackingViewer() 
    { 
     this.Dispose(false); 
    } 

    public KinectSensor Kinect 
    { 
     get 
     { 
      return (KinectSensor)this.GetValue(KinectProperty); 
     } 

     set 
     { 
      this.SetValue(KinectProperty, value); 
     } 
    } 

    public void Dispose() 
    { 
     this.Dispose(true); 
     GC.SuppressFinalize(this); 
    } 


    //i deleted the rest of the code, i dont think its relevant 
    } 
} 

//***********************// 

[ComVisible(true)] 
[Guid("2EF06BCB-A25B-41AD-B233-33A956DBEB69")] 
public struct Ponto 
{ 
    public double x; 
    public double y; 

    public Ponto(double x, double y) 
    { 
     this.x = x; 
     this.y = y; 
    } 
} 

[ComVisible(true)] 
[Guid("EB9258F5-DCFB-4F91-8342-5A05EB17557D")] 
public interface IManagedClass 
{ 
    Ponto[] Foo(); 
} 

[ComVisible(true)] 
[Guid("11B23AD7-F79E-45D7-BC87-89F0DBC8B83F")] 
[ClassInterface(ClassInterfaceType.None)] 
public class ManagedClass : IManagedClass 
{ 
    private List<Ponto> points; 

    public ManagedClass() 
    { 
     points = new List<Ponto>(); 
     points.Add(new Ponto(1.0, 1.0)); 
     points.Add(new Ponto(2.0, 2.0)); 
     points.Add(new Ponto(3.0, 3.0)); 
    } 

    public Ponto[] Foo() 
    { 
     return points.ToArray(); 
    } 
} 

C++代碼

#include "stdafx.h" 

    #import  "C:/Users/Pedro/Dropbox/0000000TESE/000_visual_studio/0FaceTMS/FaceTrackingBasics-    WPF/bin/x86/Debug/FaceTrackingBasics-WPF.tlb" 
    #include <iostream> 

    using namespace FaceTrackingBasics; 
    using namespace std; 

    int main() 
    { 
::CoInitialize(NULL); 

{ 
    IManagedClassPtr pManagedClass(__uuidof(ManagedClass)); 

    SAFEARRAY* psa = pManagedClass->Foo(); 
    Ponto* pPoints = (Ponto*)psa->pvData; 

    for (int i = 0; i < 3; ++i) 
     cout << pPoints[i].x << " " << pPoints[i].y << endl; 
} 

::CoUninitialize(); 
} 
+0

在'#import'路徑有14位,後'FaceTrackingBasics-',在這些空間應該在那裏,他們在你的原始代碼? – 2013-03-09 16:22:47

+0

你有沒有修改你像[ComVisible(true)] assambly? – Thanatos 2013-03-09 16:39:31

+0

斯科特,它的一個副本粘貼錯誤,在我的代碼中它的所有在一起,沒有空格。我不知道你的意思,但我有我的代碼 – virgula24 2013-03-09 19:46:31

回答

0
'FaceTrackingBasics-WPF' : ... is not a valid C++ identifier 

它不是,一個C++標識符不能含有破折號 「 - 」。就像C#順便說一句。這個名字來自你的項目名稱。其中設置了類型庫名稱。其中設置了#import指令創建的名稱空間的名稱。

除了簡單地重命名項目之外,還有幾種可能的解決方法。 #import指令可以將apply attributes。像「自動重命名」,「沒有命名空間」和「重命名名稱空間」:

#import "FaceTrackingBasics-WPF.tlb" rename_namespace("managed") 
    using namespace managed; 
+0

'指令'謝謝!我做了你所說的,它現在可以構建解決方案,但visual studio仍然給我紅色的「使用名稱空間管理」,並在我的所有主要類的c + +代碼。有什麼想法嗎? – virgula24 2013-03-09 20:56:38

+0

我在想#include 。它解決了,感謝所有的幫助! – virgula24 2013-03-09 20:58:37