我如何设置一个dll的入口点 [英] How can i set an entrypoint for a dll

查看:152
本文介绍了我如何设置一个dll的入口点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我想dll的DLLMain中的入口点,但是当我尝试导入它在C#我得到一个错误,entrypoint没有找到这里是我的代码:

  #include< Windows.h> 

int Test(int x,int y)
{
return x + y;
}

BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved

{
switch(ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
MessageBox(0,LTest,LFrom unmanaged dll,0);
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

我如何为我的DLL设置一个入口点?如果你不介意,你可以给我一点关于入口点的解释吗?



像我一样,我必须再次设置导入相同的dll并更改入口点,以便我可以使用其他功能在同一个dll?感谢提前。

解决方案

在您的示例中,您似乎打算将Test()作为入口点,但是您不是导出它即使您开始导出它,也可能无法正常使用C ++名称装饰(mangling)。我建议重新定义你的功能:

  externC__declspec(dllexport)int Test(int x,int y) 

externC组件将删除C ++名称变化。 __ declspec(dllexport)组件导出符号。



请参阅 http://zone.ni.com/devzone/cda/tut/p/id/3056 了解更多详情



编辑:您可以按照这种方式添加任意数量的入口点。调用代码只需要知道要检索的符号的名称(如果您正在创建一个静态的.lib,它会为您处理)。


First i thought entry point in dlls DLLMain but then when i try to import it in C# i get an error that entrypoint wasn't found Here is my code:

#include <Windows.h>

int Test(int x,int y)
{
    return x+y;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        MessageBox(0,L"Test",L"From unmanaged dll",0);
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
} 

How can i set an entry point for my dll? And if you dont mind can you give me little explanation about entry point?

Like do i have to set import the same dll again and changing the entry point so i can use other functions in same dll? thanks in advance.

解决方案

In your example, it seems you intend Test() to be an entry point however you aren't exporting it. Even if you begin exporting it, it might not work properly with C++ name "decoration" (mangling). I'd suggest redefining your function as:

extern "C" __declspec(dllexport) int Test(int x,int y)

The extern "C" component will remove C++ name mangling. The __declspec(dllexport) component exports the symbol.

See http://zone.ni.com/devzone/cda/tut/p/id/3056 for more detail.

Edit: You can add as many entry points as you like in this manner. Calling code merely must know the name of the symbol to retrieve (and if you're creating a static .lib, that takes care of it for you).

这篇关于我如何设置一个dll的入口点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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