Search code examples
c++visual-c++dllcastingdllexport

function call from dll [expression preceding parentheses of apparent call must have (pointer-to-) function type]


I am totally new to c++ and trying to create a sample dll and a client that calls a function from dll.

I have created a solution with VC++ and two projects inside one dll and one console.

Inside the plugin_dll project, I have a header and a cpp file:

plugin.h    
    #pragma once
    #define EXPORT extern "C" __declspec (dllexport)
    EXPORT char const* Greetings();

plugin.cpp
    #include "stdafx.h"
    #include "plugin.h"

    char const * Greetings()
    {
        return "Hello From  Plugin";
    }

in the console app project I have

#include "pch.h"
#include "stdafx.h"
#include <iostream>

using namespace std;
int main()
{

    HMODULE DllHandler = ::LoadLibrary(L"plugin.dll");
    char const* const getGreetings=reinterpret_cast<char const*>(::GetProcAddress(DllHandler, "Greetings"));
    cout << getGreetings() << endl; // Here I get the Error
    cin.get();
 }

at the cout line I get the error

E0109   expression preceding parentheses of apparent call must have (pointer-to-) function 

and compile time error

C2064   term does not evaluate to a function taking 0 arguments 

First, is this the correct approach for creating a dll exporting function and calling it in a client app? Is this is a correct approach to how to solve the error?


Solution

  • getGreetings is a const char*, not a function, what you want is to use reinterpret_cast<const char*(*)()>() instead to make it in a function and not a variable.