用vc++写了个简单的MFC shared 的dll,(对vc++不熟, 不知道这样有没有说错) 在C#中增加引用,就出现了错误,错误消息是: 这不是有次的程序集或COM组件。   现在要怎么办??谢谢

解决方案 »

  1.   

    你这个是托管代码调用非托管代码的,这个很容易啊http://www.microsoft.com/china/msdn/archives/library/dnnetcomp/html/unmanagedFuncs.asp
      

  2.   

    那要怎样才可以将dll添加到引用里面呢? 或者说变成托管代码呢?? 谢谢
      

  3.   

    不是引用,而是声明外部方法以下是MSDN的例子声明原型
    [C#]
    public class LibWrap 
    {
       // Declares managed prototypes for unmanaged functions.
       [ DllImport( "User32.dll", EntryPoint="MessageBox", 
          CharSet=CharSet.Auto )]
       public static extern int MsgBox( int hWnd, String text, String caption, 
          uint type );
       // Causes incorrect output in the message window.
       [ DllImport( "User32.dll", EntryPoint="MessageBoxW", 
          CharSet=CharSet.Ansi )]
       public static extern int MsgBox2( int hWnd, String text, 
          String caption, uint type );
       // Causes an exception to be thrown. EntryPoint, CharSet, and 
       // ExactSpelling fields are mismatched.
       [ DllImport( "User32.dll", EntryPoint="MessageBox", 
          CharSet=CharSet.Ansi, ExactSpelling=true )]
       public static extern int MsgBox3( int hWnd, String text, 
          String caption, uint type );
    }
    调用函数
    [C#]
    public class MsgBoxSample
    {
       public static void Main()
       {
          LibWrap.MsgBox( 0, "Correct text", "MsgBox Sample", 0 );
          LibWrap.MsgBox2( 0, "Incorrect text", "MsgBox Sample", 0 );
          try
          {
             LibWrap.MsgBox3( 0, "No such function", "MsgBox Sample", 0 );
          }
          catch( EntryPointNotFoundException )
          {
             Console.WriteLine( "EntryPointNotFoundException thrown as 
               expected!" );
          }
       }
    }
      

  4.   

    谢谢,现在明白了点,但我VC++里的方法为:
    extern "C" __declspec(dllexport) void proce(char* sSource, char* sResult)
    我在C#里相应为:
    public static extern void proce(ref String sSource, ref String sResult);
    好像不太对吧? 正确的写法应该是怎样的呢??谢谢
      

  5.   

    查了下.net帮助文档,用下面的解决了:
    public static extern void proce([In, Out] Char[] sSource, [In, Out] Char[] sResult);