c#中能否调用VC++6.0写的dll

sunshinecsdn 2004-07-29 02:21:19
如题,刚开始学c#,请多多指教,要是能调用的话,怎么写,最好能给个例子,多谢!
...全文
177 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunshinecsdn 2004-08-03
  • 打赏
  • 举报
回复
谢谢大家的帮助,问题解决了,接分。
windinwing 2004-07-30
  • 打赏
  • 举报
回复
ActiveX DLL可以直接添加引用,其他的楼上都说了
winxieddd 2004-07-30
  • 打赏
  • 举报
回复
楼上的方法可行,但要是用到很多MFC的东东,呵呵~~~我看还是算了吧。



附DllImport用法:

DllImport 属性定义如下:

namespace System.Runtime.InteropServices
{
 [AttributeUsage(AttributeTargets.Method)]
 public class DllImportAttribute: System.Attribute
 {
  public DllImportAttribute(string dllName) {...}
  public CallingConvention CallingConvention;
  public CharSet CharSet;
  public string EntryPoint;
  public bool ExactSpelling;
  public bool PreserveSig;
  public bool SetLastError;
  public string Value { get {...} }
 }
}

  说明:

  1、DllImport只能放置在方法声明上。

  2、DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。

  3、DllImport具有五个命名参数:

   a、CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。

   b、CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。

   c、EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。

   d、ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。

   e、PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。

   f、SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。

  4、它是一次性属性类。

  5、此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。

  下面是 C# 调用 Win32 MessageBox 函数的示例:

using System;
using System.Runtime.InteropServices;
class MainApp
{ //通过DllImport引用user32.dll类。MessageBox来自于user32.dll类
 [DllImport("user32.dll", EntryPoint="MessageBox")]
 public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);
 public static void Main()
 {
  MessageBox( 0, "您好,这是 PInvoke!", ".NET", 0 );
 }
}

ehom 2004-07-30
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;

[DllImport("test.dll")]
public static extern int test(int a);
qianli918 2004-07-30
  • 打赏
  • 举报
回复
当然能调用了。
设置成非托管模式

110,538

社区成员

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

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

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