News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Loading a DLL?

Started by ohmycee, August 17, 2011, 04:05:16 PM

Previous topic - Next topic

ohmycee

Hello all,

I have someone else's DLL, and there's a function in it called ReturnNumber(). How do I load the DLL into Pelles C so I can use the function?

Thanks!

Stefan Pendl

There are two ways:

  • add the header and a static .LIB file of the DLL to your project, so the linker knows it is an external library
  • use the LoadLibrary and GetProcAddress Functions to do this manually

EDIT: Nice to see that you changed the message while I was replying :(
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

I'm so sorry Stefan,  :-[

I felt my question wasn't clear, that's why I changed it. Can I have an example how to use LoadLibrary and GetProcAddress? I'm really lost right now, and very tired. I've been searching the forum for 1 hour. (Pulls some hair out).

I just want to use a simple function from someone else's dll.

Thanks.

Stefan Pendl

If you would have checked the pages I linked to, you would have found the link to the example called Using Run-Time Dynamic Linking ;)

BTW, this is not specific to PellesC, this is regular Windows API programming.
---
Stefan

Proud member of the UltraDefrag Development Team

ohmycee

#4
Hi Stefan,

I don't know why it's so complicating.

mydll.c code:

#include <windows.h>

#define DLL_EXPORT __declspec(dllexport)

DLL_EXPORT int ReturnNumber (void)
{
return 98765;
}



For example, using gcc, I can simply "link" mydll.dll as follows, and I can use all the functions in the dll just by calling it:

gcc test.c -o test -L./ -lmydll

test.c code:


#include <stdio.h>
int main(void)
{
printf("Number: %d", ReturnNumber());
return 0;
}


Inside test.c, I simply call the function ReturnNumber(), and it returns a number. Is there something similar in Pelles C?

Thanks!

ohmycee

#5
Hi timovjl,

I don't know if you're telling me something, or asking me a question?

Let me try my question again... Let's say I have 2 files: testdll.c and test.c

Let's start with testdll.c:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int ReturnNumber (void)
{
return 98765;
}

int ReturnAnotherNumber(void)
{
  return 12345;
}


With GCC, I type this, and I get only a dll file: testdll.dll. I don't get a .def file. Just testdll.dll.
gcc -shared -o testdll.dll testdll.c

At this point, pretend I don't have testdll.c anymore. It's completely gone. I can't make the .lib file. All I have is testdll.dll, and I know there are 2 functions in it.

To use testdll.dll, I write this in test.c:

#include <stdio.h>

int main (int argc, char **argv)
{
  printf("Test: %d\n", ReturnNumber()); //notice I can use the function simply by calling it, no need to loadlibrary or getprocaddress
  printf("Test: %d", ReturnAnotherNumber()); //same thing here
  return 0;
}


To compile the code, I type:
gcc -L. -ltestdll -o test test.c

And everything works.

My question is, can I do this in Pelles C?

Thanks.

ohmycee

Hi timovjl,

Thanks for trying to help me understand, but I'm even more confused now.  :-[

Would you please take me through your code step by step?

This, I understand:
; DllTest.def
LIBRARY DllTest.dll
EXPORTS
ReturnNumber
ReturnAnotherNumber


But what are you trying to do here? What's DllTest.c?
cc -x -Ze DllTest.c DllTest.def kernel32.lib

And here? DllTestTest?
cc -x DllTestTest.c kernel32.lib DllTest.lib

Remember, we don't have the sourcecode for the DLL anymore. It's gone. We only have the dll. So we can't possibly have the .lib file.

Thanks all.