How do you export members to an exe from a custom DLL?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

How do you export members to an exe from a custom DLL?

 
0
  #1
Oct 16th, 2008
Ok, I have a DLL I made. In the DllMain function, I have a variable called my_var (const char[13]). I compiled the dll (called "WGCL") and put it in the debug directory of my Win32 Project. I used /DELAYLOAD:WGCL.dll to Delay Load the DLL which seems to be the only way to link the library. It loads the DLL fine and there is no errors or warnings. But when I try to use the variable (which is in the DllMain section), it says it doesnt exist. So obviously the compiler cannot access the variable from DllMain because it has not been properly exported, how can I export the variable? I have tried extern "C", and extern "C" PASCAL EXPORT. How do I export this variable for use in my Win32 Project?

This is how it looks:

WGCL DLL Proj. - WGCL.cpp
  1. #include "stdafx.h"
  2.  
  3. #ifdef _MANAGED
  4. #pragma managed(push, off)
  5. #endif
  6.  
  7. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  8. {
  9. const char my_var[13] = "Hello World!";
  10.  
  11. return TRUE;
  12. }
  13.  
  14. #ifdef _MANAGED
  15. #pragma managed(pop)
  16. #endif

WGCL Win32 EXE Proj. - WGCL Test.cpp
  1. #include "stdafx.h"
  2. #include "WGCL Test.h"
  3.  
  4. #define MAX_LOADSTRING 100
  5.  
  6. // Global Variables:
  7. HINSTANCE hInst; // current instance
  8. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  9. TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  10.  
  11. // Forward declarations of functions included in this code module:
  12. ATOM MyRegisterClass(HINSTANCE hInstance);
  13. BOOL InitInstance(HINSTANCE, int);
  14. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  15. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  16.  
  17. int APIENTRY _tWinMain(HINSTANCE hInstance,
  18. HINSTANCE hPrevInstance,
  19. LPTSTR lpCmdLine,
  20. int nCmdShow)
  21. {
  22. UNREFERENCED_PARAMETER(hPrevInstance);
  23. UNREFERENCED_PARAMETER(lpCmdLine);
  24.  
  25. // TODO: Place code here.
  26. MSG msg;
  27. HACCEL hAccelTable;
  28.  
  29. // Initialize global strings
  30. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  31. LoadString(hInstance, IDC_WGCLTEST, szWindowClass, MAX_LOADSTRING);
  32. MyRegisterClass(hInstance);
  33.  
  34. // Perform application initialization:
  35. if (!InitInstance (hInstance, nCmdShow))
  36. {
  37. return FALSE;
  38. }
  39.  
  40. hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WGCLTEST));
  41.  
  42. // Main message loop:
  43. while (GetMessage(&msg, NULL, 0, 0))
  44. {
  45. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  46. {
  47. TranslateMessage(&msg);
  48. DispatchMessage(&msg);
  49. }
  50. }
  51.  
  52. return (int)msg.wParam;
  53. }
  54.  
  55. ATOM MyRegisterClass(HINSTANCE hInstance)
  56. {
  57. WNDCLASSEX wcex;
  58.  
  59. wcex.cbSize = sizeof(WNDCLASSEX);
  60.  
  61. wcex.style = CS_HREDRAW | CS_VREDRAW;
  62. wcex.lpfnWndProc = WndProc;
  63. wcex.cbClsExtra = 0;
  64. wcex.cbWndExtra = 0;
  65. wcex.hInstance = hInstance;
  66. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WGCLTEST));
  67. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  68. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  69. wcex.lpszClassName = szWindowClass;
  70. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  71.  
  72. return RegisterClassEx(&wcex);
  73. }
  74.  
  75. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  76. {
  77. HWND hWnd;
  78.  
  79. hInst = hInstance; // Store instance handle in our global variable
  80.  
  81. hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  82. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  83.  
  84. if (!hWnd)
  85. {
  86. return FALSE;
  87. }
  88.  
  89. ShowWindow(hWnd, nCmdShow);
  90. UpdateWindow(hWnd);
  91.  
  92. return TRUE;
  93. }
  94.  
  95. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  96. {
  97. int wmId, wmEvent;
  98. PAINTSTRUCT ps;
  99. HDC hdc;
  100.  
  101. switch (message)
  102. {
  103. case WM_CREATE:
  104. {
  105. // Use my_var in MessageBox (it throws an error: error C2065: 'my_var': undeclared identifier)
  106. MessageBox(hWnd, (LPCWSTR)my_var, (LPCWSTR)"Hello World! Its 420!", MB_OK);
  107. }
  108. break;
  109. case WM_DESTROY:
  110. PostQuitMessage(0);
  111. break;
  112. default:
  113. return DefWindowProc(hWnd, message, wParam, lParam);
  114. }
  115. return 0;
  116. }

Can anybody: help me, refer me to a link, give me a tip/hint, or something please?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do you export members to an exe from a custom DLL?

 
0
  #2
Oct 16th, 2008
You can't put it in DLLMain() because just like any other function the object will disappear as soon as DllMain() returns. Instead, the object must be global. Then to make it visible to the application program you have two choices:
1) use __dllspec(__dllexport) tag
__declspec( dllexport ) const char my_var[13] = "Hello World!";
or
2) create a *.def file and ass my_var to it

In the application program
extern __declspec( dllimport ) const char my_var[13];

More info here.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: How do you export members to an exe from a custom DLL?

 
0
  #3
Oct 17th, 2008
Originally Posted by Ancient Dragon View Post
You can't put it in DLLMain() because just like any other function the object will disappear as soon as DllMain() returns. Instead, the object must be global. Then to make it visible to the application program you have two choices:
1) use __dllspec(__dllexport) tag
__declspec( dllexport ) const char my_var[13] = "Hello World!";
or
2) create a *.def file and ass my_var to it

In the application program
extern __declspec( dllimport ) const char my_var[13];

More info here.
The exporting works, but I cant import the variable. It says: Unresovled exertnal symbol extern __declspec( dllimort )
I even tried loading user32.lib manually even though it was automatically linked.
And yes, I didnt put the import in WinMain().
Any tips on what I'm doing wrong?
Last edited by killdude69; Oct 17th, 2008 at 1:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: How do you export members to an exe from a custom DLL?

 
0
  #4
Oct 17th, 2008
um .. could it be a spelling mistake on your part ?

its __declspec( dllimport )

not __declspec( dllimort )
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 45
Reputation: killdude69 is an unknown quantity at this point 
Solved Threads: 1
killdude69 killdude69 is offline Offline
Light Poster

Re: How do you export members to an exe from a custom DLL?

 
0
  #5
Oct 18th, 2008
Originally Posted by stilllearning View Post
um .. could it be a spelling mistake on your part ?

its __declspec( dllimport )

not __declspec( dllimort )
No, the keyword highlighted, thats how I know it was not mispelled.
I just mispelled it on the thread, but not in the code.
Last edited by killdude69; Oct 18th, 2008 at 10:22 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do you export members to an exe from a custom DLL?

 
0
  #6
Oct 18th, 2008
Did you link your application program with the *.lib file that the compiler produced for the *.dll program?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 693 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC