VC ++ 2008,CLR控制台应用程序,在win7 x64下开发。
我正在使用.net和MS Office v.12 PIA来做一些Excel自动化,这很顺利。但是现在,我正在启动代码的下一部分,其中涉及做一些简单的电子邮件事务,所以我试图让MAPI在我的代码中工作。基本上,它读取相应的注册表项以获取OLMAPI32.DLL文件的完整路径,然后从该dll中尝试LoadLibrary / GetProcAddress。
这是一个片段:
using namespace System;
using namespace Microsoft::Office::Interop;
using namespace Microsoft::Win32;
int main(array<System::String ^> ^args)
{
RegistryKey^ subK = Registry::LocalMachine->OpenSubKey("SOFTWARE\\Clients\\Mail\\Microsoft Outlook");
String^ mailDll = safe_cast<String^>(subK->GetValue("DLLPathEx"));
CStringW cStrMailDll = mailDll; //Just to put mailDll in a format that LoadLibrary() can read.
LPMAPIINITIALIZE mapiInit = NULL;
HMODULE mapiLib = LoadLibrary(cStrMailDll); // This Returns NULL
if(mapiLib == NULL)
{
printf("\nError: %d\n", GetLastError()); // System Error 193 - ERROR_BAD_EXE_FORMAT
return 1;
}
...(more code)
...
LoadLibrary设置系统错误193:“%1不是有效的Win32应用程序”对我来说并不震惊。做了一些研究后,我认为我需要做的就是强制x86编译。所以我转到Configuration Manager,在Active solution平台下,我唯一的选择是Win32,New和Edit。所以我点击New,在“键入或选择新平台”下键入“x86”,然后单击“Copy settings from”选择“Any CPU”或其他合适的东西,但我唯一的选择是Win32,和!我想也许是因为我已经瞄准了.net,所以为了测试这个理论,我开始了一个新项目,这次是作为Win32控制台应用程序。即使在这种类型的项目下,我唯一的选择是Win32。我听说过的x86,x64,Any CPU和Itanium在我的VS2008中不存在!
所以我在这里不知所措。我如何强制VS将我的exe编译为x86,以便我可以使用mapi接口?或者我可以使用64位版本的OLMAPI32.DLL吗?如果没有64位的库可以让我的代码中的MAPI工作,当我尝试为x86设置我的环境时,VS给了我大脑中的亲爱的光芒?我简直无法相信我的64位环境会自动取消使用MAPI的资格。
由于
答案 0 :(得分:1)
我相信Win32在Visual Studio C ++中是x86
答案 1 :(得分:1)
您可以通过usihg corflags强制使用32位CLR。例如CorFlags.exe /32bit+ file.exe
答案 2 :(得分:0)
有一个64位版本的MAPI与64位版本的MS Outlook一起提供。 This MSDN article详细描述了它。
答案 3 :(得分:0)
所以,为了帮助那些帮助我的人,以及任何可能有类似问题并且发生在这个主题上的人,这就是我最终做的......
我从纯MAPI路线中走出来并决定改为使用Outlook PIA。所以现在我的代码看起来像这样:
#define nul Reflection::Missing::Value
#include "stdafx.h"
using namespace System;
using namespace Microsoft::Office::Interop;
int main(array<System::String ^> ^args)
{
// Create the outlook object
Outlook::Application^ olApp = gcnew Outlook::Application();
// Get an instance of the MAPI namespace via the outlook object
Outlook::NameSpace^ olNs = olApp->GetNamespace("MAPI");
// Log this instance into the MAPI interface
// Note that this will cause the user to be prompted for which profile
// to use. At least, it prompts me, and I only have one profile anyway. Whatever.
// Change the first argument to the name of the profile you want it to use (String)
// to bypass this prompt. I personally have mine set to "Outlook".
olNs->Logon(nul, nul, true, true);
// Get my Inbox
Outlook::Folder^ defFolder = safe_cast<Outlook::Folder^>(olNs->GetDefaultFolder(Outlook::OlDefaultFolders::olFolderInbox));
// Get all of the items in the folder (messages, meeting notices, etc.)
Outlook::Items^ fItems = defFolder->Items;
// Sort them according to when they were received, descending order (most recent first)
fItems->Sort("[ReceivedTime]", true);
// Show me the folder name, and how many items are in it
printf("\n%s: %d\n", defFolder->Name, fItems->Count);
// Make an array of _MailItems to hold the messages.
// Note that this is a _MailItem array, so it will only hold email messages.
// Other item types (in my case, meeting notices) cannot be added to this array.
array<Outlook::_MailItem^>^ mail = gcnew array<Outlook::_MailItem^>(fItems->Count);
int itemNum = 1;
int offset = 0;
// If there's anything in my Inbox, do the following...
if(fItems->Count)
{
// Try to grab the first email with "fItems->GetFirst()", and increment itemNum.
try
{
mail[itemNum++] = safe_cast<Outlook::_MailItem^>(fItems->GetFirst());
}
// If it threw an exception, it's probably because the item isn't a _MailItem type.
// Since nothing got assigned to mail[1], reset itemNum to 1
catch(Exception^ eResult)
{
itemNum = 1;
}
// Ok, now use "fItems->GetNext()" to grab the rest of the messages
for(; itemNum <= (fItems->Count-offset); itemNum++)
{
try
{
mail[itemNum] = safe_cast<Outlook::_MailItem^>(fItems->GetNext());
}
// If it puked, then nothing got assigned to mail[itemNum]. On the next iteration of
// this for-loop, itemNum will be incremented, which means that *this* particular index
// of the mail array would be left empty. To prevent this, decrement itemNum.
// Also, if itemNum ever gets decremented, then that will ultimately cause this loop
// to iterate more times than fItems->Count, causing an out-of-bounds error. To prevent
// that, increment "offset" each time you decrement "itemNum" to compensate for the
// offset.
catch(Exception^ eResult)
{
itemNum--;
offset++;
}
}
// Show me the money!
for(int i=1; i <= (fItems->Count-offset); i++)
printf("%d - %s\n", i, mail[i]->Subject);
}
olNs->Logoff();
return 0;
}
现在我知道如何进入,我可以继续完成我的项目!感谢大家的帮助!
干杯! d