我看网上说用托管
我也加了
#pragma managed
#using <mscorlib.dll>
using namespace System;但还是不太会我想调用 FormsAuthentication.HashPasswordForStoringInConfigFile()函数
请问该怎么做
我也加了
#pragma managed
#using <mscorlib.dll>
using namespace System;但还是不太会我想调用 FormsAuthentication.HashPasswordForStoringInConfigFile()函数
请问该怎么做
解决方案 »
- VC 串口 多线程 编程
- 请问VC中自定义消息和普通处理有什么区别??
- 谁可以教我用TreeControl控件做个树形表啊
- 请问这个东西叫什么?
- 如何將char *函數中的data傳送出來呢?
- 大虾们请帮忙啊
- vc自带的MediaPlayer控件太老了,所用的解码器是2.0版本,我想升级一下解码器,该怎么弄啊?
- 流文件读写
- 《com技术内幕》的第5章例子怎么编译时包错LIBCD.lib(crt0.obj) : error LNK2001:unresolved external symbol _main?
- 如何在动画控件中通过资源标识符来播放AVI动画资源?
- 问题 急啊!
- tinyxml有方法将node或doc保存到string里吗
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();
也可以把托管代码集中到某几个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"测试");
}
#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中要具体如何操作,求详细点的
#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"));
}