制作 VC++ .exe 到 DLL 可以吗?
我有一个 VC++ win 32 应用程序,它编译成 EXE。但现在我想将其转换为 dll,以便可以在另一个应用程序中加载它。我尝试将 Visual Studio 属性从 .EXE 更改为 .DLL,成功转换了它,但当我使用 GetProcAddress 时,它总是返回 NULL 。我不确定我所做的是对还是错。
基本上这就是我想要实现的目标:
- 我想链接项目1和项目2
- 项目2应该能够调用项目1的功能(这是一个exe文件)
编辑 大家好,感谢您的意见。我告诉了你们所说的。即使这样我的 GetProcAddress 返回 0 。我做错了什么吗?下面显示了我的 dll 加载代码。
HINSTANCE LoadMe = LoadLibrary( _T("D:\\VC++Project\\CVAList\\CVAList\\ExportTest.dll"));
if (LoadMe != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");
EntryPointfuncPtr LibMainEntryPoint;
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"PrintFloatsVal");
LibMainEntryPoint (a1 ,a,b,c,d ); // 4 double
编辑 DLL 导出代码
#define DllExport __declspec( dllexport )
DllExport void PrintFloatsVal ( int amount, double &d1 ,double &d2 , double &d3 ,double &d4)
{
....
..
}
I have an VC++ win 32 application which compiles into an EXE. But now I want to convert it into dll so that I can load that in another application.I tried changing in Visual Studio properties from .EXE to .DLL which successfully converted it but whn i use GetProcAddress it always returns NULL . I am not sure what I am doing is right or wrong .
Basically this what I want to achieve :
- I want to link project 1 and project2
- Project 2 should be able to invoke the functions of project1(which is an exe currenlty)
EDIT
Hi guys thanks for your input .I told what you guys said . even then my GetProcAddress returns zero . Am i am doing anything wrong .Shown my dll loading code below .
HINSTANCE LoadMe = LoadLibrary( _T("D:\\VC++Project\\CVAList\\CVAList\\ExportTest.dll"));
if (LoadMe != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");
EntryPointfuncPtr LibMainEntryPoint;
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"PrintFloatsVal");
LibMainEntryPoint (a1 ,a,b,c,d ); // 4 double
EDIT DLL Export Code
#define DllExport __declspec( dllexport )
DllExport void PrintFloatsVal ( int amount, double &d1 ,double &d2 , double &d3 ,double &d4)
{
....
..
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
__declspec dllexport
关键字导出您想要访问的函数。因此,如果您将清单常量“BUILDING_MY_DLL”添加到项目中,则声明您关心的函数的头文件可以在 DLL 项目和任何使用 DLL 的代码中使用:
并装饰您想要导出的函数
:您希望访问的函数是用 C++ 实现的,出于函数重载和其他目的,它将被修饰,并且最好像任何其他函数一样直接访问它。但是,如果您希望使用 GetProcAddress(),则最好通过使用 extern "C" { ... } 包围该函数来为其提供 C 链接。这将使导出的名称与代码中使用的名称相同。
参考: http://msdn.microsoft.com /en-us/library/a90k134d(v=vs.80).aspx
You need to export the functions you wish to access using the
__declspec dllexport
keyword.So if you add the manifest constant 'BUILDING_MY_DLL' to the project, the header file that declares the functions you care about can be used in both the DLL project and any code that uses the DLL:
And decorate the functions you wish to export:
If the function you wish to access is implemented in C++ it will be decorated, for the purposes of function overloading and other purposes, and it best accessed directly like any other function. If you wish to use
GetProcAddress()
however you are better off giving it C-linkage by surrounding the function withextern "C" { ... }
. This will make the exported name the same the name used within the code.Reference: http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx