tags:

views:

116

answers:

3
+1  Q: 

How to use a dll?

I have a .dll file and the .lib file for it.

The DLL talks to an electronic key reader, and allows you to read/write the key ID.

This is the only documentation that comes with:

DLL Usage:
boolean = object.DevicePresent (PROPERTY: true if the device is present)
boolean = object.KeyPresent (PROPERTY: true if a key is in the device)
long = object.KeyId (PROPERTY: gets the keys id)
object.WriteKeyId KeyId (METHOD: Writes new id to the key)
Private Sub object_KeyRemoved (EVENT: Key removed)

I have never used DLL before and really have no idea how I am supposed to use it in a C program. I really have no idea what do past this:

#include <stdlib.h>
#include <windows.h>

typedef int (__cdecl *MYPROC)(LPWSTR); 
int main(int argc, char *argv[])
{
 HINSTANCE hinstLib; 
 hinstLib = LoadLibrary(TEXT("ekey.dll")); 
 if (hinstLib != NULL) 
 { 
   //now what? how do i get the properties or call a method?
 }
 return 0;
}

If someone could show me an example how how to get DevicePresent and how to use WriteKeyId I would be very greatful!

+4  A: 

That documentation suggests that the DLL is an OCX, intended for use with Visual Basic.

Try regsvr32 on it. If that likes it, you can then build the necessary COM API for it from visual studio.

It will be very hard to arrange direct calls to this sort of thing from C, but you can try looking at it with dumpbin and seeing what it exports.

As per the comment, adding a #import for the DLL to your C program is the quickest way forward.

bmargulies
If the dll is an OCX intended for use by visual basic, Then try import it with #import. Then look in the intermediate folder for the auto generated header files that should yield much more info. And questions possibly depending on ones understanding of COM.
Chris Becke
A: 

See GetProcAddress(). Ensure the symbol is exported with dumpbin /exports Foo.dll

E.g.

BOOL *pPresent = (BOOL *)GetProcAddress(hInstLib, _T("DevicePresent"));
if (pPresent) {
  printf("%d\n", *pPresent);
}

Caveat You must know the exact data type this object is at the binary level! There is probably a reference somewhere comparing VB <-> Platform SDK Data types.

Alex
Care to elaborate? As it stands this isn't very helpful.
Daniel Pryden
This isn't likely to help. In a COM DLL, the function you want to call are usually not exported at all. There is some faint hope that the presence of a .lib file suggests that some useful things are exported, but they may not correspond to the documented API.
bmargulies
A: 

It's a COM DLL. Which makes it practically impossible to use in straight C.

kiase
Not really true. Generate a set of headers and you can call com from C. MS supports it.
bmargulies