有C++的源文件,怎么能生成dll文件?
1.新建一个MFC DLL或Win32 DLL工程
2.创建好后里面会有stdafx.h和stdafx.cpp以及{$工程名}.cpp文件
3.删除{$工程名}.cpp文件 将你的cpp文件添入工程
4.修改你的cpp文件 添加一句#include "stdafx.h"在代码第一行
3和4,或者直接复制自己的C++源文件覆盖{$工程名}.cpp文件中除第一句#include
"stdafx.h"外的所有内容
5.Build
一、生成DLL
1.新建DLL工程
生成DLL可以多种方法,这里介绍一种。在VS中,新建一个空的项目,项目类型选Visual C++ 模板中选Win32
Console
Application,输入名称,我们在此以dll为名称,确认,点下一步,在应用程序类型中,勾选DLL(D),点完成。或者直接新建完后修改工程属性:把生成EXE改为生成DLL(点项目-属性-配置属性-常规-项目默认值-配置类型,初始为应用程序(.exe),点下拉框,选择动态库(.dll)即可)。
2.源代码:
#include
#include
using namespace std;
#ifdef
__cplusplus // if used by C++ code
extern "C"
{ // we need to export the C interface
#endif
__declspec(dllexport) void output()
{
MessageBox(NULL, "hello world", "hello", 0);
}
#ifdef __cplusplus
}
#endif
直接复制上面的C++源文件覆盖{$工程名}.cpp文件中除第一句#include
"stdafx.h"外的所有内容
这时候会报错:error C2664: 'MessageBoxW' : cannot
convert parameter 2 from 'const char [12]' to 'LPCWSTR'
Types pointed to are unrelated; conversion
requires reinterpret_cast, C-style cast or function-style cast
错误原因,我查了,如下:
Problem
This error message means that you are trying to pass a
multi-byte string (const char [12]) to a function which expects a
unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*,
where TCHAR is char when you compile for
multi-byte and wchar_t for unicode. Since the
compiler doesn't accept the char array, we can safely assume that
the actual type of TCHAR, in this compilation, is wchar_t.
Resolution
You will have to do one of two things:
Change your project configuration to use multibyte strings.
Press ALT+F7 to open the properties, and navigate to Configuration
Properties > General. Switch Character Set to "Use
Multi-Byte Character Set".
Indicate that the string literal, in this case "Hello world!"
is of a specific encoding. This can be done through either
prefixing it with L, such as
L"Hello world!", or surrounding it with the
generic _T("Hello world!") macro.
The latter will expand to the L prefix if you are
compiling for unicode (see #1), and nothing (indicating multi-byte)
otherwise.
Variations
Another error message, indicating the same problem, would
be:
cannot convert
parameter 1 from 'const char [12]' to 'LPCWSTR'
Where LPCWSTR maps to a wchar_t pointer, regardless of your
build configuration. This problem can be resolved primarily by
using solution #2, but in some cases also #1. A lot of the
Microsoft provided libraries, such as the Platform SDK, have got
two variations of each function which takes strings as parameters.
In case of a unicode build, the actual functions are postfixed W,
such as the MessageBoxW seen above. In case of multi-byte, the
function would be MessageBoxA (ASCII). Which of these functions is
actually used when you compile your application, depends on the
setting described in resolution #1 above.
它给出了两种解决办法,我选择了第一种,即设置:项目-属性-配置属性-常规-项目默认值-字符集,初始为使用 Unicode
字符集,点下拉框,选择使用多字节字符集即可
3.编译连接(生成解决方案),生成dll.dll文件,在Debug目录中
二、使用DLL
1.新建工程
新建一个Win32 Console Application,选择空的工程。
2.源代码:
#include
#include
using namespace std;
void main()
{
HMODULE
hMod = LoadLibrary("dll");
if
(hMod)
{
FARPROC a = GetProcAddress(hMod, TEXT("output"));
if (a)
a();
else
cout<
GetProcAddress"<
FreeLibrary(hMod);
}
else
cout<
LoadLibrary"<
}
同上,直接复制上面的C++源文件覆盖{$工程名}.cpp文件中除第一句#include
"stdafx.h"外的所有内容
这时候同样会报错:error C2664: 'LoadLibraryW' : cannot
convert parameter 1 from 'const char [4]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
1>d:vs825_7825_7.cpp(25) : error C2664:
'GetProcAddress' : cannot convert parameter 2 from 'const wchar_t
[7]' to 'LPCSTR'
1> Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast
这样的错误已经是第二次了,聪明的你一定也知道怎么办了......
设置完成之后,正确编译
3.将上面工程生成的dll.dll文件复制到此工程的目录下,保证源文件与DLL文件在同一目录下,可以是工程名下,也可以是工程名里Debug下。如果生成的EXE文件要直接运行,则要保证EXE文件与DLL文件在同一目录下。
4.编译连接,执行。
C++生成与使用DLL指南
本文详细介绍了如何在Visual Studio中使用C++创建DLL文件,包括新建工程、修改源代码、解决字符集问题,以及如何在另一个工程中加载和使用生成的DLL。通过实例演示了解DLL的生成和应用过程。
11万+

被折叠的 条评论
为什么被折叠?



