Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a DLL function in C?

Tags:

c

windows

dll

I was given a DLL that I'm trying to use. The DLL contains the function "send". this is what I did:

#include <stdio.h>
#include <Windows.h>

int main(int argc, char * argv[])
{
    HMODULE libHandle;

    if ((libHandle = LoadLibrary(TEXT("SendSMS.dll"))) == NULL)
    {
        printf("load failed\n");
        return 1;
    }
    if (GetProcAddress(libHandle, "send") == NULL)
    {
        printf("GetProcAddress failed\n");
        printf("%d\n", GetLastError());
        return 1;
    }
    return 0;
}

GetProcAddress returns NULL, and the last error value is 127. (procedure was not found)

What am I doing wrong?

like image 888
Mikey Avatar asked Aug 29 '10 12:08

Mikey


2 Answers

Code look more or less good, so probably something is wrong with *.dll. Please download Dependency Walker application and check what kind of functions are exported by this library.

like image 101
Zuljin Avatar answered Nov 10 '22 19:11

Zuljin


If you running 64bit environment and "sendsms.dll" is compiled as 32bit loadlibrary does not work. You need to compile your project as 32bit to load dlls.

like image 21
ertan Avatar answered Nov 10 '22 19:11

ertan