如何在VC ++中删除使用C#DLL的错误

时间:2012-10-08 14:33:15

标签: c# visual-studio-2010 visual-c++ dll dllimport

这是我的接口和名为DataAccess.cs的类

 using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Data.Common;
    using System.Linq;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using IISWebFrameworkDAAB.BusinessLayer;
    using IISWebFrameworkDAAB.DataAccessLayer;
    using Framework.Data;
    using System.Text;

    namespace IISWebFrameworkDAAB.DataAccessLayer
    {
       [Guid("76f20d76-b030-4487-a315-2d727dedd4ec")]
        public interface DataAccessInterface
        {
            #region "Methods"

     void InsertSampleTable(Byte varTinyInt, Int32 varInt, String varText, String varVarchar, Boolean varBit, DateTime varDateTime, Decimal varDecimal);
    };

   [Guid("c686d756-98a7-4048-b79c-b1ff9889730c")]
    public class DataAccess : DataAccessInterface
        {
    public void InsertSampleTable(Byte varTinyInt, Int32 varInt, String varText, String varVarchar, Boolean varBit, DateTime varDateTime, Decimal varDecimal)
            {
                m_procName = "Insert_SampleTable";
                m_dbCommand = m_db.GetStoredProcCommand(m_procName);
                m_db.AddInParameter(m_dbCommand, "_varTinyInt", DbType.Byte, varTinyInt);
                m_db.AddInParameter(m_dbCommand, "_varInt", DbType.Int32, varInt);
                m_db.AddInParameter(m_dbCommand, "_varText", DbType.String, varText);
                m_db.AddInParameter(m_dbCommand, "_varVarchar", DbType.String, varVarchar);
                m_db.AddInParameter(m_dbCommand, "_varBit", DbType.Boolean, varBit);
                m_db.AddInParameter(m_dbCommand, "_varDateTime", DbType.DateTime, varDateTime);
                m_db.AddInParameter(m_dbCommand, "_varDecimal", DbType.Decimal, varDecimal);
                m_db.ExecuteNonQuery(m_dbCommand);
            }
    };

    }

这是我的AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 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("IISWebFrameworkDAAB")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("IISWebFrameworkDAAB")]
[assembly: AssemblyCopyright("Copyright ©  2012")]
[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(“76f20d76-b030-4487-a315-2d727dedd4ec”)]

// 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")]

这是矿工客户: -

#include <windows.h>
#include <stdio.h>

#pragma warning (disable: 4278)

// To use managed-code servers like the C# server, 
// we have to import the common language runtime:
#import <mscorlib.tlb> //raw_interfaces_only

#import "C:\Documents and Settings\Administrator\Desktop\new\NiproDAAB\IISWebFrameworkDAAB\bin\Debug\IISWebFrameworkDAAB.tlb"
using namespace IISWebFrameworkDAAB;
int main(int argc, char* argv[])
{
    DataAccessInterface *cpi = NULL;
     int retval = 1;
     CoInitialize(NULL);
   HRESULT hr = CoCreateInstance(CLSID_DataAccess,
               NULL, CLSCTX_INPROC_SERVER,
               IID_DataAccessInterface, reinterpret_cast<void**>(&cpi));

   if (FAILED(hr))
   {
      printf("Couldn't create the instance!... 0x%x\n", hr);
   }
}

当我运行客户端时,我收到以下错误: -

error C2065: 'CLSID_DataAccess' : undeclared identifier
1>c:\documents and settings\administrator\my documents\visual studio 2010\projects\connectdll\connectdll\cli.cpp(19): error C2065: 'IID_DataAccessInterface' : undeclared identifier

我按照以下链接http://msdn.microsoft.com/en-us/library/aa645738%28v=vs.71%29.aspx

给出了

1 个答案:

答案 0 :(得分:1)

尝试将“named_guids”添加到#imports,例如

'#import named_guids'

此步骤在您引用的示例中的COMClient.cpp源中说明,如下所示:

..    
#import "CSharpServer.tlb" no_namespace named_guids
..