Note that there are some explanatory texts on larger screens.

plurals
  1. POcan't get address of the dll function with GetProcAddress
    primarykey
    data
    text
    <p>I created a dll with VS C++ (of course as a dll project) with the following code of the header file:</p> <pre><code>#pragma once #include &lt;iostream&gt; #include "..\..\profiles/ProfileInterface.h" using namespace std; extern "C" __declspec(dllexport) class CExportCoordinator: public CProfileInterface { public: CExportCoordinator(void); virtual ~CExportCoordinator(void); CProfileInterface* Create(); void Initialize(); void Start(); }; </code></pre> <p>Here is .cpp file of the dll:</p> <pre><code>#include "StdAfx.h" #include "ExportCoordinator.h" CExportCoordinator::CExportCoordinator(void) { } CExportCoordinator::~CExportCoordinator(void) { } CProfileInterface* CExportCoordinator::Create(){ cout &lt;&lt; "ExportCoordinator3 created..." &lt;&lt; endl; return new CExportCoordinator(); } void CExportCoordinator::Initialize(){ cout &lt;&lt; "ExportCoordinator3 initialized..." &lt;&lt; endl; } void CExportCoordinator::Start(){ cout &lt;&lt; "ExportCoordinator3 started..." &lt;&lt; endl; } </code></pre> <p>I exported the whole class <code>CExportCoordinator</code> because I need to use all three methods it offers. Following is the code from the main application loading the, above given, dll on the fly.</p> <pre><code> typedef CProfileInterface* (WINAPI*Create)(); int _tmain(int argc, _TCHAR* argv[]) { HMODULE hLib = LoadLibrary(name); if(hLib==NULL) { cout &lt;&lt; "Unable to load library!" &lt;&lt; endl; return NULL; } char mod[MAXMODULE]; GetModuleFileName(hLib, (LPTSTR)mod, MAXMODULE); cout &lt;&lt; "Library loaded: " &lt;&lt; mod &lt;&lt; endl; Create procAdd = (Create) GetProcAddress(hLib,"Create"); if (!procAdd){ cout &lt;&lt; "function pointer not loaded"; } return; } </code></pre> <p>On the output I get that correct library is loaded, but that function pointer <code>procAdd</code> is NULL. I thought it had something to do with name mangling and added <code>extern "C"</code> when exporting the class in header of dll, but nothing changed. Btw, I used <a href="http://www.nirsoft.net/utils/dll_export_viewer.html" rel="nofollow noreferrer">dll export viewer</a> for viewing the exported functions of the class, and the whole class is exported correctly. Any help?</p> <p><strong>UPDATE</strong><br> there is an error in the header file of dll. I shouldn't be using <code>extern "C" __declspec(dllexport)</code> before class because then class won't be exported at all. If I use <code>class __declspec(dllexport) CExportCoordinator</code> then the class is exported correctly, but anyway I can't get the address of the function other than NULL.</p>
    singulars
    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