64-bit MFC program calls 32-bit DLL

When a 64-bit MFC application calls a 32-bit DLL, you can do so by creating an intermediate Wrapper DLL. Here is a simple example showing how to call a function of a 32-bit DLL from a 64-bit MFC application:

Suppose you have a 32-bit DLL that contains a MyFunctionfunction called that takes an integer argument and returns an integer. You want to call this function from a 64-bit MFC application.

First, create a new 64-bit DLL project (Wrapper DLL) for interacting with the 32-bit DLL. In this 64-bit DLL project, write an export function that will call a function of the 32-bit DLL.

Here is a simple example:

// WrapperDLL.h
#pragma once

#ifdef WRAPPERDLL_EXPORTS
#define WRAPPERDLL_API __declspec(dllexport)
#else
#define WRAPPERDLL_API __declspec(dllimport)
#endif

extern "C" WRAPPERDLL_API int CallMyFunction(int value);
// WrapperDLL.cpp
#include "WrapperDLL.h"
#include "windows.h"

typedef int(*MYFUNCTION)(int);

int CallMyFunction(int value)
{
    
    
    HINSTANCE hDLL = LoadLibrary(L"path_to_32bit_dll.dll");
    if (hDLL != NULL)
    {
    
    
        MYFUNCTION myFunction = (MYFUNCTION)GetProcAddress(hDLL, "MyFunction");
        if (myFunction != NULL)
        {
    
    
            int result = myFunction(value);
            FreeLibrary(hDLL);
            return result;
        }
        FreeLibrary(hDLL);
    }
    return -1;
}

In this example, we create an CallMyFunctionexported function named . This function loads a 32-bit DLL and uses GetProcAddressfunction to get MyFunctionthe address of the function. Then, we pass parameters to the function of the 32-bit DLL and return the result.

Next, compile the Wrapper DLL project and generate a 64-bit DLL file.

Then, in your 64-bit MFC application, you can CallMyFunctionindirectly call the functions of the 32-bit DLL by calling the function. For example:

// MFCAppDlg.cpp (64位MFC应用程序的对话框类文件)
#include "WrapperDLL.h"

// ...

void CMFCAppDlg::OnBnClickedButton1()
{
    
    
    int result = CallMyFunction(42);
    // 处理结果...
}

Here, we called CallMyFunctionthe function in the button click event handler in the MFC application, passing the parameter value 42 to the function of the 32-bit DLL. You can use the returned result as needed.

You need to make sure that you use the corresponding 64-bit compiler options when compiling 64-bit MFC applications and Wrapper DLLs. Also, you need to replace the path to the 32-bit DLL with your own actual path.

This is just a simple example, and actual implementations may vary. You may need to make more adjustments and adaptations based on the specific functions and parameters of the 32-bit DLL.

Guess you like

Origin blog.csdn.net/chy555chy/article/details/130952183