Generate/create DLLs and call/reference functions in DLLs, global variables, static C++ class member

Create a DLL:

 New DLL project:

Under VC6, File —> New —> Projects —> Win32 Dynamic-Link Library (usually in the penultimate) —> Enter the name —> Select An empty DLL project. —> Finish

 New header file:

  File —> New —> Files —> C/C++ Header File

My header file is: ErrTest.h

 
  1. /***************************************************************************

  2. Module: ErrTest.h

  3. ***************************************************************************/

  4. #ifndef ERRTEST_H

  5. #define ERRTEST_H

  6. // This macro is defined by the system default in (VC6.0) project properties --> C / C + + --> project options

  7. // So when the same DLL project references this header file, there is no need to manually define ERRTEST_EXPORTS.

  8. #ifdef ERRTEST_EXPORTS

  9. #define ERRTESTAPI __declspec(dllexport) // Defined when this header file is referenced by the source code module in the DLL

  10. #else

  11. #define ERRTESTAPI __declspec(dllimport) // Defined when this header file is referenced by a source code module in another project

  12. #endif

  13. Class ERRTESTAPI ErrTest { // ERRTESTAPI is not placed on the left side of the class!

  14. public:

  15. static void printf(const char *fmt, ...);

  16. static HANDLE GetConsole(int nStdHandle = STD_OUTPUT_HANDLE);

  17. static HANDLE hConsole;

  18. };

  19. ERRTESTAPI extern int nErrTest; // add extern

  20. ERRTESTAPI void func(void);

  21. #endif

  22. // End of File /


 

 New source file:

  File —> New —> Files —> C++ Source File

My source file is: ErrTest.cpp

 
  1. /***************************************************************************

  2. Module: ErrTest.cpp

  3. ***************************************************************************/

  4. #include <windows.h>

  5. #include <stdio.h>

  6. #include <exception>

  7. #include <stdarg.h>

  8. #include "ErrTest.h"

  9. HANDLE ErrTest::hConsole = NULL;

  10. int nErrTest = 5;

  11. // output the formatted string to the console window

  12. void ErrTest::printf(const char *fmt, ...)

  13. {

  14. const int BUFSIZE = 65535;

  15. int sz;

  16. // const char *p;

  17. char szBuf[BUFSIZE];

  18. GetConsole();

  19. va_list valist;

  20. // p = fmt+strlen(fmt)-1;

  21. va_start(valist, fmt);

  22. // vsprintf(szBuf, fmt, valist);

  23. vsprintf(szBuf, fmt, valist);

  24. va_end(valist);

  25. sz = strlen(szBuf)*sizeof(char);

  26. __try {

  27. WriteConsole(GetConsole(), szBuf, sz, NULL, NULL);

  28. } __except (EXCEPTION_EXECUTE_HANDLER) {

  29. int e = GetLastError();

  30. MessageBox(NULL, TEXT("Error in ErrTest::printf when WriteConsole()!"), 0, 0);

  31. }

  32. }

  33. // Get the console handle, the default is to return the handle for the output.

  34. HANDLE ErrTest::GetConsole(int nStdHandle /* = STD_OUTPUT_HANDLE */)

  35. {

  36. if (hConsole == NULL) {

  37. AllocConsole();

  38. hConsole = GetStdHandle(nStdHandle);

  39. // hConsole = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,

  40. // FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

  41. }

  42. return hConsole;

  43. }

  44. void func()

  45. {

  46. MessageBox(0, "errtest.cpp 00", 0, 0);

  47. }


 

 Generate a DLL file:

After Build (or F7), you will find two files ErrTest.lib and ErrTest.dll in the Debug folder, plus ErrTest.h, which is the most important three files generated by this project.

Create a runnable file for the example:

  File —> New —> Win32 Application —> A typical "Hello World!" application —> Finish

Add the appropriate content as shown in the Chinese code below:

 
  1. // 1.cpp : Defines the entry point for the application.

  2. //

  3. #include "stdafx.h"

  4. #include "resource.h"

  5. #include "ErrTest.h" // "———— Add header file

  6. #pragma comment(lib, "ErrTest.lib") // "--- tell the linker to load the lib file

  7. #define MAX_LOADSTRING 100

  8. // Global Variables:

  9. HINSTANCE hInst; // current instance

  10. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text

  11. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

  12. // Foward declarations of functions included in this code module:

  13. ATOM MyRegisterClass(HINSTANCE hInstance);

  14. BOOL InitInstance(HINSTANCE, int);

  15. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  16. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

  17. int APIENTRY WinMain(HINSTANCE hInstance,

  18. HINSTANCE hPrevInstance,

  19. LPSTR lpCmdLine,

  20. int nCmdShow)

  21. {

  22. // TODO: Place code here.

  23. ErrTest::printf("ErrTest::printf(): %d", nErrTest); // "--- Call class static members and global variables in DLL

  24. Func(); // "--- call DLL function

  25. MSG msg;

  26. HACCEL hAccelTable;


 


Then, copy the ErrTest.h header file, ErrTest.lib, ErrTest.dll file generated after the DLL was created to the folder where the sample project .dsw is located. Press Ctrl+F5 to run the result.

note:

  1The linker must be notified to load the lib file. If the linker cannot load the file, all variables and functions referenced in the project will have an unresolved external symbol error, as follows:

 
  1. --------------------Configuration: 1 - Win32 Debug--------------------

  2. Compiling...

  3. 1.cpp

  4. Linking...

  5. 1.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl func(void)" (__imp_?func@@YAXXZ)

  6. 1.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static void __cdecl ErrTest::printf(char const *,...)" (__imp_?printf@ErrTest@@SAXPBDZZ)

  7. 1.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int nErrTest" (__imp_?nErrTest@@3HA)

  8. Debug/1.exe : fatal error LNK1120: 3 unresolved externals

  9. Error executing link.exe.

  10. 1.exe - 4 error(s), 0 warning(s)


 

Then, there are two ways to notify the linker to load lib. One is to add #pragma comment(lib, "ErrTest.lib") to the source code as shown above. Note here that #pragma is not # Progma! The other is to add ErrTest.lib to Project -> settings -> link tab -> Object / library modules.

  2The .DLL and lib and header files are not necessarily in the same directory as the exe file. They can be placed anywhere in the system environment variable PATH (or include include, lib).

//

///

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值