tags:

views:

71

answers:

4

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?

+3  A: 

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.

Zuljin
Only one function is exported - "MAGIC_BIND".Actually the example code that was given was in Magic. Is it possible that the dll can be used only through Magic?
Mikey
Calling that function requires magic. http://www.ng-sw.de/mg-wikka/MagicDLLs
Hans Passant
A: 

Probably the DLL doesn't export such a function.

This is usually caused by the "decorations" the compiler adds to the function name. For instance "send" may actually be seen as:

  • _send
  • _send@4
  • ?send@@ABRACADABRA

To resolve this that's what you should do:

  1. Use the "depends" utility (depends32.exe, shipped with MSVC) to view what your DLL actually exports.
  2. If you're the author of the DLL - you may force the export name to be what you want, by using "def" file (for linker)
valdo
A: 

I noticed that you're using TEXT on LoadLibrary, but not on GetProcAddress. If GetProcAddress is misinterpreting your string, it could be looking for the wrong function.

DeadMG
GetProcAddress does not take wide strings.
dreamlax
+1  A: 

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.

Ertan