我有一個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();
}
在'#import'路徑有14位,後'FaceTrackingBasics-',在這些空間應該在那裏,他們在你的原始代碼? – 2013-03-09 16:22:47
你有沒有修改你像[ComVisible(true)] assambly? – Thanatos 2013-03-09 16:39:31
斯科特,它的一個副本粘貼錯誤,在我的代碼中它的所有在一起,沒有空格。我不知道你的意思,但我有我的代碼 – virgula24 2013-03-09 19:46:31