我在 Visual Studio 2010 中使用 CLR 选项创建了一个 c++/cli dll,如下所示,并创建了 email.dll。然后为了测试这一点,创建了另一个 vs2010 win32 项目并尝试使用 LoadLibrary 加载 email.dll,它总是返回 NULL:
HINSTANCE hGetProcIDDLL = LoadLibrary((LPCWSTR)"pathto\\email.dll");
我的问题是:应该以其他方式加载 email.dll 吗?或者如果 email.dll 创建不正确。
email.cpp 的 C++/CLI 代码:定义 DLL 应用程序的导出函数。
#using <mscorlib.dll>
#using <system.dll>
include "stdafx.h"
using namespace System;
using namespace System::Net::Mail;
extern int CallSendEmailFromGmail(char* fromEmail, char* password, char* toEmail, char* subject, char* message);
extern "C"
{
__declspec(dllexport) int SendEmailFromGmail(char* fromEmail, char* password, char* toEmail, char* subject, char* message)
{
return CallSendEmailFromGmail(fromEmail, password, toEmail, subject, message);
}
}
int CallSendEmailFromGmail(char* fromEmail, char* password, char* toEmail, char* subject, char* message)
{
String ^from = gcnew String(fromEmail);
String ^pwd = gcnew String(password);
String ^to = gcnew String(toEmail);
String ^subjectStr = gcnew String(subject);
String ^messageStr = gcnew String(message);
SmtpClient ^client = gcnew SmtpClient();
// client->DeliveryMethod = SmtpDeliveryMethod.Network;
client->UseDefaultCredentials = false;
client->Credentials = gcnew System::Net::NetworkCredential(from, pwd);
client->Port = 587;
client->Host = "smtp.gmail.com";
client->EnableSsl = true;
MailMessage ^mail = gcnew MailMessage(from, to);
mail->Subject = subjectStr;
mail->Body = messageStr;
try
{
client->Send(mail);
}
catch (Exception ^ex)
{
Console::WriteLine("Message : " + ex->Message);
return 1;
}
Console::WriteLine("Message : Done" );
return 0;
}