大家好啊,向大家请教个问题:
C#如何调用"VC++.Net下生成的MFC DLL"?
请大家帮忙看下,谢谢
(1)新建项目选择:MFC DLL,工程文件名称是mfcdll;
(2)应用程序设置选择:使用共享MFC DLL的规则;
(3)
// 重写
public:
virtual BOOL InitInstance(); DECLARE_MESSAGE_MAP()
};bool IS_OK(CString str);  //判断传入的字符串是否匹配(不属于类CmfcdllApp)
void test_ISOK(); //调用bool IS_OK(CString str) 
(4)EXPORTS
    ; 此处可以是显式导出
IS_OK @1
test_ISOK @2 
  (5)// CmfcdllApp 初始化BOOL CmfcdllApp::InitInstance()
{
CWinApp::InitInstance(); return TRUE;
}
//方法1
bool IS_OK(CString str) 
 //判断传入的字符串是否匹配
{
bool ret_flag = false; if (str == "OK") //返回值true
{
ret_flag = true;
} else  //返回值false
{
ret_flag = false;
}
return ret_flag;
}//方法2
void test_ISOK()
 //调用bool IS_OK(CString str) 
{
bool flag1 = false;
CString tmpstr =L"OK"; flag1 = IS_OK(tmpstr); if (flag1 == true)
{
//向文本文件写入OK
} if (flag1 == false)
{
//向文本文件写入NG
}
}

解决方案 »

  1.   

    VC++中的CString在C#中怎么处理啊?
    请大家帮忙看下,在线等,谢谢 
      

  2.   

    C#使用c++的DLL,不要用CString等类型做参数,用char*d等,这样C#可以用string等来处理
      

  3.   

    using System; 
    //这是用到DllImport时候要引入的包 
    using System.Runtime.InteropServices; public class InvokeDll { [DllImport("外部生成的DLL.dll", CharSet=CharSet.Auto)] //声明外部的标准动态库的函数, 跟Win32API是一样的. 
    static extern int add(int a,int b); public static void Main() { //使用外部的标准动态库的函数 
    Console.WriteLine(add(10,30)); } } 
    不管对方的dll是什么编译的,但肯定符合dll的规范吧!所以用c#调用没有什么大问题,网上相关的文章很多, 
    可能比较麻烦点是类型转换和函数在C#中的声明 建议用VC .NET建一个包容类dll,然后C#就可以很自然地使用这个VC.NET的包容dll了
    网友回复:楼上正解 
    1。符合dll规范 
    2。注重类型转换 
    3。基本语法 [DllImport("system32.dll"] 
      

  4.   

    static extern int add(int a,int b); 
    (1)向楼上的朋友请教一下:static extern是必须的么?
    导出函数的类型有限制么?(2)C#调用VC.Net生成的DLL时,是不是最好把方法放在一个自定义类里面,导出一个类?
      

  5.   


    static extern int add(int a,int b); 
    (1)向楼上的朋友请教一下:static extern是必须的么? 
    导出函数的类型有限制么? 
    -------------------
    最好这样,标准化接口函数名(2)C#调用VC.Net生成的DLL时,是不是最好把方法放在一个自定义类里面,导出一个类?
    ------------------------
    不是最好,是最好不要。那不然你就得用C#来调COM了
      

  6.   

    (1)static extern int add(int a,int b);
    在DLL中定义的方法原型是int add(int a,int b);?(2)VC++导出的方法,不可以直接用C#调用么?