我用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
现在已经引用成功,但是现在还有问题就是不知道怎么调用.
不知道谁有相关的经验能不能该一个简单的调用例子(最好能列举出详细步骤),不胜感激!
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
现在已经引用成功,但是现在还有问题就是不知道怎么调用.
不知道谁有相关的经验能不能该一个简单的调用例子(最好能列举出详细步骤),不胜感激!
解决方案 »
- 想用c# 做安装包. 同想法的请进!
- share 不同单位的计算
- 我用vs2005 + windows mobile 5.0 SDK 只能创建qqvga屏幕的应用程序,怎么才能创建QVGA屏的应用程序呢?
- 如何向WORD,excel,ppt窗口发送保存命令
- 我有个DepartReport.rpt报表文件,问怎么用代码来进行打印预览?
- 如何用C#实现电子磅秤上的数据的读取
- 请教一个照片匹配算法问题,分不够另开贴给
- HttpWebRequest问题
- CreateUserWizard 注册成功一个用户后,怎么不叫他自动登陆啊?
- 各位大侠帅哥,小女子求助如何在c# web应用程序中把服务器上的内容下载到客户端任一选定的路径下?????
- 多个字符数组,返回什么类型比较好?
- 用哪个浏览器控件可以实现提取当前网页HTML代码功能 顶者有分~
http://www.codeproject.com/csharp/com_object_in_c_.asp
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" );这个是我们的原代码,运行到调用的时候返回值不对,失败,怎么回事??
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" );
还是不对,各位大哥不知道谁能帮帮忙,急!
//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;
}
}
}
#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;
}