0

I have MyDll.dll and I want access its function Myfunction which is in the form:

Void pascal Myfunction(BOOL);

Here is my code:

#include "stdafx.h"
#include "iostream"
#include "windows.h"
using namespace std;
void calldll();

extern "C" __declspec(dllexport) void Myfunction(BOOL);

void calldll()
{
    HINSTANCE hDll;
    DWORD dwErrorCode=0;
    LoadMe=LoadLibrary(_T("MyDll.dll"));


    if(hDll!=NULL)
    {
        cout<<"\n DLL loaded successfully \n";
    }

    else
        cout<<"\n Unable to load DLL \n";

    Myfunction sp1=(Myfunction)GetProcAddress(hDll,"Myfunction");


    if(sp1!=NULL)
    {
        cout<<"\n Process loaded successfully \n";
    }
    else
    {
              cout<<"\n Unable to load Process \n";

    }


    FreeLibrary(hDll);

}

It is giving error as:

1.syntax error : missing ';' before identifier 'sp1'

2.'sp1' : undeclared identifier

3.syntax error : missing ';' before identifier 'GetProcAddress'

Which I don't understand. Can any help me in this regard.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
  • possible duplicate of [GetProcAddress unble to get adress which of the form Void pascal Myfunction(BOOL)](http://stackoverflow.com/questions/9003231/getprocaddress-unble-to-get-adress-which-of-the-form-void-pascal-myfunctionbool) – Raymond Chen Jan 26 '12 at 10:40

1 Answers1

2

You need to declare a variable that is a pointer to a function that returns void and accepts a BOOL argument:

typedef void (pascal *MYFUNCTION_PTR)(BOOL);

MYFUNCTION_PTR fp = (MYFUNCTION_PTR)GetProcAddress(hDll,"Myfunction");

For example:

#include <stdafx.h>
#include <iostream>
#include <windows.h>

void calldll();

void calldll()
{
    typedef void (pascal *MYFUNCTION_PTR)(BOOL);
    HINSTANCE hDll;

    hDll = LoadLibrary(_T("MyDll.dll"));

    if(hDll!=NULL)
    {
        cout<<"\n DLL loaded successfully \n";

        MYFUNCTION_PTR sp1=(MYFUNCTION_PTR)GetProcAddress(hDll,"Myfunction");

        if(sp1!=NULL)
        {
            cout<<"\n Process loaded successfully \n";
        }
        else
        {
            cout<<"\n Unable to locate function: " << GetLastError() << \n";
        }

        FreeLibrary(hDll);
    }
    else
    {
        cout<<"\n Unable to load DLL \n";
    }

}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • i have declared the variable already by Myfunction sp1=(Myfunction)GetProcAddress(hDll,"Myfunction). – srinivasporam Jan 25 '12 at 12:30
  • by adding this it is printing message as "DLL loaded successfully" "Unable load function" "127" – srinivasporam Jan 25 '12 at 12:39
  • Error code 127 means "The specified procedure could not be found." From the earlier [question](http://stackoverflow.com/questions/9001619/regarding-getprocaddress/9001711#9001711) @DavidHeffernan mentioned the name decoration due to use of `pascal` calling convention. Did you dump the exports of the DLL using `dumpbin.exe` as I mentioned to see the actual exported name of the function? – hmjd Jan 25 '12 at 14:02