我用C#编写了一个Com+组件原代码如下:
using System;
using System.Reflection;
using System.EnterpriseServices;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;[assembly: ApplicationName("ComTest")]
[assembly: AssemblyKeyFileAttribute(@"ComTest.snk")]
[assembly: AssemblyKeyName("ComTest.snk")]
[assembly: ApplicationActivation(ActivationOption.Server)]namespace ComTest
{
[Guid("948A3600-3327-4457-B4E5-6CE043136625")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IComTest
{
int Output();
} public class ComTest : ServicedComponent,IComTest
{
public ComTest()
{
} public int Output()
{
return 1;
} }
}
然后激活安装在组件服务内.
用asp测试借口正确无误,代码如下:
<%
dim obj
set obj = server.CreateObject("ComTest.ComTest")
Response.Write obj.Output()
%>
现在问题是,这个接口提供给Vc++调用无效.
首先遇上的第一个问题,如何引用?
原先的调用办法是:
#import "C:\\bin\\ComTest.dll" no_namespace
但是提示加载类型错误
经过发贴别人建议修改如下:#import "c:\\bin\\ComTest.tlb" raw_interfaces_only
现在已经引用成功,但是现在还有问题就是不知道怎么调用.
不知道谁有相关的经验能不能该一个简单的调用例子(最好能列举出详细步骤),不胜感激!

解决方案 »

  1.   

    Build com object for vc
    http://www.codeproject.com/csharp/com_object_in_c_.asp
      

  2.   

    DBCOM_InterfacePtr spDisp( _T("ComTest.ComTest") );
    BSTR vt; BSTR s1=(unsigned short *)malloc(10);
    memset(s1,0,10);
    memcpy(s1,"scott",5);

    BSTR s2=(unsigned short *)malloc(10);
    memset(s2,0,10);
    memcpy(s2,"tiger",5);

    HRESULT hr;
    hr=spDisp->Init(s1,s2);
    if (FAILED(hr))
    AfxMessageBox( "error" );这个是我们的原代码,运行到调用的时候返回值不对,失败,怎么回事??
      

  3.   

    =.=,不好意思,我已经改了com+接口,按照上面的例子http://www.codeproject.com/csharp/com_object_in_c_.asp现在情况如下:原码如下:
    using System;
    using System.Reflection;
    using System.EnterpriseServices;
    using System.Runtime.InteropServices;
    using System.Runtime.CompilerServices;[assembly: ApplicationName("ComTest")]
    [assembly: AssemblyKeyFileAttribute(@"ComTest.snk")]
    [assembly: AssemblyKeyName("ComTest.snk")]
    [assembly: ApplicationActivation(ActivationOption.Server)]namespace ComTest
    {
    [Guid("948A3600-3327-4457-B4E5-6CE043136625")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface DBCOM_Interface
    {
    [DispId(1)]
    void Init(string userid , string password);
    [DispId(2)]
    bool ExecuteSelectCommand(string selCommand);
    [DispId(3)]
    bool NextRow();
    [DispId(4)]
    void ExecuteNonSelectCommand(string insCommand);
    [DispId(5)]
    string GetColumnData(int pos);
    } [Guid("47C976E0-C208-4740-AC42-41212D3C34F0"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface DBCOM_Events 
    {
    } [Guid("5F1F52B7-DBE5-4004-B92C-D2EC967D471A")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(DBCOM_Events))]
    public class ComTest : ServicedComponent,DBCOM_Interface
    {
    public ComTest()
    {
    } public void Init(string userid , string password)
    {
    } public bool ExecuteSelectCommand(string selCommand)
    {
    return true;
    }
            
    public bool NextRow()
    {
    return true;
    } public string GetColumnData(int pos)
    {
    return "Hello";
    } public void ExecuteNonSelectCommand(string insCommand)
    {
    }
    }
    }
    下面是用Vc++的调用:DBCOM_InterfacePtr spDisp( _T("ComTest.ComTest") );
    BSTR vt; BSTR s1=(unsigned short *)malloc(10);
    memset(s1,0,10);
    memcpy(s1,"scott",5);

    BSTR s2=(unsigned short *)malloc(10);
    memset(s2,0,10);
    memcpy(s2,"tiger",5);

    HRESULT hr;
    hr=spDisp->Init(s1,s2);
    if (FAILED(hr))
    AfxMessageBox( "error" );
    还是不对,各位大哥不知道谁能帮帮忙,急!
      

  4.   

    n我是写C#的,对Vc++有不熟悉,所以希望大家能帮我看看我写的代码有什么问题,或者给我一个Vc++的用例给我!
      

  5.   

    大致代码如下
    //c#
    using System;
    using System.Runtime.InteropServices;namespace ComTest
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    [Guid("694C1820-04B6-4988-928F-FD858B95C880")]
    public interface IComTest
    {
    [DispId(1)]
    string Show(string p_strMsg);
    } [Guid("47C976E0-C208-4740-AC42-41212D3C34F0"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IComEvents
    {
    } [Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(IComEvents))]
    public class ComTest : IComTest
    {
    public ComTest()
    {
    } public string Show(string p_strMsg)
    {
    return p_strMsg;
    }
    }
    }
      

  6.   

    //VC call
    #include "stdafx.h"
    #import "ComTest.tlb"int _tmain(int argc, _TCHAR* argv[])
    {
    CoInitialize(NULL);
    ComTest::IComTestPtr p(__uuidof(ComTest::ComTest)); char arg[30];
    lstrcpy(arg, "hello world");
    _bstr_t ret = p->Show(arg);
    printf( ret ); CoUninitialize(); return 0;
    }