创建自定义简单Mapi DLL,执行时失败

2022-06-09 00:00:00 dll attachment c++ mapi

我需要创建小DLL来拦截简单的MAPI调用并通过以下方式发送文件:

文件->以附件形式发送到(在Excel、Word、Acrobat Reader中...) 或通过资源管理器->右键菜单->发送至->邮件收件人

附加到Gmail。

阅读本文后:

Mapi32.dll Stub Registry Settings (Windows)

Mapi32 Stub Library (Windows)

在Stackoverflow和其他网站上进行搜索时,我使用代码::块创建了一个非常小的DLL,它向我显示了调查该进程的信息。

我已正确地将其安装在注册表中,如HKEY_LOCAL_MACHINESOFTWAREClientsMailSimpMapi.

上前面的链接所示

我已在控制面板中的Win XP PRO->互联网选项中选择了我的SimpMapi客户端。

当我打开Excel编写一些单元格并转到文件->类似发送附件DLL向我显示,第一个调用MapiLogon,第二个调用MapiSendMail,第三个在我关闭Excel时调用MapiLogoff,但随后它抛出一个错误并关闭exel.exe。

如果我从资源管理器窗口尝试,右键单击一个文件并发送到->邮件收件人,它会直接调用MapiSendMail,当我在MsgBox中单击OK时,它会抛出一个错误并关闭Explorer.exe

您能帮我找出出了什么问题吗?

这是mail.h文件:

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

// Todo lo necesario a incluir por mapi.h
#define SUCCESS_SUCCESS         0
#define MAPI_E_USER_ABORT       1
#define MAPI_E_LOGIN_FAILURE    3

typedef unsigned long FLAGS;
typedef unsigned long LHANDLE;
typedef unsigned long FAR *LPLHANDLE, FAR *LPULONG;

typedef struct {
  ULONG ulReserved;
  ULONG ulRecipClass;
  LPSTR lpszName;
  LPSTR lpszAddress;
  ULONG ulEIDSize;
  LPVOID lpEntryID;
} MapiRecipDesc, *lpMapiRecipDesc;

typedef struct {
  ULONG ulReserved;
  ULONG flFlags;
  ULONG nPosition;
  LPSTR lpszPathName;
  LPSTR lpszFileName;
  LPVOID lpFileType;
} MapiFileDesc, *lpMapiFileDesc;

typedef struct {
  ULONG ulReserved;
  LPSTR lpszSubject;
  LPSTR lpszNoteText;
  LPSTR lpszMessageType;
  LPSTR lpszDateReceived;
  LPSTR lpszConversationID;
  FLAGS flFlags;
  lpMapiRecipDesc lpOriginator;
  ULONG nRecipCount;
  lpMapiRecipDesc lpRecips;
  ULONG nFileCount;
  lpMapiFileDesc lpFiles;
} MapiMessage, *lpMapiMessage;


/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

extern "C" void DLL_EXPORT SomeFunction(const LPCSTR sometext);

extern "C" ULONG DLL_EXPORT MAPILogon(ULONG_PTR ulUIParam,LPSTR lpszProfileName,LPSTR lpszPassword,FLAGS flFlags,ULONG ulReserved,LPLHANDLE lplhSession);
extern "C" ULONG DLL_EXPORT MAPILogoff(LHANDLE lhSession,ULONG_PTR ulUIParam,FLAGS flFlags,ULONG ulReserved);
extern "C" ULONG DLL_EXPORT MAPISendDocuments(ULONG_PTR ulUIParam,LPSTR lpszDelimChar,LPSTR lpszFilePaths,LPSTR lpszFileNames,ULONG ulReserved);
extern "C" ULONG DLL_EXPORT MAPISendMail(LHANDLE lhSession,ULONG_PTR ulUIParam,lpMapiMessage lpMessage,FLAGS flFlags,ULONG ulReserved);

#endif

这是main.cpp文件:

#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
ULONG DLL_EXPORT MAPILogon(ULONG_PTR ulUIParam,LPSTR lpszProfileName,LPSTR lpszPassword,FLAGS flFlags,ULONG ulReserved,LPLHANDLE lplhSession)
{
    MessageBoxA(0, "MAPILogon", "MAPILogon", MB_OK | MB_ICONINFORMATION);
    return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPILogoff(LHANDLE lhSession,ULONG_PTR ulUIParam,FLAGS flFlags,ULONG ulReserved)
{
    MessageBoxA(0, "MAPILogoff", "MAPILogoff", MB_OK | MB_ICONINFORMATION);
    return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPISendDocuments(ULONG_PTR ulUIParam,LPSTR lpszDelimChar,LPSTR lpszFilePaths,LPSTR lpszFileNames,ULONG ulReserved)
{
    MessageBoxA(0, "MAPISendDocuments", "MAPISendDocuments", MB_OK | MB_ICONINFORMATION);
    return SUCCESS_SUCCESS;
}
ULONG DLL_EXPORT MAPISendMail(LHANDLE lhSession,ULONG_PTR ulUIParam,lpMapiMessage lpMessage,FLAGS flFlags,ULONG ulReserved)
{
    MessageBoxA(0, "MAPISendMail", "MAPISendMail", MB_OK | MB_ICONINFORMATION);
    return SUCCESS_SUCCESS;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            // attach to process
            // return FALSE to fail DLL load
            break;

        case DLL_PROCESS_DETACH:
            // detach from process
            break;

        case DLL_THREAD_ATTACH:
            // attach to thread
            break;

        case DLL_THREAD_DETACH:
            // detach from thread
            break;
    }
    return TRUE; // succesful
}

提前谢谢。

豪尔赫


解决方案

链接中:Creating Custom Simple Mapi DLL, throws error when executing

Andy帮我解决了调用约定不正确的问题。

将WINAPI添加到我所有简单的MAPI函数声明和定义中,它起作用了,示例:

extern "C" ULONG DLL_EXPORT WINAPI MAPILogon( // etc

然后问题是我的导出函数是用声明导出的。

要在代码中更正此错误::块,您必须添加:

主菜单:项目->构建选项->GNU GCC编译器->链接器设置->其他链接器选项:-WL,--KILL-AT

谢谢!

相关文章