我看网上说用托管
我也加了
#pragma managed
#using <mscorlib.dll> 
using namespace System;但还是不太会我想调用 FormsAuthentication.HashPasswordForStoringInConfigFile()函数
请问该怎么做

解决方案 »

  1.   

    1 使用#using引用C# DLL,而不是#include;
    2 别忘了using namespace XXX;
    3 使用C++/clr语法,采用正确的访问托管对象,即:使用帽子'^',而不是星星'*'。eg:C++类似下面的代码 返回char *#define EXPORT_API extern "C" __declspec(dllexport)
    char g_aszBuffer[1024] = {0};EXPORT_API const char* GetBufferString()
    {
     sprintf(aszBuffer,"%s", _T("Text"));
     return aszBuffer;
    }C#调用
    [DllImport(@"*.dll", EntryPoint = "GetBufferString", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
            public extern static string GetBufferString(); 
      

  2.   

    楼主要的C++调用C#,而不是C#调用C++.(反了)
      

  3.   

    在非托管的VC++.Net中,可以混入托管代码,即混合编程。以.Net 2005为例,通过在项目属性中,把“配置属性\常规”下的“公共语言运行库支持”选项设为“公共语言运行库支持(/clr)”,即可在全项目范围内使用托管代码。
    也可以把托管代码集中到某几个CPP文件中,只对这几个CPP文件进行托管。此时项目属性中还设为“无公共语言运行库支持”,仅在这几个CPP文件的属性中设为“公共语言运行库支持(/clr)”(位于“C/C++\常规”中),同时把这几个CPP文件的属性“C/C++\预编译头\创建/使用预编译头”改为“不使用预编译头”,把“C/C++\代码生成\启用C++异常”改为“是,但有 SEH 异常(/EHa)”即可。
    对于混合编程DLL,如果DllMain所在的CPP文件托管,需要在DllMain函数前后分别加上#pragma managed(push, off)和#pragma managed(pop)以保证DllMain不托管,并且确保不要在DllMain中直接或间接调用任何托管代码,另外把该CPP文件中的#include <afxdllx.h>移到一个没有托管且禁用预编译头的CPP中,并在它之前包含afxwin.h(注,在.Net 2005 SP2之上的版本中不移动afxdllx.h的位置应该也行,因为afxdllx.h已被定义为不使用托管代码)。这是因为DllMain被调用时托管环境还没有建立好,调用托管代码会导致出错。所以,一般情况下不要对整个工程设置托管,只托管几个CPP文件会更简单。
    要想使用托管代码,首先要用#using加入mscorlib.dll、system.dll等.Net模块的引用,然后编写托管代码。托管代码和非托管代码中的函数可以相互调用,在托管代码中可以像非托管代码那样编码,例如使用MFC对象、指针等,但不要在DllMain或全局、静态变量中调用托管代码。下面的代码演示了调用.Net组件中的MessageBox,在非托管代码中调用Test函数即可。
    //例1
    //.Net2005, dotNet1.cpp
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Windows.Forms.dll>
    using namespace System;void Test()
    {
    Windows::Forms::MessageBox::Show(L"测试");
    }
      

  4.   

    2l的代码看不太懂,3l我加
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Web.Security.dll>
    using namespace System;编译提示找不到 System.Web.Security.dll
    我不清楚具体要怎么写.net里是
        using System.Web.Security;string hashPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(this.Password.Text, "MD5");
    现在我就想实现这个函数,在mfc中要具体如何操作,求详细点的
      

  5.   

    没有System.Web.Security.dll,在System.Web.dll里面。#using <System.Web.dll>Web::Security::FormsAuthentication::HashPasswordForStoringInConfigFile(L"abc", L"MD5");
      

  6.   

    刚试了一下,可以//一个托管cpp
    #include <afxwin.h>
    #include <afxext.h>
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Windows.Forms.dll>
    #using <System.Web.dll>
    using namespace System;CString Test(const wchar_t* inputStr)
    {
    String^ inputS = gcnew String(inputStr);
    String^ ret = Web::Security::FormsAuthentication::HashPasswordForStoringInConfigFile(inputS, L"MD5");
    CString sPassword = ret;
    return ret;
    }//在非托管代码中调用
    CString Test(const wchar_t* inputStr);void CabcdDlg::OnBnClickedOk()
    {
    // TODO: 在此添加控件通知处理程序代码
    AfxMessageBox(Test(L"abc"));
    }
      

  7.   

    不好意思,这几天很忙,都没时间来看,shuke的方法确实可以,非常感谢