让一个dll通过发送一个指针调用一个.exe函数 [英] Let a dll call a .exe function by sending a pointer

查看:156
本文介绍了让一个dll通过发送一个指针调用一个.exe函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题看起来像我以前问过,除了我现在知道,你不能从一个全局对象调用main函数。所以这段代码不适用于main。



这是代码。



。 exe

main.cpp

  #includedll_class.h
#include< iostream>
int my_main(void)
{
std :: cout< 输入my_main code.\\\
;
std :: getchar();
}

DllClass对象(my_main);
int main(void)
{
std :: cout<< 输入主代码。
std :: getchar();
}

.dll

dll_class.h

  #includeplatform.h
#include< iostream>
class DLL_API DllClass // DLL_API只是一个用于导入和导出的宏。
{
public:
DllClass(int(* main)(void))
{
std :: cout< 启动application.\\\
;
platform = new Platform(main);
}
〜DllClass(void)
{
删除平台;
}
private:
Platform * platform;
};

platform.h

  class DLL_API Platform 
{
public:
Platform(main_t main_tp);
〜Platform(void){}
};

platform.cpp

  #includeplatform.h
#includeWindows.h
#include< iostream>

HHOOK hookHandle;
int(* main_p)(void); //这将保存.exe的main函数。
LRESULT CALLBACK keyHandler(int nCode,WPARAM wParam,LPARAM lParam);

DWORD WINAPI callMain(_In_ LPVOID lpParameter)
{
std :: cout< 调用主函数。
main_p();
return 0;
}

Platform :: Platform(int(* main_tp)(void))
{
main_p = main_tp;
CreateThread(NULL,0,callMain,NULL,0,NULL);
std :: cout<< Setting hooks.\\\
;
hookHandle = SetWindowsHookEx(WH_MOUSE_LL,keyHandler,NULL,0);
std :: cout<< Enter message loop.\\\
;
MSG消息;
while(GetMessage(& message,(HWND)-1,0,0)!= 0){
TranslateMessage(& message);
DispatchMessage(& message);
}
}

LRESULT CALLBACK keyHandler(int nCode,WPARAM wParam,LPARAM lParam)
{
std :: cout< 里面的钩子函数。\\\
< std :: endl;
return CallNextHookEx(hookHandle,nCode,wParam,lParam);
}

它运行得很好,直到某一时刻。
这是输出。

 启动应用程序。 
设置钩子。
调用主函数。
输入消息循环。
里面的钩子函数。 (当然很多次)。

但它从不说:

 输入my_main代码。 
输入主代码。

是不可能让dll调用exe函数?

对象里面 main 。如果需要,将 my_main 传递给对象。但这将解决所有的试图在它被构造之前使用的东西。



这里的关键(我只从另一个答案中读取评论收集的)是 cout 对于主可执行文件和DLL不是同一个。这导致在您输入 main 之前调用 cout 的问题[我在之前解释过的问题也是如此--C运行时库需要初始化某些事情,并且在 main 被调用之前以未指定的顺序发生。


This question looks like one I asked before, except that I now know that you can't call the main function from a global object. So this code doesn't work with main. But why does it fail with other functions as well?

This is the code.

.exe
main.cpp

#include "dll_class.h"
#include <iostream>
int my_main(void)
{
    std::cout << "Enter the my_main code.\n";
    std::getchar();
}

DllClass object(my_main);
int main(void)
{
    std::cout << "Enter the main code.\n";
    std::getchar();
}

.dll
dll_class.h

#include "platform.h"
#include <iostream>
class DLL_API DllClass //DLL_API is just a macro for import and export.
{
public:
    DllClass(int(*main)(void))
    {
        std::cout << "Start application.\n";
        platform = new Platform(main);
    }
    ~DllClass(void)
    {
        delete platform;
    }
private:
    Platform* platform;
};

platform.h

class DLL_API Platform
{
public:
    Platform(main_t main_tp);
    ~Platform(void){}
};

platform.cpp

#include "platform.h"
#include "Windows.h"
#include <iostream>

HHOOK hookHandle;
int(*main_p)(void);//This will hold a the main function of the the .exe.
LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam);

DWORD WINAPI callMain(_In_  LPVOID lpParameter)
{
    std::cout << "Calling the main function.\n";
    main_p();
    return 0;
}

Platform::Platform(int(*main_tp)(void))
{
    main_p = main_tp;
    CreateThread(NULL, 0, callMain, NULL, 0, NULL);
    std::cout << "Setting hooks.\n";
    hookHandle = SetWindowsHookEx(WH_MOUSE_LL, keyHandler, NULL, 0);
    std::cout << "Enter message loop.\n";
    MSG message;
    while(GetMessage(&message, (HWND)-1, 0, 0) != 0){
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
}

LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam)
{
    std::cout << "Inside the hook function.\n" << std::endl;
    return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}

It runs great, till a certain moment. This is the output.

Start application.  
Setting hooks.  
Calling the main function.  
Enter message loop.  
Inside the hook function. (A lot of times of course).  

but it never says:

Enter the my_main code.
Enter the main code.

Is it impossible to let dll call a exe function?

解决方案

Surely, the CORRECT thing to do here is to construct the object inside main. If needed, pass in my_main to the object. But this will solve all of the "trying to use something before it has been constructed".

The key here (which I only gather from reading the comments on the other answer) is that the cout object is not "the same one" for the main executable and the DLL. This leads to issues with calling things that do cout before you have entered main [which I tried to explain in the previous question too - the C runtime library needs to initialize certain things, and that happens in an unspecified order before main is called].

这篇关于让一个dll通过发送一个指针调用一个.exe函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆