簡體   English   中英

如何在x64環境中使用32位DLL? 它不會讓我強制x86編譯!

[英]How to use 32-bit DLL in x64 environment? It won't let me force x86 compile!

VC ++ 2008,CLR控制台應用程序,在win7 x64下開發。

我將MS Office v.12 PIA與.net結合使用來實現一些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解決方案平台下,我唯一的選擇是Win32,New和Edit。 因此,我單擊“新建”,在“類型或選擇新平台”下鍵入“ x86”,然后單擊“復制設置來自”以選擇“任何CPU”或其他合適的選項,但我唯一的選擇是Win32,然后選擇! 我想也許是因為我已經瞄准了.net,所以為了測試這個理論,我開始了一個新項目,這次是作為Win32控制台應用程序。 即使在這種類型的項目下,我唯一的選擇是Win32。 我聽說過的x86,x64,任何CPU和Itanium在我的VS2008中都不存在!

所以我在這里不知所措。 如何強制VS將exe編譯為x86,以便可以使用mapi接口? 還是我可以使用64位版本的OLMAPI32.DLL? 如果沒有64位庫,如何使MAPI在我的代碼中工作?當我嘗試為x86設置環境時,VS給了我極大的幫助嗎? 我簡直不敢相信我的64位環境會自動使我失去使用MAPI的資格。

謝謝

我相信Win32在Visual Studio C ++中是x86

您可以通過usihg corflags強制使用32位CLR。 例如CorFlags.exe /32bit+ file.exe

有一個64位版本的MAPI與64位版本的MS Outlook一起提供。 此MSDN文章對其進行了詳細描述。

所以,為了幫助那些幫助我的人,以及任何可能有類似問題並且發生在這個主題上的人,這就是我最終做的......

我放棄了純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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM