1. 创建classlibrary

2.编写一个COM接口和一个COM类

[Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]

public interface IManagedInterface

{             int PrintHi(string name);         }

[Guid("C6659361-1625-4746-931C-36014B146679")]

public class InterfaceImplementation : IManagedInterface

{             public int PrintHi(string name)             {                 Console.WriteLine("Hello, {0}!", name);                 return 33;             }         }

3.在PROJECT上点击右键,选择属性---》bulid,选择registry from com interep.

4.一定要以管理员的方式运行 VS

5.在BIN里面有dll。可以不用 CSC 那种命令行了,happy.

6.怎么在C#中引用 :

添加引用

using System.Runtime.InteropServices;

using ClassLibrary2;

[DllImport("ClassLibrary2.dll", SetLastError = true)]

public static extern int DoWork(bool flags);

ClassLibrary2.Class1 c1 = new Class1();

B1.Content =c1.DoWork(true);

7. 如果是要发布给c++,那就对不起了,还有好多步骤。

7.1请添加以下属性

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("6EEF31AA-4D8B-4BE6-9254-F9C971E80BAB")]
    public interface IF1

{
        [DispId(1)]
        int DoWork(bool user);
      
    }

[ProgId("ClassLibrary2.DotNet")]
   [ClassInterface(ClassInterfaceType.None)]
   [Guid("51B24524-2B52-4EDB-89FF-B9F2A3BB8B44")]

7.2

请在debug中运行sn -k TestKeyPair.snk

AssemblyInfo.cs中

[assembly: ComVisible(true)]

[assembly: AssemblyKeyFile("TestKeyPair.snk")]

7.3

gacutil /i MyInterop.dll

REGASM MyInterop.dll /tlb:com.MyInterop.tlb 

Register your assembly for COM by using the REGASM command along with the "/tlb" option to generate a COM type library.

8在c++项目中 

#import "<>\\com.ClassLibrary2.tlb" named_guids raw_interfaces_only

build

得到\\com.ClassLibrary2.tlh

9在c++项目中

#pragma comment(lib,"<>\\com.ClassLibrary2.tlh")

10 请找到uuid号码,用CoCreateInstance得到实例

CoInitialize(NULL);   //Initialize all COM Components

HRESULT hr = CoInitialize(NULL);

CLSID clsid = __uuidof(ClassLibrary2::ClassLibrary2);

IID iid = __uuidof(ClassLibrary2::IF1);

11:

ClassLibrary2::IF1 *ptr;

hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,__uuidof(ClassLibrary2::IF1), (LPVOID*)&ptr);

long * rs;

rs = (long *)malloc(sizeof(long)* 1);

ptr->DoWork(true, rs);

CoUninitialize();

或者12:

IClassFactory * p_classfactory;

hr = CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory,(LPVOID*)&p_classfactory);

ClassLibrary2::IF1 *ptr2;  p_classfactory->CreateInstance(NULL, __uuidof(ClassLibrary2::IF1), (LPVOID*)&ptr2);

ptr2->DoWork(true, rs);

其实还有13:

CComPtr

但是这个是VC++专属类

#include <atlbase.h>

CComPtr<ClassLibrary2::IF1> ptr3;

ptr3.CoCreateInstance(clsid);

http://www.codeproject.com/Articles/12673/Calling-Managed-NET-C-COM-Objects-from-Unmanaged-C

http://www.myexception.cn/vc-mfc/145728.html

创建C#DLL的更多相关文章

  1. 在Visual Studio 2010 中创建类库(dll)

    创建类库 选择"文件"->新建->项目->Visual C# ->类库,输入名称,选择位置,单击确定 浏览解决方案资源管理器,可以看到两个C#类,第一个是A ...

  2. C++创建动态链接库(*.dll)

    1.      从 “文件”菜单中,选择 “新建”,然后选择 “项目…”. 2.      在“项目类型”窗格中,选择“Visual C++”下的“Win32”. 3.      在“模板”窗格中,选 ...

  3. MFC创建动态链接库DLL并调用方法详解

    实例一: 1.创建一个动态链接库工程,如login_dll. 2.在原工程头文件或者新建头文件如showdlg.h定义动态链接库的导出函数,代码如下: #include "stdafx.h& ...

  4. 在vs2013下手把手创建/调用dll

    body { font: 16px } 参考了大佬的文章 首先,体会一下静态编译: 创建Win32Project,选DLL,添加一个.h和.cpp文件 点击生成解决方案,然后去debug目录下拷贝.l ...

  5. VS2010创建动态链接库(DLL)的方法

    1.第一步创建WIN32项目,选择DLL 2.第二步,创建你自己的DLL CPP文件和头文件,下面以两个简单的加减法函数为例子导出 然后编译生成即可.DLL文件在Debug或Release目录中 .d ...

  6. RookeyFrame Bug 线上创建的DLL被删除了 模块无法删除 临时解决

    不知道什么情况 在线创建模块,DLL被删除了,但是模块的相关数据无法删除.可以按照下面的方法临时用一下. 产生这个的原因,好像是Config里面的NeedInit一直都是true,没有改为false, ...

  7. VS2019 C++动态链接库的创建使用(1) - 创建使用dll

    静态库:函数和数据被编译进一个二进制文件,通常扩展名为.lib,在使用静态库的情况下,在编译链接可执行文件时,链接器从库中复制这些函数和数据并把它们和应用程序的其它模块组合起来创建最终的可执行文件. ...

  8. 其它 用VB6创建ActiveX.dll

    1.打开VB6 2.选择 ActiveX DLL,点击打开 3.在窗口输入测试代码 Public Function addstr(str As String) As String addstr = & ...

  9. 创建win32 dll 空项目

    动态库,多字节 win32 空项目 添加导出头文件  类 导入: #pragma once #ifndef IP_CLASS_DLL_H #define IP_CLASS_DLL_H #pragma ...

随机推荐

  1. 关于Android WebView内容不同屏幕兼容处理

    最近团队里项目开发APP时,其中有个界面要用到WebView来显示,而不用原生的控件. 开发这个界面的时候遇到一个问题,就是Android的碎片化导致有众多屏幕分辨率,不同分辨率的屏幕对这个界面Web ...

  2. HttpClient and FileUpload

    All communication over the Internet happens using a standard set of protocols, such as File Transfer ...

  3. Microsoft Visual Studio 6.0 Enterprise Edition

    我们的老古董啊  啊啊啊 啊啊 <Microsoft Visual Studio 6.0 Enterprise Edition>(完整9CD,带中文MSDN&   <Micr ...

  4. USBSpirit(USB精灵)更新到1.2.300.105

    USBSpirit(USB精灵)是CopyU!的内核引擎,CopyU!的主要功能均由该引擎提供,此次更新主要内容如下:(版本号:1.2.300.105) 1.[修复]:修复了几处引擎的资源泄露问题,提 ...

  5. Oracle extent

    extent 上的块 物理上绝对连续 多个连续的block组成一个extent 不连续怎么多块读? Oracle 给表分配的单位是extent 去做 全表扫描的时候 以块为单位,按照extent来读取 ...

  6. delphi客户端调服务器端的java webservice如何在参数中传对象? 转

    我试过java返回一个对象到delphi端没问题,可反过来,delphi通过参数传一个对象到java,java端得到的对象值变为空,不知道是不是delphi这边设置或者对象注册方面有问题,究竟该怎么解 ...

  7. jQuery的基本语法

    index.html代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  8. appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator

    appium 1.7.6 不支持findElementByName(locator)  不知道为什么? 脚本中许多这样的语句,麻烦事情多了 org.openqa.selenium.InvalidSel ...

  9. 一款豪华绚丽的移动端图册(zepto.js+photoswipe.js)

    应公司要求,开发一款移动端杂志(等同于移动端幻灯片相册,iOS上能轻松滑动缩放,android上也能滑动缩放).网上找了很久没有符合需求的插件,无意间找到了photoswipe,进行改造之后就成了符合 ...

  10. Collision使用 获取其组件执行变色操作

    using UnityEngine; using System.Collections; public class CyCollision : MonoBehaviour { void OnColli ...