vc怎么调用c# 写的dll呢。 最好有个例子

skinfeature 2008-07-18 10:21:05
vc怎么调用c# 写的dll呢。 最好有个例子
...全文
534 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Red_angelX 2008-07-18
  • 打赏
  • 举报
回复
clr还有可能
其他的别想
gomoku 2008-07-18
  • 打赏
  • 举报
回复
Calling .net library using COM example
To test the sample, follow the steps:
1. Copy the following example as Hello.cs and CallHello.cpp to a folder like c:\temp
2. Open up a Visual studio dos window (to make sure all paths are set correctly)
Start -> Program -> Visual Studio 2005 -> Vistual Studio Tools -> Visual Studio Command Prompt.
[code=BatchFile]
3. cd \temp
4. csc /t:library Hello.cs
5. regasm /tlb /codebase Hello.dll
6. cl CallHello.cpp
[/code]
7. Now you have CallHello.exe to run.
8. You can unregister Hello.dll by regasm /u Hello.dll


// Hello.cs
// 1. Compile with: csc.exe /t:library Hello.cs
// 2. Register with: regasm.exe /tlb /codebase Hello.dll

using System;
using System.Runtime.InteropServices;

[assembly: ComVisible(true)]
[assembly: System.Reflection.AssemblyVersion("1.0.0.0")]
[assembly: Guid("2b31035b-7575-4980-8ba7-111111111111")]

[Guid("2b31035b-7575-4980-8ba7-333333333333")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IHello // expose as COM interface
{
[DispId(1)] void SayHello();
}

[Guid("2b31035b-7575-4980-8ba7-222222222222")]
[ClassInterface(ClassInterfaceType.None)]
public class Program : IHello
{
public void SayHello()
{
System.Windows.Forms.MessageBox.Show("Hello from .net");
}
}



// CallHello.cpp : Defines the entry point for the console application.
// 3. Compile with: cl.exe CallHello.cpp

#include <windows.h>
#include <objbase.h>
#import "Hello.tlb" raw_interfaces_only

int main(int argc, char* argv[])
{
CoInitialize(NULL);
printf("Callint .net class as COM (showing a dialog box)");

Hello::IHelloPtr iHello;
HRESULT hr = iHello.CreateInstance( __uuidof(Hello::Program) );
if(SUCCEEDED(hr))
{
iHello->SayHello(); // call .net interface via COM
}
CoUninitialize();
return 0;
}
wo2xie 2008-07-18
  • 打赏
  • 举报
回复
[DllImport("MWRF32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr rf_init(int port, long baud);
要使用这种格式的吧.小弟也只是低手而己,希望能帮到你.把dll复制到Debug下面
北京的雾霾天 2008-07-18
  • 打赏
  • 举报
回复
MFC的话最好不要这样调用,因为这样要安装framework,如果是.net的VC就引用就可以了。
gomoku 2008-07-18
  • 打赏
  • 举报
回复
1. use COM
2. CLR hosting

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧