Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variables to function exported by a DLL, I get error LNK2001
    primarykey
    data
    text
    <p>I have to pass a HWND variable from the main program to a function exported by a DLL. Variable is called mainHwnd and DLL is defined in this way:</p> <p><strong>mydll.h</strong></p> <pre><code>#ifdef MYDLL_EXPORTS #define MYDLL_API extern "C" __declspec(dllexport) #else #define MYDLL_API extern "C" __declspec(dllimport) #endif MYDLL_API HWND mainHwnd; MYDLL_API void testFunction(void); MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam); </code></pre> <p><strong>mydll.cpp</strong></p> <pre><code>#include "stdafx.h" #include "mydll.h" #include &lt;string&gt; #define CLASSNAMELEN 5 MYDLL_API HWND mainHwnd = 0; // This is an example of an exported function. MYDLL_API void testFunction(void) { MessageBox(NULL, (LPCWSTR)L"Test", (LPCWSTR)L"Test", MB_OK); } MYDLL_API LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam) { // processes the message if(nCode &gt;= 0) { if(wParam != NULL &amp;&amp; (wParam == WM_RBUTTONDOWN || wParam == WM_RBUTTONUP)) { std::wstring s; MessageBox(NULL, (LPCWSTR)L"Captured mouse right button", (LPCWSTR)L"Test", MB_OK); MOUSEHOOKSTRUCT *m = (MOUSEHOOKSTRUCT*) lParam; GetClassName(m-&gt;hwnd, (LPWSTR) s.c_str(), CLASSNAMELEN); if(s == L"Edit") SendMessage(mainHwnd, WM_APP, 0, (LPARAM) lParam); } } // calls next hook in chain return CallNextHookEx(NULL, nCode, wParam, lParam); } </code></pre> <p><strong>main program</strong></p> <pre><code>MYDLL_API HWND mainHwnd; ... case WM_CREATE: { // now it will load DLL and set up hook procedure for mouse events // declares local variables HOOKPROC hkprcMouseProc; HINSTANCE hinstDLL; HHOOK hhookMouseProc; // loads DLL if((hinstDLL = LoadLibrary(TEXT("C:\\Users\\Francesco\\Dropbox\\poli\\bi\\not\\pds\\sp\\wk5\\lsp5\\Debug\\mydll.dll"))) == NULL) { MessageBox(hWnd, (LPCWSTR)L"Error loading DLL", (LPCWSTR)L"Error", MB_OK | MB_ICONERROR); break; } // saves main window handle for DLL functions mainHwnd = hWnd; ... </code></pre> <p>Compiling I get </p> <pre><code>error LNK2001: unresolved external symbol __imp__mainHwnd </code></pre> <p>while using dumpbin /exports mydll.dll I get that global variable name is mangled as:</p> <pre><code>mainHwnd = _mainHwnd </code></pre> <p>I went through a lot of pages on google with no results. Maybe there is a conceptual error.. Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload