C#调用vc++写的dll 的问题 用vc++写了个简单的MFC shared 的dll,(对vc++不熟, 不知道这样有没有说错) 在C#中增加引用,就出现了错误,错误消息是: 这不是有次的程序集或COM组件。 现在要怎么办??谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这个是托管代码调用非托管代码的,这个很容易啊http://www.microsoft.com/china/msdn/archives/library/dnnetcomp/html/unmanagedFuncs.asp 那要怎样才可以将dll添加到引用里面呢? 或者说变成托管代码呢?? 谢谢 不是引用,而是声明外部方法以下是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!" ); } }} 谢谢,现在明白了点,但我VC++里的方法为:extern "C" __declspec(dllexport) void proce(char* sSource, char* sResult)我在C#里相应为:public static extern void proce(ref String sSource, ref String sResult);好像不太对吧? 正确的写法应该是怎样的呢??谢谢 查了下.net帮助文档,用下面的解决了:public static extern void proce([In, Out] Char[] sSource, [In, Out] Char[] sResult); 如何给一个现有的TreeView增加节点 请教一个在C# webBrowse调用JS的方法. 本地窗体与浏览器窗体通信,请问用什么实现? C#乱码问题,求助!!!! 用ADOX如何修改Access数据库的列数据类型? 枚举类型的问题 给客户的项目做完后,软件的版权和著作权属于谁的? 数据导入的字段格式转换问题! 请教个小问题 急 在线等 C#调用存储过程传递数组参数的问题 应用程序加锁问题??? 如何把字符串001;002;003中001,002,003单独截取出来
[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!" );
}
}
}
extern "C" __declspec(dllexport) void proce(char* sSource, char* sResult)
我在C#里相应为:
public static extern void proce(ref String sSource, ref String sResult);
好像不太对吧? 正确的写法应该是怎样的呢??谢谢
public static extern void proce([In, Out] Char[] sSource, [In, Out] Char[] sResult);