使用COM接口C#从c ++访问会产生错误

时间:2013-03-09 15:52:49

标签: c# c++ com interop

我有一个c ++项目,它将调用一个C#函数,之后,c#将调用一个c ++函数(两者都带有返回值)。我停留在COM接口,有人在这上面stackoverflow给了我(我没有太多经验的c ++或c#,只有java),但由于某种原因它给我一个错误,当我尝试编译C ++代码,致命错误C1196 :'FaceTrackingBasics-WPF':在类型库c中找到的标识符:\ FaceTrackingBasics-WPF / bin / x86 / Debug / FaceTrackingBasics-WPF.tlb'不是有效的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();
 }

1 个答案:

答案 0 :(得分:0)

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

不是,C ++标识符不能包含短划线“ - ”。就像C#btw一样。此名称来自您的项目名称。其中设置了类型库名称。其中设置了#import指令创建的命名空间的名称。

除了简单地重命名项目之外,还有一些可行的解决方法。你可以apply attributes到#import指令。像“auto-rename”,“no-namespace”和“rename-namespace”:

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