1、 首先創(chuàng)建一個(gè)c#的類庫(kù)項(xiàng)目。
2、 將AssemblyInfo.cs文件中的[assembly: ComVisible(false)]改為[assembly: ComVisible(true)],或者在項(xiàng)目屬性中“應(yīng)用程序”構(gòu)選上“使程序集com可見(jiàn)”復(fù)選框。
3、 在項(xiàng)目屬性中”生成”構(gòu)選上”為com interop注冊(cè)”復(fù)選框。
4、 參考以下代碼及MSDN中COM 類示例(C# 編程指南):
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Youtube
{
[Guid("FA1BEAD8-D5E2-4a70-ACDF-0A7FB4DA17FD")]
public interface IYoutubeService
{
void UploadFile();
}
[Guid("2BD4711C-CAC0-4df5-B167-447FCD00D55C"),
ClassInterface(ClassInterfaceType.AutoDispatch)]
public class YoutubeService : IYoutubeService
{
public void UploadFile()
{
Console.WriteLine("My Com Test!");
}
}
}
5、 編譯,這時(shí)候會(huì)生成*.dll文件和*.tlb文件
6、 創(chuàng)建一個(gè)vc++的控制臺(tái)項(xiàng)目,將*.tlb文件拷貝到vc++工程目錄下
7、 Vc++代碼如下:
#include "stdafx.h"
#import "Youtube.tlb"
#include
#include
#pragma comment(lib,"comsuppw.lib")
using namespace Youtube;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
try
{
IYoutubeServicePtr service( __uuidof( YoutubeService ) );
service->UploadFile();
}
catch (const _com_error& e)
{
}
CoUninitialize();
return 0;
}
8、編譯程序,并把之前的*.dll文件拷貝到和生成exe一起,運(yùn)行程序則測(cè)試ok!
2、 將AssemblyInfo.cs文件中的[assembly: ComVisible(false)]改為[assembly: ComVisible(true)],或者在項(xiàng)目屬性中“應(yīng)用程序”構(gòu)選上“使程序集com可見(jiàn)”復(fù)選框。
3、 在項(xiàng)目屬性中”生成”構(gòu)選上”為com interop注冊(cè)”復(fù)選框。
4、 參考以下代碼及MSDN中COM 類示例(C# 編程指南):
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Youtube
{
[Guid("FA1BEAD8-D5E2-4a70-ACDF-0A7FB4DA17FD")]
public interface IYoutubeService
{
void UploadFile();
}
[Guid("2BD4711C-CAC0-4df5-B167-447FCD00D55C"),
ClassInterface(ClassInterfaceType.AutoDispatch)]
public class YoutubeService : IYoutubeService
{
public void UploadFile()
{
Console.WriteLine("My Com Test!");
}
}
}
5、 編譯,這時(shí)候會(huì)生成*.dll文件和*.tlb文件
6、 創(chuàng)建一個(gè)vc++的控制臺(tái)項(xiàng)目,將*.tlb文件拷貝到vc++工程目錄下
7、 Vc++代碼如下:
#include "stdafx.h"
#import "Youtube.tlb"
#include
#include
#pragma comment(lib,"comsuppw.lib")
using namespace Youtube;
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
try
{
IYoutubeServicePtr service( __uuidof( YoutubeService ) );
service->UploadFile();
}
catch (const _com_error& e)
{
}
CoUninitialize();
return 0;
}
8、編譯程序,并把之前的*.dll文件拷貝到和生成exe一起,運(yùn)行程序則測(cè)試ok!
- 2009年9月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2009年3月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2008年9月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2008年4月全國(guó)計(jì)算機(jī)四級(jí)數(shù)據(jù)庫(kù)工程師考試真題及答案
- 2011年3月全國(guó)計(jì)算機(jī)二級(jí)Java考試真題及答案
- 2010年9月全國(guó)計(jì)算機(jī)二級(jí)Java考試真題及答案
- 查看計(jì)算機(jī)等級(jí)考試全部真題>>
- 2024年9月上海計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月全國(guó)計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月全國(guó)計(jì)算機(jī)一級(jí)考試成績(jī)查詢?nèi)肟冢ㄒ验_(kāi)
- 2025年3月全國(guó)計(jì)算機(jī)二級(jí)成績(jī)查詢時(shí)間及成績(jī)查詢
- 2025年3月全國(guó)計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及成績(jī)查詢
- 2024年9月江蘇計(jì)算機(jī)一級(jí)成績(jī)查詢時(shí)間及查分入口
- 2024年9月浙江省全國(guó)計(jì)算機(jī)等級(jí)考試(NCRE)
- 2024年9月浙江計(jì)算機(jī)二級(jí)考試準(zhǔn)考證打印入口(9