C++和C#编写调用COM组件
转自:http://www.maomao365.com/?p=6973
C++和C#编写调用COM组件
摘要:
在sql脚本编写中,如果需要在update
delete
中使用表别名的方法,必须按照一定的规则编写,否则将会出现相应的异常信息,如下所示:
实验环境:sqlserver 2008 R2
下载源代码新浦金娱乐场官网,
摘要:现在COM组件的使用越来越广泛,在各个程序中调用COM组件我想大家都遇到过,这篇文章就是关于COM组件的编写和调用的,主要包含了使用VC6.0编写和调用COM组件,VS2005中使用C#编写和调用COM组件,以及在VC6.0和VS2005之间互相调用COM组件。
关键字:VC6.0编写调用COM VS2005中C#编写和调用COM VC6.0和VS2005之间互相调用COM
---update 表别名的写法
update [别名] set [别名].[字段] =[字段值] from [表名] as [表别名] where [条件]
---delete 表别名的写法
delete [别名] set [别名].[字段] =[字段值] from [表名] as [表别名] where [条件]
正文:
前一阵在工作中做项目的时候,遇到了COM组件的调用和使用问题,当时研究和好一阵,才把中间的环节打通,现在写出来为大家提供方便,这里包含了四个类型:
1、在VS2005中,C#编写DLL并使用C++调用
2、在VS2005中C#编写的COM组件,使用VC6.0调用
3、在VC6.0中编写COM组件,使用VS2005 C#调用
4、在VC6.0中编写COM组件,使用VC6.0调用
其中每个类型都写了两个程序,一个为COM组件程序,一个为调用程序
程序实现:
1、在VS2005中,C#编写DLL并使用C++调用
(1)C#编写DLL程序
建立C#编写的DLL程序AddDll,项目类型为:类库
程序代码:
using System;using System.Collections.Generic;using System.Text;namespace AddDll{ public class Add { public int iadd(int a, int b) { int c = a + b; return c; } }}
(2)C++编写调用程序
建立C++的Win32控制台应用程序UseDll,项目类型为:Win32控制台应用程序
配置:右键点击解决方案资源管理器中的UseDll,选择“属性”,将公共语言运行库支持设置为“公共语言运行库支持(/clr)”
图一 公共语言运行库设置
程序代码:
#include "stdafx.h"#include "stdio.h"#using "..debugAddDll.dll"using namespace AddDll;int _tmain(int argc, _TCHAR* argv[]){ int result; Add ^add = gcnew Add(); result = add->iadd(10,90); printf("%d",result); scanf("%s"); return 0;}
2、在VS2005中C#编写的COM组件,使用VC6.0调用
(1)VS2005中使用C#编写COM组件
建立C#编写的COM组件,项目类型为类库
配置:右键点击解决方案资源管理器中的AddCom,选择“属性”,选择“生成”,选择“为COM Interop注册(_P)”
打开AssemblyInfo.cs文件,设置[assembly: ComVisible(true)]
这用就可以生成AddCom.tlb文件
图二 COM生成设置
程序代码:
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace AddCom{ //可以通过//菜单的 “工具/guid生成”。 //注意要选择Define Guid{….}格式,并全//部保存下来,保存到哪都行,记事本呀什么的。 //因为在做VC程序/////////的时候要用到的。 [Guid("298D881C-E2A3-4638-B872-73EADE25511C")] public interface AddComInterface { [DispId(1)] int iadd(int a, int b); [DispId(2)] float ladd(float a, float b); } [Guid("2C5B7580-4038-4d90-BABD-8B83FCE5A467")] [ClassInterface(ClassInterfaceType.None)] public class AddComService : AddComInterface { public AddComService() { } public int iadd(int a, int b) { int c = 0; c = a + b; return c; } public float ladd(float a, float b) { float c = 0; c = a + b; return c; } }}
(2)VC6.0编写调用程序
使用VC6.0编写建立MFC应用程序UseCom,项目类型为MFC AppWizard(exe)
在stdafx.h添加: