LoadLibrary无法加载C ++ CLI创建的dll

时间:2013-06-18 09:48:30

标签: c++-cli

我在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;
 }

1 个答案:

答案 0 :(得分:2)

当Win32 API函数失败时,它通常会为该线程设置错误代码。所以,说函数返回NULL并不够。 LoadLibrary文档指向GetLastError文档。

这可能是错的:

LoadLibrary((LPCWSTR)"pathto\\email.dll");

您无法强制转换以更改字符串的文件路径表示形式。你似乎已经定义了UNICODE,这很好;您最终将调用LoadLibraryW而不是LoadLibraryA。要为LoadLibraryW构造文件路径,请使用:

LoadLibraryW(L"pathto\\email.dll");