77222

Error with compiling DLL with intel compiler

Question:

I'm trying to compile DLL from console, without using any IDE and faced with next error.

I wrote this code:

<strong>test_dll.cpp</strong>

#include <windows.h> #define DLL_EI __declspec(dllexport) BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){ return 1; } extern "C" int DLL_EI func (int a, int b){ return a + b; }

Then compiled with command icl /LD test_dll.cpp. And I'm trying to call this func from another program:

<strong>prog.cpp</strong>

int main(){ HMODULE hLib; hLib = LoadLibrary("test_dll.dll"); double (*pFunction)(int a, int b); (FARPROC &)pFunction = GetProcAddress(hLib, "Function"); printf("begin\n"); Rss = pFunction(1, 2); }

Compile it with icl prog.cpp. Then I run it, and it fails with standard window <em>"Program isn't working"</em>. Maybe there is a <em>segmentation fault</em> error.

What am I doing wrong?

Answer1:

Check that both LoadLibrary() and GetProcAddress() succeed, in this case they definitely will not as the exported function is called func, not "Function" as specified in the argument to GetProcAddress() meaning the function pointer will be NULL when the attempt to invoke it is made.

The signature of the function pointer also does not match the signature of the exported function, the exported function returns an int and the function pointer is expecting a double.

For example:

typedef int (*func_t)(int, int); HMODULE hLib = LoadLibrary("test_dll.dll"); if (hLib) { func_t pFunction = (func_t)GetProcAddress(hLib, "func"); if (pFunction) { Rss = pFunction(1, 2); } else { // Check GetLastError() to determine // reason for failure. } FreeLibrary(hLib); } else { // Check GetLastError() to determine // reason for failure. }

Recommend

  • DllMain and Qt Mfc Migration
  • PostMessage(), SendMessage not working in ATL dll (event handling)
  • Error with compiling DLL with intel compiler
  • c++ error: invalid use of member 'calls_object::OBJECT' in static member function
  • Timing inconsistency with killing thread using event
  • Implementing gravity flipping mechanics using C++ Win32
  • Connect to running IE instance C++
  • Abort thread properly when dialog box close button is clicked
  • Windows System Programming OpenFile function
  • C struct to Java JNA Structure (pointer to struct)
  • Calling unmanaged dll from C#. Take 2
  • How to send Return/Enter character to the Console using WriteConsoleInput/WriteConsole
  • 1>main.obj : error LNK2001: unresolved external symbol _D3D10CreateDeviceAndSwapChain@32
  • Creating an HICON from a byte array in C++?
  • _CrtDumpMemoryLeaks( ) == 1 on first line of code?
  • displaying a message when the user taps a key
  • How to Fire Windows Service stop event on System restart?
  • Guzzle Error with Laravel. ClientException in RequestException.php line 107:
  • C# Getting window's title by handle in Hebrew return question marks
  • Programmatically set PIN/Password used to unlock device
  • x86: Count transitions from 1 to 0 in 32 bit number
  • C++ reference variable across files
  • What does the lower left pane in OllyDbg displays?
  • How to intall Tomcat as a daemon on OS X?
  • Custom Nav Title offset ios 11
  • C++ pointer value changes with static_cast
  • Functions in global context
  • Update CALayer sublayers immediately
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?